SlideShare a Scribd company logo
• Object Oriented
Programming
Unit-02
Selection,
Mathematical
Functions and
loops
 Looping
Outline
If statement
Two way if statement
Nested if statement
Switch statement
Conditional Expression
While loop
Do-while loop
For loop
Nested loop
Break and continue statement
Common mathematical
expression
3
Control Statements
 Control Statements in Java is one of the fundamentals required for Java
Programming. It allows the smooth flow of a program.
 Statement can simply be defined as an instruction given to the computer to
perform specific operations.
 A control statement in java is a statement that determines whether the other
statements will be executed or not.
 Control statements in Java,
 If statement
 If-else statement
 If-else ladder statement
 Switch statement
4
if statement
 if statement tests the condition. It executes the if block if condition is true.
public class IfStatementDemo{
public static void main(String[] ar)
{
int a = 10, b = 20;
if( a < b ) {
System.out.println("A is smaller than B");
}
}
}
5
if-else statement
 if-else statement also tests the condition. It executes the if block if condition is
true otherwise else block is executed.
public class IfElseStatementDemo{
public static void main(String[] ar)
{
int a = 10, b = 20;
if(a<b) {
System.out.println("A is smaller than B");
}
else {
System.out.println(“A is not smaller than B");
}
}
}
6
if-else statement
 if-else-if ladder statement executes one condition from multiple statements.
int marks = 65;
if (marks < 60) {
System.out.println("fail");
} else if (marks >= 60 && marks < 80) {
System.out.println("B grade");
} else if (marks >= 80 && marks < 90) {
System.out.println("A grade");
} else if (marks >= 90 && marks < 100) {
System.out.println("A+ grade");
} else {
System.out.println("Invalid!");
}
7
Nested If statement
 We can also use if/else if statement inside another if/else if statement, this is
known as nested if statement.
int username = Integer.parseInt(args[0]);
int password = Integer.parseInt(args[1]);
double balance = 123456.25;
if(username==1234){
if(password==987654){
System.out.println("Your Balance is
="+balance);
}
else{
System.out.println("Password is invalid");
}
}
else{
System.out.println("Username is invalid");
}
8
switch statement
 switch statement executes one statement from multiple conditions. It is like if-
else-if ladder statement.
public class SwitchExampleDemo {
public static void main(String[] args)
{
int number = 20;
switch (number) {
case 10:
System.out.println("10");
break;
case 20:
System.out.println("20");
break;
default:
System.out.println("Not 10 or
20");
}
}
}
9
Programs to perform (Conditional Statements)
 Write a Java program to get a number from the user and print whether it is
positive or negative.
 Write a program to find maximum no from given 3 no.
 The marks obtained by a student in 5 different subjects are input through the
keyboard.
 The student gets a division as per the following rules:
 Percentage above or equals to 60-first division
 Percentage between 50 to 59-second division
 Percentage between 40 and 49-Third division
 Percentage less than 40-fail
Write a program to calculate the division obtained by the student.
 Write a Java program that takes a number from the user and displays the name
of the weekday accordingly (For example if user enter 1 program should return
Monday) .
10
Looping Statement
 Looping in programming languages is a feature which facilitates the execution
of a set of instructions/functions repeatedly while some condition evaluates to
true.
 Java provides three ways for executing the loops. While all the ways provide
similar basic functionality, they differ in their syntax and condition checking
time.
 While loop
 Do-while loop
 For
 Foreach (will cover this after array)
11
While Loop
 while loop is used to iterate a part of the program several times. while is entry
control loop.
 If the number of iteration is not fixed, it is recommended to use while loop.
//code will print 1 to 9
public class WhileLoopDemo {
public static void main(String[] args) {
int number = 1;
while(number < 10) {
System.out.println(number);
number++;
}
}
}
12
Do-while Loop
 do-while loop is executed at least once because condition is checked after loop
body.
//code will print 1 to 9
public class DoWhileLoopDemo {
public static void main(String[] args) {
int number = 1;
do {
System.out.println(number);
number++;
}while(number < 10) ;
}
}
13
For Loop
 for loop is used to iterate a part of the program several times.
 If the number of iteration is fixed, it is recommended to use for loop.
//code will print 1 to 9
public class ForLoopDemo {
public static void main(String[] args)
{
for(int number=1;number<10;number++)
{
System.out.println(number);
}
}
}
14
Nested Loop
15
Programs to perform (Looping Statements)
 Write a program to print first n odd numbers.
 Write a program to check that the given number is prime or not.
 Write a program to draw given patterns,
16
Break statement
 When a break statement is encountered inside a loop, the loop is immediately
terminated and the program control resumes at the next statement following
the loop.
//code will print 1 to 4 followed by “After Loop”
public class BreakDemo{
public static void main(String[] args)
{
for(int number=1;number<10;number++)
{
if(number==5) {
break;
}
System.out.println(number);
}
System.out.println(“After Loop”);
}
}
17
Continue statement
 The continue statement is used in loop control structure when you need to
immediately jump to the next iteration of the loop. It can be used with for loop
or while loop.
//code will print 1 to 9 but not 5, followed by “After
Loop”
public class ContinueDemo {
public static void main(String[] args)
{
for(int number=1;number<10;number++)
{
if(number==5) {
continue;
}
System.out.println(number);
}
System.out.println(“After Loop”);
}
}
18
Math class
 The Java Math class provides more advanced mathematical calculations
other than arithmetic operator.
 The java.lang.Math class contains methods which performs basic numeric
operations such as the elementary exponential, logarithm, square root, and
trigonometric functions.
 All the methods of class Math are static.
 Fields :
 Math class comes with two important static fields
 E : returns double value of Euler's number (i.e 2.718281828459045).
 PI : returns double value of PI (i.e. 3.141592653589793).
19
Methods of class Math
20
Methods of class Math (Cont.)
21
Methods of class Math (Cont.)
22
Methods of class Math (Cont.)
23
Methods of class Math (Cont.)
24
Math Example
public class MathDemo {
public static void main(String[] args) {
double sinValue = Math.sin(Math.PI / 2);
double cosValue = Math.cos(Math.toRadians(80));
int randomNumber = (int)(Math.random() * 100);
// values in Math class must be given in
Radians
// (not in degree)
System.out.println("sin(90) = " + sinValue);
System.out.println("cos(80) = " + cosValue);
System.out.println("Random = " + randomNumber);
}
}

More Related Content

PPTX
Loop
prabhat kumar
 
PDF
Control flow statements in java web applications
RajithKarunarathne1
 
PPTX
Lecture - 5 Control Statement
manish kumar
 
PPTX
Unit- 1 -Java - Decision Making . pptx
CmDept
 
PPTX
Control structures in java
VINOTH R
 
PPTX
Control statements in java
Madishetty Prathibha
 
PPTX
3. Java Installations ppt for engineering
vyshukodumuri
 
PPTX
control statements
Azeem Sultan
 
Control flow statements in java web applications
RajithKarunarathne1
 
Lecture - 5 Control Statement
manish kumar
 
Unit- 1 -Java - Decision Making . pptx
CmDept
 
Control structures in java
VINOTH R
 
Control statements in java
Madishetty Prathibha
 
3. Java Installations ppt for engineering
vyshukodumuri
 
control statements
Azeem Sultan
 

Similar to Unit-02 Selection, Mathematical Functions and loops.pptx (20)

PPS
Programming in Arduino (Part 2)
Niket Chandrawanshi
 
PPTX
22H51A6755.pptx
Parameshwar Maddela
 
PPTX
LOOPING STATEMENTS, JAVA,PROGRAMMING LOGIC
DiwakaranM3
 
PPTX
C Programming Control Structures(if,if-else)
poonambhagat36
 
PPTX
Control Statement programming
University of Potsdam
 
PPT
Chapter 4 flow control structures and arrays
sshhzap
 
PDF
Loops and conditional statements
Saad Sheikh
 
PDF
OIT 116 LOOPS AND CONDITION STATEMENTS.pdf
Carlos701746
 
PPTX
07 flow control
dhrubo kayal
 
PPTX
Java While Loop
Ducat India
 
PPT
Control statements
CutyChhaya
 
PPTX
LESSON 4 Control Flow Statements in JAVA.pptx
KiRe6
 
PDF
java notes.pdf
RajkumarHarishchandr1
 
PPT
Repetition Structure
PRN USM
 
PDF
csj-161127083146power point presentation
deepayaganti1
 
PPTX
Programming in java - Concepts- Operators- Control statements-Expressions
LovelitJose
 
PPT
Loops
Kamran
 
PPT
C Sharp Jn (3)
jahanullah
 
PPTX
Java PSkills Session-4 PNR.pptx
ssuser99ca78
 
PPT
Ch3 repetition
Hattori Sidek
 
Programming in Arduino (Part 2)
Niket Chandrawanshi
 
22H51A6755.pptx
Parameshwar Maddela
 
LOOPING STATEMENTS, JAVA,PROGRAMMING LOGIC
DiwakaranM3
 
C Programming Control Structures(if,if-else)
poonambhagat36
 
Control Statement programming
University of Potsdam
 
Chapter 4 flow control structures and arrays
sshhzap
 
Loops and conditional statements
Saad Sheikh
 
OIT 116 LOOPS AND CONDITION STATEMENTS.pdf
Carlos701746
 
07 flow control
dhrubo kayal
 
Java While Loop
Ducat India
 
Control statements
CutyChhaya
 
LESSON 4 Control Flow Statements in JAVA.pptx
KiRe6
 
java notes.pdf
RajkumarHarishchandr1
 
Repetition Structure
PRN USM
 
csj-161127083146power point presentation
deepayaganti1
 
Programming in java - Concepts- Operators- Control statements-Expressions
LovelitJose
 
Loops
Kamran
 
C Sharp Jn (3)
jahanullah
 
Java PSkills Session-4 PNR.pptx
ssuser99ca78
 
Ch3 repetition
Hattori Sidek
 
Ad

Recently uploaded (20)

PDF
Chad Ayach - A Versatile Aerospace Professional
Chad Ayach
 
PDF
Introduction to Ship Engine Room Systems.pdf
Mahmoud Moghtaderi
 
PPTX
22PCOAM21 Session 2 Understanding Data Source.pptx
Guru Nanak Technical Institutions
 
PPTX
IoT_Smart_Agriculture_Presentations.pptx
poojakumari696707
 
PDF
AI-Driven IoT-Enabled UAV Inspection Framework for Predictive Maintenance and...
ijcncjournal019
 
PPTX
Information Retrieval and Extraction - Module 7
premSankar19
 
PDF
EVS+PRESENTATIONS EVS+PRESENTATIONS like
saiyedaqib429
 
PPTX
database slide on modern techniques for optimizing database queries.pptx
aky52024
 
PPTX
MSME 4.0 Template idea hackathon pdf to understand
alaudeenaarish
 
PPTX
Chapter_Seven_Construction_Reliability_Elective_III_Msc CM
SubashKumarBhattarai
 
PPTX
MULTI LEVEL DATA TRACKING USING COOJA.pptx
dollysharma12ab
 
PPTX
business incubation centre aaaaaaaaaaaaaa
hodeeesite4
 
PDF
Construction of a Thermal Vacuum Chamber for Environment Test of Triple CubeS...
2208441
 
PDF
CAD-CAM U-1 Combined Notes_57761226_2025_04_22_14_40.pdf
shailendrapratap2002
 
PDF
STUDY OF NOVEL CHANNEL MATERIALS USING III-V COMPOUNDS WITH VARIOUS GATE DIEL...
ijoejnl
 
PPTX
MT Chapter 1.pptx- Magnetic particle testing
ABCAnyBodyCanRelax
 
PDF
Zero carbon Building Design Guidelines V4
BassemOsman1
 
PDF
Zero Carbon Building Performance standard
BassemOsman1
 
PPTX
Inventory management chapter in automation and robotics.
atisht0104
 
PPTX
Tunnel Ventilation System in Kanpur Metro
220105053
 
Chad Ayach - A Versatile Aerospace Professional
Chad Ayach
 
Introduction to Ship Engine Room Systems.pdf
Mahmoud Moghtaderi
 
22PCOAM21 Session 2 Understanding Data Source.pptx
Guru Nanak Technical Institutions
 
IoT_Smart_Agriculture_Presentations.pptx
poojakumari696707
 
AI-Driven IoT-Enabled UAV Inspection Framework for Predictive Maintenance and...
ijcncjournal019
 
Information Retrieval and Extraction - Module 7
premSankar19
 
EVS+PRESENTATIONS EVS+PRESENTATIONS like
saiyedaqib429
 
database slide on modern techniques for optimizing database queries.pptx
aky52024
 
MSME 4.0 Template idea hackathon pdf to understand
alaudeenaarish
 
Chapter_Seven_Construction_Reliability_Elective_III_Msc CM
SubashKumarBhattarai
 
MULTI LEVEL DATA TRACKING USING COOJA.pptx
dollysharma12ab
 
business incubation centre aaaaaaaaaaaaaa
hodeeesite4
 
Construction of a Thermal Vacuum Chamber for Environment Test of Triple CubeS...
2208441
 
CAD-CAM U-1 Combined Notes_57761226_2025_04_22_14_40.pdf
shailendrapratap2002
 
STUDY OF NOVEL CHANNEL MATERIALS USING III-V COMPOUNDS WITH VARIOUS GATE DIEL...
ijoejnl
 
MT Chapter 1.pptx- Magnetic particle testing
ABCAnyBodyCanRelax
 
Zero carbon Building Design Guidelines V4
BassemOsman1
 
Zero Carbon Building Performance standard
BassemOsman1
 
Inventory management chapter in automation and robotics.
atisht0104
 
Tunnel Ventilation System in Kanpur Metro
220105053
 
Ad

Unit-02 Selection, Mathematical Functions and loops.pptx

  • 2.  Looping Outline If statement Two way if statement Nested if statement Switch statement Conditional Expression While loop Do-while loop For loop Nested loop Break and continue statement Common mathematical expression
  • 3. 3 Control Statements  Control Statements in Java is one of the fundamentals required for Java Programming. It allows the smooth flow of a program.  Statement can simply be defined as an instruction given to the computer to perform specific operations.  A control statement in java is a statement that determines whether the other statements will be executed or not.  Control statements in Java,  If statement  If-else statement  If-else ladder statement  Switch statement
  • 4. 4 if statement  if statement tests the condition. It executes the if block if condition is true. public class IfStatementDemo{ public static void main(String[] ar) { int a = 10, b = 20; if( a < b ) { System.out.println("A is smaller than B"); } } }
  • 5. 5 if-else statement  if-else statement also tests the condition. It executes the if block if condition is true otherwise else block is executed. public class IfElseStatementDemo{ public static void main(String[] ar) { int a = 10, b = 20; if(a<b) { System.out.println("A is smaller than B"); } else { System.out.println(“A is not smaller than B"); } } }
  • 6. 6 if-else statement  if-else-if ladder statement executes one condition from multiple statements. int marks = 65; if (marks < 60) { System.out.println("fail"); } else if (marks >= 60 && marks < 80) { System.out.println("B grade"); } else if (marks >= 80 && marks < 90) { System.out.println("A grade"); } else if (marks >= 90 && marks < 100) { System.out.println("A+ grade"); } else { System.out.println("Invalid!"); }
  • 7. 7 Nested If statement  We can also use if/else if statement inside another if/else if statement, this is known as nested if statement. int username = Integer.parseInt(args[0]); int password = Integer.parseInt(args[1]); double balance = 123456.25; if(username==1234){ if(password==987654){ System.out.println("Your Balance is ="+balance); } else{ System.out.println("Password is invalid"); } } else{ System.out.println("Username is invalid"); }
  • 8. 8 switch statement  switch statement executes one statement from multiple conditions. It is like if- else-if ladder statement. public class SwitchExampleDemo { public static void main(String[] args) { int number = 20; switch (number) { case 10: System.out.println("10"); break; case 20: System.out.println("20"); break; default: System.out.println("Not 10 or 20"); } } }
  • 9. 9 Programs to perform (Conditional Statements)  Write a Java program to get a number from the user and print whether it is positive or negative.  Write a program to find maximum no from given 3 no.  The marks obtained by a student in 5 different subjects are input through the keyboard.  The student gets a division as per the following rules:  Percentage above or equals to 60-first division  Percentage between 50 to 59-second division  Percentage between 40 and 49-Third division  Percentage less than 40-fail Write a program to calculate the division obtained by the student.  Write a Java program that takes a number from the user and displays the name of the weekday accordingly (For example if user enter 1 program should return Monday) .
  • 10. 10 Looping Statement  Looping in programming languages is a feature which facilitates the execution of a set of instructions/functions repeatedly while some condition evaluates to true.  Java provides three ways for executing the loops. While all the ways provide similar basic functionality, they differ in their syntax and condition checking time.  While loop  Do-while loop  For  Foreach (will cover this after array)
  • 11. 11 While Loop  while loop is used to iterate a part of the program several times. while is entry control loop.  If the number of iteration is not fixed, it is recommended to use while loop. //code will print 1 to 9 public class WhileLoopDemo { public static void main(String[] args) { int number = 1; while(number < 10) { System.out.println(number); number++; } } }
  • 12. 12 Do-while Loop  do-while loop is executed at least once because condition is checked after loop body. //code will print 1 to 9 public class DoWhileLoopDemo { public static void main(String[] args) { int number = 1; do { System.out.println(number); number++; }while(number < 10) ; } }
  • 13. 13 For Loop  for loop is used to iterate a part of the program several times.  If the number of iteration is fixed, it is recommended to use for loop. //code will print 1 to 9 public class ForLoopDemo { public static void main(String[] args) { for(int number=1;number<10;number++) { System.out.println(number); } } }
  • 15. 15 Programs to perform (Looping Statements)  Write a program to print first n odd numbers.  Write a program to check that the given number is prime or not.  Write a program to draw given patterns,
  • 16. 16 Break statement  When a break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop. //code will print 1 to 4 followed by “After Loop” public class BreakDemo{ public static void main(String[] args) { for(int number=1;number<10;number++) { if(number==5) { break; } System.out.println(number); } System.out.println(“After Loop”); } }
  • 17. 17 Continue statement  The continue statement is used in loop control structure when you need to immediately jump to the next iteration of the loop. It can be used with for loop or while loop. //code will print 1 to 9 but not 5, followed by “After Loop” public class ContinueDemo { public static void main(String[] args) { for(int number=1;number<10;number++) { if(number==5) { continue; } System.out.println(number); } System.out.println(“After Loop”); } }
  • 18. 18 Math class  The Java Math class provides more advanced mathematical calculations other than arithmetic operator.  The java.lang.Math class contains methods which performs basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions.  All the methods of class Math are static.  Fields :  Math class comes with two important static fields  E : returns double value of Euler's number (i.e 2.718281828459045).  PI : returns double value of PI (i.e. 3.141592653589793).
  • 20. 20 Methods of class Math (Cont.)
  • 21. 21 Methods of class Math (Cont.)
  • 22. 22 Methods of class Math (Cont.)
  • 23. 23 Methods of class Math (Cont.)
  • 24. 24 Math Example public class MathDemo { public static void main(String[] args) { double sinValue = Math.sin(Math.PI / 2); double cosValue = Math.cos(Math.toRadians(80)); int randomNumber = (int)(Math.random() * 100); // values in Math class must be given in Radians // (not in degree) System.out.println("sin(90) = " + sinValue); System.out.println("cos(80) = " + cosValue); System.out.println("Random = " + randomNumber); } }