Flow Control Statements
Flow control statements are used to control the flow of program execution.
1. Conditional Statements
Conditional statements allow the execution of certain parts of the code
based on specific conditions.
a = 20; // Assignment stmt
1. Conditional Statements
Conditional statements allow the execution of certain parts of the code
based on specific conditions.
ex:
if() cond stmt
Switch - condi stmt
2. Loop Statements:
for Loop
- Used
to execute a block of code a certain number of times.
while Loop
- Used to
execute a block of code while a condition is true.
do while Loop
- Used
to execute a block of code once, and then repeatedly execute it while a
condition is true.
1. Conditional Statements:
if() Conditional Statement
Syntax:
if (cond) note : there is no ;
at the end of if stmt
{ // if block
/ beginning of if block
stmt2; //
gets executed if cond is true only
stm3;
} // end of if block
stmt4;
· If cond is true, it executes stmt2 and stmt3.
· If cond is
false, it won't enter the if block and will execute all statements after the if
block, i.e., stmt4.
Block:
- A collection of statements
enclosed in curly braces.
ex:
{ // if block
/ beginning of 'if' block
stmt2;
stm3;
} // end of if block
· 'if' is a predefined word (or keyword) in Java.
· It is used
to write conditional statements.
· There is no ; at the end of the if
statement.
· Do not use ; at the end of the if
statement.
· if(cond) ; is an invalid statement.
Ex1: WAP to check given number is
greater than 10
package ConditionnalFlowBasics;
public class IfBasics1 {
public
static void main(String[] args) {
//Ex1:
Checking if a number is greater than 10
// number > 10
int
number = 12;
if(number>10)
{
// 12 > 10 // beginning of if block
// true -- it enters if block
System.out.println("Number
is > 10");
}
// end of if block
System.out.println("after
if bock");
}
}
o/p:
Ex2: WAP to check given number is Less than 10
package ConditionnalFlowBasics;
public class IfBasics1 {
public
static void main(String[] args) {
//Ex2:
Checking if a number is Less than 10
// number < 10
int
number = 5;
if(number
< 10)
{// 5
<10 -- > true -- it enters if block
System.out.println("Number
< 10");
}
System.out.println("after
if block");
}
}
o/p:
Number < 10
after if block
ex3: Write o/p for below
program?
package IFCondStmts;
public class ifbasics1 {
public
static void main(String[] args) {
System.out.println("stmt-1");
if(false)
{
System.out.println("stmt-2");
System.out.println("stmt-3");
}
System.out.println("stmt-4");
}
}
o/p:
stmt-1
stmt-4
2. if-else Statement
Syntax:
stmt1;
if(cond)
{
stmt -2; // gets executed if cond
is true only
stmt -3;
}
else //
else - k/w in java - can be used to define else block
{
stmt -4;
stmt -5;
}
// end of if block
stmt6;// always gets executed if
condition - true/ false
ex: Write o/p for below program?
package IFCondStmts;
public class ifbasics1 {
public
static void main(String[] args) {
System.out.println("stmt-1");
if(true)
{
System.out.println("stmt-2");//
true
System.out.println("stmt-3");
}
else // if
con- is false - else block gets executed
{
System.out.println("stmt-4");
System.out.println("stmt-5");
}
System.out.println("stmt-6");
}
}
o/p:
stmt-1
stmt-2
stmt-3
stmt-6
ex: Write o/p for below program?
package IFCondStmts;
public class ifbasics1 {
public
static void main(String[] args) {
System.out.println("stmt-1");
if(false)
{
System.out.println("stmt-2");//
true
System.out.println("stmt-3");
}
else // if
con- is false - else block gets executed
{
System.out.println("stmt-4");
System.out.println("stmt-5");
}
System.out.println("stmt-6");
}
}
o/p:
stmt-1
stmt-4
stmt-5
stmt-6
ex: write a program to check the
given Age is greater than 18.
If age is > 18, display msg
“age is >18”
Else display msg “age is not
>18 ”
package ConditionnalFlowBasics;
public class
CheckAgeIsGreaterThan18 {
public static void main(String[] args) {
// Example: Write a program to check if
the given age is greater than 18.
// If age is > 18, display msg
"age is >18"
// Else display msg "age is not
>18"
int age = 20;
if (age > 18) { // 20 > 18 is
true, so it enters the if block
System.out.println("age is
>18");
} else {
System.out.println("age is not
>18");
}
}
}
o/p:
age is >18
-ve :
package ConditionnalFlowBasics;
public class
CheckAgeISGreaterThan18 {
public
static void main(String[] args) {
// ex: write a program to check the given Age is
greater than 18.
// If age is > 18, display msg “age is
>18”
// Else display msg “age is not >18
// int age
= 20;
int
age =
10;// -ve
if(age
> 18)
{//
10 >18 false -- wont enters if block,, ctrl will go
Else part
System.out.println("age
is >18");
}
else
{
System.out.println("age
is not >18");
}
}
}
o/p:
age is not >18
ex: program to check given number is even number.
If it is even no, display “Given
no is even no”
Else display msg “Given no is not
even no”
package ConditionnalFlowBasics;
public class EvenNo {
public
static void main(String[] args) {
// ex:
program to check given number is even number.
// If it is even no, display “Given no is
even no”
// Else display msg “Given no is not even no”
int
no = 4;
//
Even no = check no is divisible by 2 =
if we get remainder ==0 - even
// else
not even no
if(no
% 2 == 0)
{
// 4 %2
rem = 0
// 0 == 0 true - enters if block
System.out.println("It is Even no");
}
else
{
System.out.println("It is not
Even no");
}
}
}
o/p:
-ve :
package ConditionnalFlowBasics;
public class EvenNo {
public
static void main(String[] args) {
// // TODO Auto-generated method stub
// ex:
program to check given number is even number.
// If it is even no, display “Given no is
even no”
// Else display msg “Given no is not even no”
// int no = 4;
int
no = 5;
//
Even no = check no is divisible by 2 =
if we get remainder ==0 - even
// else
not even no
if(no
% 2 == 0)
{
// 5 % 2
// 1 == 0 false - does not enters if block,
ctrl will go to else block
System.out.println("It is Even no");
}
else
{
System.out.println("It is not
Even no");
}
}
}
HW program to check given number
is odd number
If it is even no, display msg “Given no is odd no”
Else display msg “Given no is not odd no"
HW -ve check 4 is not odd no
HW Write a program to check if
the student's marks are greater than or equal to 35. If marks are greater than
or equal to 35, display the message "student passed". Else, display
the message "student failed"
HW Write a program to check if
the given salary is greater than or equal to 50,000. If the salary is greater
than or equal to 50,000, display the message "Salary is >=
50,000". Else, display the message "Salary is not >= 50,000".
HW Write a program to check if
the given salary is less than or equal to 50,000. If the salary is less than or
equal to 50,000, display the message "Salary <= 50,000". Else,
display the message "Salary is not <= 50,000".
HW Write a program to check if
the given salary is equal to 10,000. If the salary is equal to 10,000, display
the message "esal=10000". Else, display the message "esal is not
equal to 10000".
WAP to increment salary by 10000 for eid =10;
package ConditionnalFlowBasics;
public class IncrementSal {
public
static void main(String[] args) {
//
// WAP to increment salary by 10000 for eid =10;
int
eid =10;
// int eid =20;
int
empsal = 50000;
if(eid
== 10)
{
// 10 == 10 true - it enters if block
//
20 == 10 false, it enters else block
// increment salary by 10000
System.out.println("increment
the salary by 10,000");
empsal
= empsal + 10000;
System.out.println("New
Salary ="+ empsal);
}
else
{
//
no increment
System.out.println("No
Increment as empid is not equal
10");
}
}
}
-ve :
package ConditionnalFlowBasics;
public class IncrementSal {
public
static void main(String[] args) {
//
// WAP to increment salary by 10000 for eid =10;
// int eid =10;
int
eid =20;
int
empsal = 50000;
if(eid
== 10)
{
// 10 == 10 true - it enters if block
//
20 == 10 false, it enters else block
// increment salary by 10000
System.out.println("increment
the salary by 10,000");
empsal
= empsal + 10000;
System.out.println("New
Salary ="+ empsal);
}
else
{
//
no increment
System.out.println("No
Increment as empid is not equal
10");
}
}
}
o/p:
No Increment as empid is not equal 10
HW WAP to check esal > 80000 , decrease sal by 5000
//WAP to check eid =100 and esal
=50000?
package ConditionnalFlowBasics;
public class ifWithAnd {
public
static void main(String[] args) {
// WAP to check eid
=100 and esal =50000?
int
eid =100;
int
esal = 50000;
if(eid
== 100 && esal == 50000)
{//100
== 100 && 50000 == 50000
// true
&& true
// true - it enters if block
System.out.println("True.
eid =100 and esal =50000 ");
}
else
{
System.out.println("False.
eid =100 and esal =50000");
}
}
}
-ve:
package ConditionnalFlowBasics;
public class ifWithAnd {
public
static void main(String[] args) {
// WAP to check eid
=100 and esal =50000?
int
eid =100;
// int esal = 50000;
// int eid =25;
int
esal = 7000;
if(eid
== 100 && esal == 50000)
{//100
== 100 && 7000 == 50000
// true
&& false -- && - if any one i/p is false - o/p is
also - false
// false - control goes to else part or block
System.out.println("True.
eid =100 and esal =50000 ");
}
else
{
System.out.println("False.
not eid =100 and esal =50000");
}
}
}
o/p:
False. not eid =100 and esal =50000
//HW WAP to check eid =100
or esal =50 000, display proper
msg 'eid = 100 or salary is 50000"
else display msg
"either eid is not = 100 or salary is not = 50000"
//note : use logical OR symbol
(||)
HW WAP to check no is Divisible
By 3
HW WAP to check given no divisible by 4
WAP to check given no is
PositiveNo or not
10,20 --
= + Ve numbers
a
= 10;
-1,-2,-3 -- are not Positive numbers -- -VE numbers
package ConditionnalFlowBasics;
public class PositiveNo {
public
static void main(String[] args) {
//
WAP to check given no is PositiveNo or
not
int
no = 3;// > 0 -- +ve no
//
3 >0 --> +Ve no
// -1
>0 -- not postive
// int no = -1;
if(no
> 0) // + ve no
{//
2 >0 , true, ctrl goes to if block
//
3>0, true,
//
-1 >0 - false, ctrl goes to else
block
System.out.println("Given
no ="+ no+ " is +Ve no");
// 3 is +ve no
}
else
{ // -ve no
System.out.println("Given
no ="+ no+ " is not +Ve no");
// Given no = -1
is not +Ve no
}
}
}
3. if else if else if else if () -Multiple else if cond
Syntax:
stmt-1;
if(cond)
{
stmts-2;
stmt-3;
}
else if(cond-2)
{
stmt-4;
stmt-5;
}
else if(cond-3)
{
stmt-6;
stmt-7;
}
stmt-8;
ex: o/p for below program?
package IFCondStmts;
public class ifbasics1 {
public
static void main(String[] args) {
System.out.println("stmt-1");
if(true)
{
System.out.println("stmt-2");// gtes executed if cond - true
System.out.println("stmt-3");
}
else
if(true) // if con- is false - else block gets executed
{
System.out.println("stmt-4");
System.out.println("stmt-5");
}
else
if(true)
{
System.out.println("stmt-6");
System.out.println("stmt-7");
}
System.out.println("stmt-8");
}
}
o/p:
stmt-1
stmt-2
stmt-3
stmt-8
Write o/p for below program?
package IFCondStmts;
public class ifbasics1 {
public
static void main(String[] args) {
System.out.println("stmt-1");
if(false)
{
System.out.println("stmt-2");// gtes executed if cond - true
System.out.println("stmt-3");
}
else
if(true) // if con- is false - else block gets executed
{
System.out.println("stmt-4");
System.out.println("stmt-5");
}
else
if(true)
{
System.out.println("stmt-6");
System.out.println("stmt-7");
}
System.out.println("stmt-8");
}
}
o/p:
stmt-1
stmt-4
stmt-5
stmt-8
Write o/p for below program?
package IFCondStmts;
public class ifbasics1 {
public
static void main(String[] args) {
System.out.println("stmt-1");
if(false)
{
System.out.println("stmt-2");// gtes executed if cond - true
System.out.println("stmt-3");
}
else
if(false) // if con- is false - else block gets executed
{
System.out.println("stmt-4");
System.out.println("stmt-5");
}
else
if(false)
{
System.out.println("stmt-6");
System.out.println("stmt-7");
}
System.out.println("stmt-8");
}
}
o/p:
stmt-1
stmt-8
if all conditions are false
-- cond-1, cond-2,cond-3 , it will go to out of if block and executes stmts after if block.
Write a program to determine the
class of a student based on their marks. The classification is as follows:
- Marks > 85 and marks <= 100: "1st
Class"
- Marks > 60 and marks <= 85: "2nd
Class"
- Marks >= 35 and marks <= 60: "3rd
Class"
- Otherwise: "Fail"
package ConditionnalFlowBasics;
public class ifelseIF {
public
static void main(String[] args) {
// marks > 85
and marks<=100 -->
1st class
// marks > 60 and marks <=85 -->
2nd class
// marks >=35 and marks <=60 -->
3rd class
// else -->
Fail
// int marks = 90;
// int marks = 70;
// int marks = 50;
int
marks = 12;
if(marks>
85 && marks <=100)
{// 50 >85
&& 90 <=100
// false
&& true
// false -- ctrl goes to else if
System.out.println("Student
got 1st class");
}
else
if(marks>60 && marks <=85)
{
// 50>60 70 <=85
// false
&& true
// false
- ctrl goes to if block
System.out.println("Student
got 2nd class");
}
else
if(marks >=35 && marks <=60)
{// 50 >= 35 &&
50 <= 60
// trueb
&& true
// true
System.out.println("Student
got 3rd class");
}
else
{
System.out.println("Student
failed as his/her marks < 35");
}
}
}
Nested if:
Writing an if conditional
statement within another if
conditional statement is called a nested if
statement.
Syntax:
if(cond-1) // outer if condition
{
//
if(cond-2)// inner if condition
{
//
cond -1 is true and cond-2 is also true
}
}
else
{
if(cond-3) // cond-1 is false, cond-3 is true
{
}
}
ex1: program - to check if the age is >=18, if age >=18, check weight > 50, display msg "You are
eligible to donate blood"
package ConditionnalFlowBasics;
public class NestedIFAge {
public
static void main(String[] args) {
// ex1: program - to
check the age is >=18,
//if
age >=18, check weight > 50,
display msg "You are eligible to donate blood"
// int age
= 20;
// int weight = 55;
int
age = 10;
int
weight = 55;
if(age
>=18)
{//
10 >=18 --false - ctrl goes after if
blokc
//
System.out.println("Age
is > =18");// print
if(weight
> 50)
{// 55
> 50 - true --ctrl goes to if block
System.out.println("You
are eligible to donate blood");
}
else
{
System.out.println("You
are not eligible for donate blood bcoz ur weight is not > 50");
}
}
else
{
System.out.println("ur
age is not >=18.so You are not
eligible to donate blood");
}
}
}
1. Switch - Conditional Statements
if
, if else, multiple else if , nested if
·
Switch - is k/w in java language.
·
It executes one statement from multiple
conditions
·
The switch
statement can be used to check which condition (or case) is matched and execute
the corresponding block of code.
syntax:
----------
switch(int val/variable (or) char
val /variable (or) String val/variable (or) expresion)
{ // // begining of switch block
case
val1 :
stmt1;
break;
case val2:
stmt2;
break;
case val3:
stmt3;
break;
default :
stmt4;
} // //
end of swicth block
stmt5;
·
where there are multiple cases / options are
available - we will go for Switch statement
·
It is similar to multiple else if statements.
ex: Write a
program to check given dayNo = 1 -->
Sunday
2 --> Monday
3 --> TuesDay
package ConditionnalFlowBasics;
public class SwitchBasics1 {
public
static void main(String[] args) {
// ex:Write a program to check given dayNo = 1 --> display msg
"Sunday"
// 2 -->
display msg "Monday"
// 3 -->
display msg "TuesDay "
// else -->
display msg "No case is matched"
int
dayNo = 1;
// int dayNo = 2;
// int dayNo = 3;
// int dayNo = 9;
switch(dayNo)
{// 1 ,2,3, 9
//
beginning of switch block
case
1:
System.out.println("1 -->
Sunday");
break;
case
2:
System.out.println("2--> Monday");
break;
case
3:
System.out.println("3-->
TuesDay");
break;//
come out of swicth blokc
default
:
System.out.println("No case is
matched");
}
// end of switch block
System.out.println("after
switch block");
}
}
Explanation:
- The switch statement checks the value
of the variable day.
- Depending on the value of day, the corresponding case
block is executed.
- The break statement terminates the
switch block.
- The default case is executed if no
other case matches.
- stmt5 is always executed after
the switch block, if present.
ex2: if no case is matched
package ConditionnalFlowBasics;
public class SwitchBasics1 {
public
static void main(String[] args) {
// ex:Write a program to check given dayNo = 1 --> display msg
"Sunday"
// 2 -->
display msg "Monday"
// 3 -->
display msg "TuesDay "
// else -->
display msg "No case is matched"
// int dayNo = 1;
// int dayNo = 2;
// int dayNo = 3;
int
dayNo = 9;
switch(dayNo)
{// 1 ,2,3, 9
//
beginning of switch block
case
1:
System.out.println("1 -->
Sunday");
break;
case
2:
System.out.println("2--> Monday");
break;
case
3:
System.out.println("3-->
TuesDay");
break;//
come out of swicth blokc
default
:
System.out.println("No case is
matched");
}
// end of switch block
System.out.println("after
switch block");
}
}
o/p:
No case is matched
after switch block
Break Statement
breakis a predefined keyword in Java.- It can
be used to exit from a
switchblock,forloop,whileloop, ordo-whileloop.
Syntax:
break;
·
If there is no break statement in a case block, the program continues to
execute the subsequent case blocks even if they do not match the switch
expression.
Ex: if there is no break stmt in
case 1 : it goes to Case 2 even if it is
not matched, it executes case 2 stmts
Break, default are optional.
·
default is executed
when none of the cases match the switch expression.
·
It is typically placed at the end of the switch
block.
package ConditionnalFlowBasics;
public class SwitchBasics1 {
public
static void main(String[] args) {
// ex:Write a program to
check given dayNo = 1 --> display msg
"Sunday"
// 2
--> display msg "Monday"
// 3
--> display msg "TuesDay "
// else -->
display msg "No case is matched"
// int dayNo = 1;
// int dayNo = 2;
// int dayNo = 3;
int
dayNo = 1;
switch(dayNo)
{// 1 ,2,3, 9
//
beginning of switch block
case
1:
System.out.println("1
--> Sunday");
// break;
case
2:
System.out.println("2--> Monday");
// break;
case
3:
System.out.println("3-->
TuesDay");
// break;// come out of switch
block
default
:
System.out.println("No
case is matched");
}
// end of switch block
System.out.println("after
switch block");
}
}
o/p:
1 --> Sunday
2--> Monday
3--> TuesDay
No case is matched
after switch block
Sunday
Monday
after switch block
Note:
Duplicate case numbers are not
allowed in Switch block
ex:
case
1:
System.out.println("Sunday");
break;
case
1: // Duplicate case are not allowed
System.out.println("Sunday");
break;
package ConditionnalFlowBasics;
public class SwitchBasics1 {
public
static void main(String[] args) {
// ex:Write a program to
check given dayNo = 1 --> display msg
"Sunday"
// 2
--> display msg "Monday"
// 3
--> display msg "TuesDay "
// else -->
display msg "No case is matched"
// int dayNo = 1;
// int dayNo = 2;
// int dayNo = 3;
int
dayNo = 1;
switch(dayNo)
{// 1 ,2,3, 9
//
beginning of switch block
case
1:
System.out.println("1 -->
Sunday");
// break;
case
1: // Error: Duplicate Case
System.out.println("test
duplicate");
//
Duplicate case are not allowed. if we write duplicate cases
case
2:
System.out.println("2--> Monday");
// break;
case
3:
System.out.println("3-->
TuesDay");
// break;// come out of switch
block
default
:
System.out.println("No
case is matched");
}
// end of switch block
System.out.println("after
switch block");
}
}
Valid and Invalid Data
Types in Switch Statements:
In Java, the switch statement can be used with certain data
types, while others are not supported. Here is a list of valid and invalid data
types for switch statements:
switch(int val/variable (or) char
val /variable (or) String val/variable (or) expression)
// We
should not use float val/variable or
double
val/variable or
boolean
val/variable in Switch Condi
valid /invalid
?
ex:
switch (2) { ... } // valid
switch (intVar) { ... } // valid
switch ('A') { ... } // valid
switch (charVar) { ... } // valid
switch ("RAM") { ... }
// valid
switch (strVar) { ... } // valid
Examples of Invalid Switch Statements:
switch (2.34f) { ... } // invalid
switch (floatVar) { ... } //
invalid
switch (2.34d) { ... } // invalid
switch (doubleVar) { ... } //
invalid
switch (true) { ... } // invalid
switch (false) { ... } // invalid
switch (boolVar) { ... } //
invalid
Examples of Invalid Switch
Statements:
// Invalid examples - These will
cause compilation errors
package ConditionnalFlowBasics;
public class SwitchBasics2 {
public
static void main(String[] args) {
// check we can pass float val, double val, in switch()?
float
f = 1.0f;
double
d =
3.4;
char
ch ='A';
// switch(f) // error :Cannot
switch on a value of type float.
//Only
convertible int values, strings or enum variables are permitted
switch(d)
//
Cannot switch on a value of type double. .
//Only
convertible int values, strings or enum variables are permitted
{
case
1.0f:
System.out.println("stmt-1");
break;
case
2.0f:
System.out.println("stmt-2");
break;
default:
System.out.println("default ");
}
System.out.println("after
switch block");
}
}
ex: Switch (char val/var)
--------------------------
package ConditionnalFlowBasics;
public class
SwitchBasics3CharValue {
public
static void main(String[] args) {
float
f = 1.0f;
double
d =
3.4;
// char ch ='A';
char
ch ='B';
// switch(f) // erro
// switch(d)
switch(ch)
// 'B'
//
Cannot switch on a value of type float. .
//Only
convertible int values, strings or enum variables are permitted
{
case 'A': // not macthing
System.out.println("Apple
-stmt-1");
break;
case 'B': // matching
System.out.println("-Ball
-stmt-2");// yes
break;// can be used to come out of swicth
cond
default:
System.out.println("default ");
}
// end of swicth block
System.out.println("after
switch block");
}
}
ex: passing boolean values in
Switch ()
-----------------------------------------
package ConditionnalFlowBasics;
public class SwitchWithBoolean {
public
static void main(String[] args) {
// can we pass boolean val/variable in
switch(()?
boolean
b = true;
switch(false)
// Cannot switch on a value of type boolean. Only convertible int values,
strings or enum variables are permitted
{
case
true:
System.out.println("stmt-1");
break;
case
false:
System.out.println("stmt-2");
break;
}
}
}
--------------
Switch (String val/ var):
HW WAP "Mon" --> Monday
Tue ---> Tuesday
Note:
It is better to write 'break'
stmt in side case block. else it executes next case block also even if case val
is not matched.
Revision:
Note :
Switch() - accepts
int/char/string val/var
but not float,double, bolean val/var
- Duplicate case values are not
allowed
case 1:
case 1:
-
break;
- default : block gets executed
, if no case val is matched --(or) none
of case is matched
No comments:
Post a Comment