0% found this document useful (0 votes)
18 views20 pages

Control Statements

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views20 pages

Control Statements

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

JAVA Programming

Course Instructor: Dr. Suvojit Dhara


School of Computer Science
UPES Dehradun
TOPICs to be discussed

 Selection Statements Jump Statements

 If-else  break
 switch  continue
 return
Iteration Statements

 While
 do-while
 for
Let’s START …!!!
Selection Statements

 Java supports two 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 (condition) When you nest ifs, the main thing to


statement1; remember is that an else statement always
else statement2; refers to the nearest if statement that is
within the same block as the else and that is
here, each statement may be a single statement or not already associated with an else.
a compound statement enclosed in curly braces
(that is, a block). if(i == 10) {
if(j > 20) a = b;
 The condition is any expression that returns a if(k < 100) c = d; //this if is
else a = c; //associated with this
boolean value. else
 The else clause is optional. }
else a = d; //this else is for if(i == 10)
The if-else-if ladder
class IfElse{
public static void main(String[] args) {
int month = 4; //April
String season;

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";

System.out.println("April is in the "+season+".");


}
}

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

 Java’s iteration statements are

 while
 do-while
 for

 These statements create what we commonly call loops.


‘while’ loop
 It repeats a statement or block while its class While {
public static void main(String[] args) {
controlling expression is true. Here is its int n = 5;
general form:
while(n > 0) {
while(condition) { System.out.println("tick "+n);
// body of loop n--;
} }
}
 The condition can be any Boolean }
expression.
 The body of the loop will be executed as long
Output: tick 5
as the conditional expression is true. When
condition becomes false, control passes to the
tick 4
next line of code immediately following the
loop.
 The curly braces are unnecessary if only a tick 3
single statement is being repeated.
tick 2
‘do-while’ loop
class DoWhile {
 The ‘do-while’ loop always executes its public static void main(String[] args) {
int n = 5;
body at least once, because its conditional
expression is at the bottom of the loop. Its do {
System.out.println("tick "+n);
general form is n--;
} while(n > 0);
do { }
// body of loop }
} while (condition);
 Each iteration of the do-while loop first
Output: tick 5
executes the body of the loop and then
evaluates the conditional expression.
 If this expression is true, the loop will tick 4
repeat. Otherwise, the loop terminates.
tick 3

tick 2
‘while’ vs ‘do-while’
class While {
public static void main(String[] args) {
int n = 5;

while(n > 5) { Output:


System.out.println("tick "+n);
n--;
}
}
}

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

 Java supports three jump statements:

 break
 continue
 return.

 These statements transfer control to another part of your program.


‘break’ statement
class BreakLoop {
By using break, you can force public static void main(String[] args) {
for(int i=1; i<50; i++){
immediate termination of a loop,
System.out.println("i: "+i);
bypassing the conditional expression and if(i==5) break;
any remaining code in the body of the }
loop. System.out.println("The loop is broken");
}
}
When a break statement is
encountered inside a loop, the loop is Output:
terminated and program control resumes i: 1
at the next statement following the loop. i: 2
i: 3
i: 4
i: 5
The loop is
broken
‘break’ as the form of ‘Goto’
 The general form of the labeled break statement is shown here:
break label;
 label is the name of a label that identifies a block of code.

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

Today, we learned about

• if-else structure, switch-case blocks

• while, do-while, for, enhanced for loops

• break, continue, return statements

You might also like