Data Types
UNIT 4: CONTROL STATEMENTS
4.1Selection Statements 4.1.1 If
4.1.2 If ..Else
4.1.3 If..Else if .. else 4.2The Switch Statement 4.3Iteration Statement
4.3.1 The while Statement 4.3.2 The do Statement 4.3.3 The for Statement 4.4The Comma Operator
4.5The break Statement 4.6The continue Statement 4.7Selection Statements
4.7.1 If 4.7.2 If ..Else
4.7.3 If..Else if .. else 4.8The Switch Statement 4.9Iteration Statement
4.9.1 The while Statement 4.9.2 The do Statement 4.9.3 The for Statement 4.10The Comma Operator 4.11The break Statement 4.12The continue Statement
4.1 Selection Statements
• A conditional statement lets us choose which statement will be executed next
• Therefore they are sometimes called selection statements
• Conditional statements give us the power to make basic decisions
• Java's conditional statements are the if statement, the if-else statement, and the switch statement
4.1.1 if
if( boolean expression ) { statement1;
...
statementn;
}
Note: Curly braces only required if there is more than one execution statement.
4.1.2 If..else if(x==y){
// do this } else { // do this }
Note: The "else" is for the last statement.
4.1.3 If..else if .. else if( x == y ) {
// do this
} else if( x > y ) { // nested 'if' // do this
// Note: any number of "else if" clauses after "if" and before "else"
} else { // do this }
Note: The "else" is for the last statement.
The output is:
x not > or = to y
4.2 The switch Statement
• The switch statement provides another means to decide which statement to execute next
• The switch statement evaluates an expression, then attempts to match the result to one of several possible cases
• Each case contains a value and a list of statements
• The flow of control transfers to statement list associated with the first value that matches
The general syntax of a switch statement is:
switch ( expression ) {
case value1 :
statement-list1; break;
case value2 :
statement-list2; break;
case value3 :
statement-list3; break;
default :
statement-list4; break;
}
• Often a break statement is used as the last statement in each case's statement list
• A break statement causes control to transfer to the end of the switch statement
• If a break statement is not used, the flow of control will continue into the next case
• A switch statement can have an optional default case
• If the default case is present, control will transfer to it if no other case value matches
• The test variable or expression to be tested can be of the primitive types byte, char, short, or int
Example :
For example, the program:
class Toomany {
static void howMany(int k) { switch (k) {
case 1: System.out.print("one ");
case 2: System.out.print("too ");
case 3: System.out.println("many");
} }
public static void main(String[] args) { howMany(3);
howMany(2);
howMany(1);
} }
contains a switch block in which the code for each case falls through into the code for the next case. As a result, the program prints:
many too many
one too many
If code is not to fall through case to case in this manner, then break statements should be used, as in this example:
class Twomany {
break; // not needed, but good style }
}
public static void main(String[] args) { howMany(1);
Java has three kinds of repetition statements: the while loop, the do loop, and the for loop
4.3.1 The while Statement
The while statement executes an Expression and a Statement repeatedly until the value of the Expression is false.
While Statement:
while (Expression) Statement
The Expression must have type boolean, or a compile-time error occurs. The while is an entry-controlled loop statement. Therefore, the body of a while loop will execute zero or more times
public class WhileTest {
public static void main(String[] args) { int r = 0;
The output will be 0
1 2
4.3.2 The do Statement
The do statement executes a Statement and an Expression repeatedly until the value of the Expression is false.
do {
statement;
}
while ( condition )
The statement is executed once initially, and then the condition is evaluated The statement is repetitively executed until the condition becomes false
• A do loop is similar to a while loop, except that the condition is evaluated after the body of the loop is executed
• Therefore the body of a do loop will execute at least one time
• The do while is an exit control loop statement.
4.3.3 The for Statement
The for statement executes some initialization code, then executes an Expression, a Statement, and some update code repeatedly until the value of the Expression is false. for ( initialization ; condition ; increment )
statement;
The increment portion is executed at the end of each iteration Example:
Example:
public class ListCharacters {
public static void main(String[] args) { for( char c = 0; c < 128; c++)
if (c != 26 ) // ANSI Clear screen
System.out.println("value: " + (int)c + " character: " + c);
} }
4.4 The comma operator
Comma operator is used to separate definitions and function arguments public class CommaOperator {
public static void main(String[] args) { for(int i = 1, j = i + 10; i < 5;i++, j = i * 2) { System.out.println("i= " + i + " j= " + j);
}}}
4.5 The break Statement
The Java break statement causes the Java interpreter to transfer the flow of execution to the end of an enclosing statement. In other words, it is used to break out of a do, for, switch, or while statement.
The syntax of the Java break statement is:
break;
Example:
for (int i=0; i < myArray.length; i++) {
if (myArray[i] == null;
{
break; // break out of for loop }
} // execution resumes at following statement on break
4.6 The continue statement
A Java continue statement is used to terminate the current iteration of a loop and continue with the next iteration. A continue statement can only be used within a do, for, or while loop.
The syntax of the Java continue statement is:
continue;
Example:
for (int i=0; i < myArray.length; i++) {
if (myArray[i] == null;
{
continue; // skips remainder of loop and begins next iteration }
myMethod (myArray[i]); // do something with a non-null myArray[i]
}
The different types of looping statements vary in how the continue statement begins a new iteration.
• do loop: On encountering a continue statement, the Java interpreter jumps to the bottom of the do loop. Then, it evaluates the test condition to determine whether to begin a new iteration.
• for loop: On encountering a continue statement, the Java interpreter begins at the top of the loop in the for statement. First, it evaluates the update expression, e.g. i++. Then, it evaluates the test expression, e.g. i < myArray.length. The important thing is that the update expression is evaluated.
• while loop: On encountering a continue statement, the Java interpreter begins at the top of the loop in the while statement. Then, it evaluates the test condition to determine whether to begin a new iteration.