01/10/26 Control Statements
1
01/10/26
Control Statements
Control statements are used in programming languages to cause the
flow of control to advance and branch based on changes to the state
of a program.
In Java, control statements can be divided under the following three
categories:
Selection statements
Iteration statements
Jump statements
2
01/10/26
Selection statements are used in a program to choose different paths
of execution based upon the outcome of an expression or the state of
Control Statements
a variable.
Using if and if...else
Nested if Statements
Using switch Statements
Conditional Operator
3
01/10/26 Control Statements
4
Principal forms:
01/10/26 Control Statements
5
Additional forms
01/10/26 Control Statements
6
01/10/26
if( a > b) class Example4_2
{ {
public static void main(String Args[])
[Link]("A = " + a + "\tB = " + {
Control Statements
b); int a = 3;
[Link]("A is greater than B"); if (a <= 10 && a > 0)
} {
[Link]("Number is valid.");
else
if ( a < 5)
{ [Link]("From 1 to 5");
[Link]("A = " + a + "\tB = " + else
b); [Link]("From 5 to 10");
}
[Link]("Either both are equal
else
or B is greater");
[Link]("Number is not valid");
} }
}
7
01/10/26 Control Statements
8
01/10/26
class Example4_1{
public static void main (String Args[]){
int a = 5;
Control Statements
boolean val = false;
if(val)
[Link]("val is false, so it won't execute";
else if (a < 0 )
[Link]("A is a negative value");
else if (a > 0)
[Link] ("A is a positive value");
else
[Link] ("A is equal to zero");
} 9
}
01/10/26 Control Statements
10
01/10/26 Control Statements
11
01/10/26
class Example4_3{
public static void main(String Args[]){
case 6:
int month = 3; [Link]("The month of June");
switch (month){ break;
Control Statements
case 7:
case 1: [Link]("The month of July");
[Link]("The month of January"); break;
case 8:
break;
[Link]("The month of August");
case 2: break;
[Link]("The month of February"); case 9:
[Link]("The month of September");
break; break;
case 3: case 10:
[Link]("The month of October");
[Link]("The month of March"); break;
break; case 11:
[Link]("The month of November");
case 4:
break;
[Link]("The month of April"); case 12:
break; case 5: [Link]("The month of December");
break;
[Link]("The month of May"); default:
break; [Link]("Invalid month"); 12
}
}
}
01/10/26 Control Statements
13
01/10/26 Control Statements
14
01/10/26
// Returns the smallest n
Control Statements
// such that 2^n >= x
public static int intLog2 (int x)
{ Initialization
int n = 0, p = 1;
while ( p < x ) Testing
{
p *= 2; Change
n++;
}
return n;
} 15
01/10/26
for is a shorthand that combines in one statement initialization,
condition, and change
Control Statements
for ( initialization; condition; change )
{
statement1;
statement2;
...
statementN;
}
16
01/10/26
Control Statements
// Returns the smallest n
// such that 2^n >= x
public static int intLog2 (int x)
{ Initialization
int n = 0, p;
for (p = 1; p < x; p *= 2) Testing
{
n++; Change
}
return n;
}
17
01/10/26 Control Statements
18
01/10/26
public class DoWhileExample
{
Control Statements
public static void main (String[ ] args)
{
int i =0;
do
{
[Link] ("i is : " + i);
i++;
} while (i < 4);
}
} 19
01/10/26 Control Statements
20
01/10/26
Break in a loop instructs the program to immediately quit the
Control Statements
current iteration and go to the first statement following the loop.
SYNTAX break label;
Break statement has two forms:
Labeled Break statement
Unlabeled Break statement
21
01/10/26
Labeled Break oUnlabeled Break
Control Statements
for(int var =0; var < 5 ; var++) Outer:
for(int var1=0; var1 < 5 ; var1++)
{ {
[Link](“Var is : “ + var); for(int var2 = 1; var2 < 5;var2++)
if(var == 3) {
[Link](“var1:” +
break; var1 + “, var2:” + var2);
} if(var1 == 3)
break Outer;
}
}
22
01/10/26
Continue statement is used when we want to skip the rest of the
statement in the body of the loop and continue with the next
Control Statements
iteration of the loop.
SYNTAX continue label;
There are two forms of continue statement in Java.
Unlabeled Continue Statement
Labeled Continue Statement
23
01/10/26
o Labeled Continue oUnlabeled Continue
Control Statements
Outer:
for(int var1 =0; var1 < 5 ; var1++)
{ for(int var1 =0; var1 < 5 ; var1++)
for(int var2=0 ; var2 < 5 ; var2++) {
{ for(int var2=0 ; var2 < 5 ; var2++)
if(var2 == 2) {
continue Outer; if(var2 == 2)
[Link](“var1:” + var1 continue;
+ “, var2:”+ var2); [Link](“var1:” +
} var1 + “, var2:”+ var2);
} }
}
24
01/10/26
Return in a loop instructs the program to immediately quit the
current method and return to the calling method.
Example
Control Statements
class Return
{
public static void main(String args[])
{
boolean t = true;
[Link]("Before the return.");
if(t) return; // return to caller
[Link]("This won't execute.");
} 25
}