Control-Statements-In-Java
Control-Statements-In-Java
10/30/2010
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.
2
SELECTION STATEMENTS
10/30/2010
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.
3
10/30/2010 Control Statements
4
Principal forms:
The if and if-else Statements (cont.)
10/30/2010
Additional forms
Control Statements
5
Flowchart For The IF And IF-ELSE Statements
10/30/2010
Control Statements
6
EXAMPLE OF IF-ELSE EXAMPLE OF NESTED IF
10/30/2010
if( a > b) class Example4_2
{ {
public static void main(String Args[])
System.out.println("A = " + a + "\tB = "
Control Statements
{
+ b); int a = 3;
System.out.println("A is greater than B"); if (a <= 10 && a > 0)
} {
System.out.println("Number is valid.");
else
if ( a < 5)
{ System.out.println("From 1 to 5");
System.out.println("A = " + a + "\tB = " + else
b); System.out.println("From 5 to 10");
}
System.out.println("Either both are equal
else
or B is greater");
System.out.println("Number is not valid");
} }
}
7
10/30/2010 Control Statements
8
Else-if ladder
Example of else-if ladder
10/30/2010
class Example4_1{
public static void main (String Args[]){
Control Statements
int a = 5;
boolean val = false;
if(val)
System.out.println("val is false, so it won't execute";
else if (a < 0 )
System.out.println("A is a negative value");
else if (a > 0)
System.out.println ("A is a positive value");
else
System.out.println ("A is equal to zero");
} 9
}
Switch Statement
10/30/2010
The switch statement of java is another selection statement that
defines different paths of execution for a program.
Control Statements
It is more efficient that the if statement
10
The break statement is optional in the switch statement
Syntax of Switch statement
10/30/2010
Control Statements
11
Example
10/30/2010
class Example4_3{
public static void main(String Args[]){
case 6:
int month = 3; System.out.println("The month of June");
switch (month){ break;
Control Statements
case 7:
case 1: System.out.println("The month of July");
System.out.println("The month of January"); break;
case 8:
break;
System.out.println("The month of August");
case 2: break;
System.out.println("The month of February"); case 9:
System.out.println("The month of September");
break; break;
case 3: case 10:
System.out.println("The month of October");
System.out.println("The month of March"); break;
break; case 11:
System.out.println("The month of November");
case 4:
break;
System.out.println("The month of April"); case 12:
break; case 5: System.out.println("The month of December");
break;
System.out.println("The month of May"); default:
break; System.out.println("Invalid month"); 12
}
}
}
Iteration Statement
10/30/2010
It is essential that a program be able to
execute the same set of instructions
Control Statements
many times: otherwise a computer would
do only as much work as a programmer!
10/30/2010
// 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
The for Loop
10/30/2010
for is a shorthand that combines in one statement initialization,
condition, and change
Control Statements
for ( initialization; condition; change )
{
statement1;
statement2;
...
statementN;
}
16
Example
10/30/2010
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
10/30/2010 Control Statements
18
The do-while Loop
Example
10/30/2010
public class DoWhileExample
{
Control Statements
public static void main (String[ ] args)
{
int i =0;
do
{
System.out.println ("i is : " + i);
i++;
} while (i < 4);
}
} 19
Jump Statements
10/30/2010
Control Statements
Jump statements are used to unconditionally transfer the
program control to another part of the program.
Java has
three jump break, continue return.
statements:
20
10/30/2010
Break in a loop instructs the program to immediately quit the current
Control Statements
iteration and go to the first statement following the loop.
21
Example
10/30/2010
Labeled Break oUnlabeled Break
Control Statements
for(int var =0; var < 5 ; var++)
Outer:
for(int var1=0; var1 < 5 ; var1++)
{ {
System.out.println(“Var is : “ + var); for(int var2 = 1; var2 < 5;var2++)
if(var == 3) {
System.out.println(“var1:” +
break; var1 + “, var2:” + var2);
} if(var1 == 3)
break Outer;
}
}
22
10/30/2010
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 iteration
Control Statements
of the loop.
SYNTAX continue label;
23
Example
10/30/2010
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)
System.out.println(“var1:” + var1 continue;
+ “, var2:”+ var2); System.out.println(“var1:” +
} var1 + “, var2:”+ var2);
} }
}
24
10/30/2010
Return in a loop instructs the program to immediately quit the
current method and return to the calling method.
Control Statements
Example
class Return
{
public static void main(String args[])
{
boolean t = true;
System.out.println("Before the return.");
if(t) return; // return to caller
System.out.println("This won't execute.");
} 25
}