4 - Control Statements
4 - Control Statements
STATEMENTS
Java’s program control statements can be categorized as 2
selection,
iteration,
jump.
if
• The if statement is Java’s conditional branch statement.
variable.
boolean dataAvailable;
if (dataAvailable)
processdata();
else
waitformoredata();
int bytesAvailable;
if (bytesAvailable > 0){
processdata();
bytesAvailable -=n;
}else
waitformoredata();
Nested ifs… 6
if(condition)
statement;
else if (condition )
statement;
else if(condition)
statement;
.
.
.
else
statement;
// Demonstrate if-else-if statements.
class IfElse { 8
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 + ".");
}
}
Switch…. 9
switch(expression) {
case constant1:
statement sequence
break;
case constant2:
statement sequence
break;
...
default:
statement sequence
}
1
1
// Demonstrate the switch.
class SwitchDemo {
public static void main(String args[]) { 12
int i;
for(i=0; i<10; i++)
switch(i) {
case 0: System.out.println("i is zero");
break;
case 1: System.out.println("i is one");
break;
case 2: System.out.println("i is two");
break;
case 3: System.out.println("i is three");
break;
case 4: System.out.println("i is four");
break;
default: System.out.println("i is five or more");
}
}
}
class Switch { case 6:
public static void case 7: 1
main(String args[]) { case 8: 3
int month = 4; season = "Summer";
break;
String season;
case 9:
switch (month) { case 10:
case 12: case 11:
case 1: season = "Autumn";
case 2: break;
season = "Winter"; default:
break; season = “Invalid
case 3: Month";
case 4: }
case 5: System.out.println("April
is in the " + season +
season = "Spring";
".");
break; }}
Nested switch Statements…..
14
• It is possible to have a switch as part of the statement
sequence of an outer switch.
• This is called a nested switch. Even if the case constants
of the inner and outer switch contain common values, no
conflicts will arise.
switch(ch1) {
case 'A': System.out.println("This A is part of outer switch.");
switch(ch2) {
case 'A': System.out.println("This A is part of inner switch");
break;
case 'B': // ...
} // end of inner switch
break;
case 'B': // ...
Iteration Statements….
15
• Java’s iteration statement are for, while & do-while.
• These statements create loops, a loop repeatedly executes
the same set of instructions until a termination condition
is met.
While 16
while(condition){
....// body of loop
}
then executing the body of the loop, and then executing the iteration
expression with each pass.
•This process repeats until the controlling expression is false.
Declaring loop control variables inside the for loop..
for(int i=10, i>0; i++)
Using the comma….
Include more than one statement in the initialization
and iteration portions of the loop.
for(a=1, b=4; a<b; a++, b--)
For infinite loop
for(; ;) 20
{
…//body
}
•Can create infinite loop if you leave all parts empty.
•This loop will run forever there is no condition under
which it will terminate
The For-Each version of the for loop….
21
• The for-each version of the for is also refereed to as the
enhanced for loop.
• Syntax:
• for(type itr-var : collection) statement-block
• type specifies the data type.
• itr-var specifies the name of an iteration variable will
receive the elements from a collection. one at a time, from
beginning to end.
• The collection being cycled through is specified by
collection.
• There are various types of collections that can be used
with the for, ex. is array. 22
int nums[]={1,2,3,4,5,6,7,8,9,10};
int sum=0;
for(int x: nums) sum=sum+x;
• As the iteration variable receives values from the
collection, type must be the same as the elements stored in
the collection.
Consider an example
int nums[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; 2
int sum = 0; 3
for(int i=0; i < 10; i++)
sum += nums[i];
The above set of statements can be optimized as follows
int nums[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int sum = 0;
for(int x: nums)
•With each pass through the loop, x is automatically given a value
equal to the next element in nums.
•Thus, on the first iteration, x contains 1; on the second iteration, x
contains 2; and so on.
•Not only is the syntax streamlined, but it also prevents boundary
class ForEach
{ 24
Using break….
• The java break statement has three uses.
1. Terminates a statement in a switch.
2. Used to exit a loop.
3. Used as a civilized form of goto.
Using break to Exit a loop…
30