Control Statements
Control Statements
Control Statements
CONTROL STATEMENTS
• FLOW OF CONTROL : CONTROL THE ORDER OF EXECUTION
• CONTROL STRUCTURE : STATEMENT WHICH USED TO ALTER THE
NORMAL SEQUENTIAL FLOW
CATEGORIES OF CONTROL
STATEMENTS
• SELECTION OR DECISION MAKING STATEMENT :
1. if statement
2. switch statement
• ITERATION OR LOOPING STATEMENT
1. for loop
2. while loop
3. do-while loop
• JUMP STATEMENT : break,continue,return
The if and if-else Statements
(cont.)
• Principal forms:
5 Fundamentals of Java
The if and if-else Statements
(cont.)
• Additional forms:
6 Fundamentals of Java
The if and if-else Statements
(cont.)
• Better to over-use braces than under-use them
• Can help to eliminate logic errors
• Condition of an if statement must be a Boolean
expression
• Evaluates to true or false
• A flowchart can be used to illustrate the behavior
of if-else statements.
Fundamentals of Java 7
The if and if-else Statements
(cont.)
• Examples of if statements:
8 Fundamentals of Java
NESTED – IF STATEMENT
If (expression 1)
{
if (expression 2)
statement 1;
else
statement 2;
}
Else
{
if (expression 3)
statement 3;
else
statement 4;
}
If (expression 1)
{
statement 1;
}
Else if (expression 2)
{
statement 2;
}
Else if (expression 3)
{
statement 3;
}
The while Statement
11 Fundamentals of Java
The while Statement (cont.)
Fundamentals of Java 12
The while Statement (cont.)
13 Fundamentals of Java
The while Statement (cont.)
• Example:
Counter-controlled loop:
– cntr is the counter
– Loop repeats a determined number of times
14 Fundamentals of Java
The while Statement (cont.)
• Counting backward:
15 Fundamentals of Java
The while Statement (cont.)
16 Fundamentals of Java
The Do/While Loop
A do...while loop is similar to a while loop, except that a do...while
loop is guaranteed to execute at least one time.
do
{ // code block to be executed }
while (condition);
int i = 0;
int i = 0; while (i < 5)
do {
{ System.out.println(i);
System.out.println(i); }
i++; i++;
} while (i < 5);
Java Switch Statement
• The Java 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.
• There are three types of for loops in java.
1. Simple For Loop
2. For-each or Enhanced For Loop
3. Labeled For Loop
Java Simple For Loop
• A simple for loop is the same as C/C++. We can initialize the variable,
check condition and increment/decrement value.
for(initialization;condition;incr/decr){
//statement or code to be executed
}
EXAMPLE
public class ForExample {
public static void main(String[] args) {
//Code of Java for loop
for(int i=1;i<=10;i++){
System.out.println(i);
}
}
}
Java Nested For Loop
public class NestedForExample {
public static void main(String[] args) {
//loop of i
for(int i=1;i<=3;i++){
//loop of j
for(int j=1;j<=3;j++){
System.out.println(i+" "+j);
}//end of i
}//end of j
}
}
Java for-each Loop
labelname:
for(initialization;condition;incr/decr){
//code to be executed
}
EXAMPLE
public class LabeledForExample {
public static void main(String[] args) {
aa: //Using Label for outer and for loop
for(int i=1;i<=3;i++){
bb:
for(int j=1;j<=3;j++){
if(i==2&&j==2){
break aa;
}
System.out.println(i+" "+j);
}
}
}
}
Java Infinitive For Loop
for(;;){
//code to be executed
}
BREAK STATMENT
• The break statement in Java programming language has the following
two usages −
• When the break statement is encountered inside a loop, the loop is
immediately terminated and the program control resumes at the next
statement following the loop.
• It can be used to terminate a case in the switch statement
EXAMPLE
break;
}
System.out.print( x );
System.out.print("\n");
} } }
CONTINUE STATMENT
public class Test {
public static void main(String args[])
{
int [] numbers = {10, 20, 30, 40, 50};
for(int x : numbers )
{
if( x == 30 )
{
continue;
}
System.out.print( x );
System.out.print("\n");
} } }