Control Statements
Control Statements
If-else break
switch continue
return
Iteration Statements
While
do-while
for
Let’s START …!!!
Selection Statements
if-else
switch.
These statements allow you to control the flow of your program’s execution based upon
conditions known only during run time.
‘if’ statement
The if statement is Java’s conditional branch Nested ifs
statement. A nested if is an if statement that is the target
The general form of the if statement: of another if or else.
if((month==12)||(month==1)||(month==2))
season = "Winter";
else if((month==3)||(month==4)||(month==5))
season = "Spring";
else if((month==6)||(month==7)||(month==8))
season = "Summer";
else if((month==9)||(month==10)||(month==11))
season = "Autumn";
else
season = "Bogus month";
Output:
April is in the Spring.
‘switch’ statement
The switch statement is Java’s class sample_switch{
public static void main(String[] args) {
multiway branch statement. int marks = 82;
It provides an easy way to dispatch switch(marks/20) {
case(4):
execution to different parts of your System.out.println("grade: "+'A');
code based on the value of an break;
case(3):
expression. System.out.println("grade: "+'B');
break;
switch(expression) { case(2):
case value1:
System.out.println("grade: "+'C');
//statement sequence
break;
break;
case value2: default:
//statement sequence System.out.println("grade: "+'D’);
break; }
. }
. }
case valueN:
//statement sequence
break;
Output:
default:
//statement sequence
} grade: A
Iteration Statements
while
do-while
for
tick 2
‘while’ vs ‘do-while’
class While {
public static void main(String[] args) {
int n = 5;
class DoWhile {
public static void main(String[] args) {
int n = 5;
Output:
do {
System.out.println("tick "+n); tick 5
n--;
} while(n > 5);
}
}
‘for’ loop
The general form of the traditional for statement: class For {
public static void main(String[] args) {
for(initialization; condition; iteration){ int n;
// body of loop
} for(n=5; n>0; n--) {
If only one statement is being repeated, there is no need System.out.println("tick "+n);
}
for the curly braces. }
When the loop first starts, the initialization portion of }
the loop is executed. Generally, this is an expression
that sets the value of the loop control variable, which
acts as a counter that controls the loop. Output: tick 5
Next, condition is evaluated. This must be a Boolean
expression. It usually tests the loop control variable tick 4
against a target value. If this expression is true, then the
body of the loop is executed. If it is false, the loop
tick 3
terminates.
Next, the iteration portion of the loop is executed. This
is usually an expression that increments or decrements tick 2
the loop control variable.
Extended ‘for’ loop
Java adds the for-each capability by enhancing the class ForEach {
public static void main(String[] args) {
for statement. The for-each style of for is also referred int nums[] = {1, 2, 3, 4, 5};
to as the enhanced for loop. int sum = 0;
The advantage of this approach is that no new
keyword is required, and no preexisting code is for(int n: nums) {
System.out.println("Value: "+n);
broken. sum += n;
The general form of the for-each version of the for is: }
System.out.println("Summation: "+sum);
for(type itr-var : collection) { }
// body of loop }
}
here, type specifies the type and itr-var specifies the Output: Value: 1
name of an iteration variable that will receive the
elements from a collection, one at a time, from
Value: 2
beginning to end.
With each iteration of the loop, the next element in the
collection is retrieved and stored in itr-var. The loop Value: 3
repeats until all elements in the collection have been
obtained. Value: 4
Jump Statements
break
continue
return.
class BreakGoto {
public static void main(String[] args) {
boolean t = true;
first: {
second: {
third: {
System.out.println("Before the break.");
if(t) break second;
System.out.println("This won’t execute."); Output:
}
System.out.println("This won’t execute too.");
} Before the break.
System.out.println("This is after the second block."); This is after the second block.
}
}
}
‘continue’ statement
A continue statement causes control to be transferred directly to the conditional expression
that controls the loop. class ContinueLabel {
public static void main(String[] args) {
class Continue { outer: for(int i=0; i<5; i++) {
public static void main(String[] args) { for(int j=0; j<5; j++) {
for(int i=0; i<10; i++) { if(j>i) {
System.out.print(i+" "); System.out.println();
if(i%2 == 0) continue; continue outer;
System.out.println(""); }
} System.out.print(" "+(i*j));
} }
} }
}
}
Output: 0 1
Output: 0
2 3
0 1
4 5
0 2 4
6 7
0 3 6 9
8 9
0 4 8 12 16
‘return’ statement
The return statement is used to explicitly return from a method.
It causes program control to transfer back to the caller of the method.
At any time in a method the return statement can be used to cause execution to branch back
to the caller of the method. Thus, the return statement immediately terminates the method in
which it is executed.
class Return {
public static void main(String[] args) {
boolean t = true;
System.out.println("Before the return statement");
if(t) return; Output:
System.out.println("This won’t execute.");
}
} Before the return statement
Summary