Control Statements in java

Java’s program control statements can be put into the following categories: selection, iteration, and jump. Selection statements allow your program to choose different paths of execution based upon the outcome of an expression or the state of a variable.

Iteration statements enable program execution to repeat one or more statements (that is, iteration statements form loops). Jump statements allow your program to execute in a nonlinear fashion

Java’s Selection Statements

Java supports two selection statements: if and switch. These statements allow you to control the flow of your program’s execution based upon conditions known only during run time

if

The if statement is Java’s conditional branch statement. It can be used to route program execution through two different paths.

if (condition) statement1;
else statement2;

int a, b;
if(a < b) a = 0;
else b = 0;

Nested ifs

A nested if is an if statement that is the target of another if or else. Nested ifs are very common in programming. When you nest ifs, the main thing to remember is that an else statement always refers to the nearest if statement that is within the same block as the else and that is not already associated with an else

if(i == 10) {
if(j < 20) a = b;
if(k > 100) c = d; 
else a = c;
}
else a = d;

The if-else-if Ladder

if(condition)
statement;
else if(condition)
statement;
else if(condition)
statement;
.
.
.
else
statement;

Demonstrate if-else-if statements

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 + ".");
}
}

switch

The switch statement is Java’s multiway branch statement. It provides an easy way to dispatch execution to different parts of your code based on the value of an expression. As such, it often provides a better alternative than a large series of if-else-if statements.

switch (expression) {
case value1:
// statement sequence
break;
case value2:
// statement sequence
break;
.
.
.
// statement sequence
break;
default:
// default statement sequence
}

The expression must be of type byte, short, int, or char; each of the values specified in the case statements must be of a type compatible with the expression

Each case value must be a unique literal (that is, it must be a constant, not a variable). Duplicate case values are not allowed

An enumeration value can also be used to control a switch statement.

The switch statement works like this: The value of the expression is compared with each of the literal values in the case statements. If a match is found, the code sequence following that case statement is executed. If none of the constants matches the value of the expression, then the default statement is executed. However, the default statement is optional. If no case matches and no default is present, then no further action is taken.

The break statement is used inside the switch to terminate a statement sequence. When a break statement is encountered, execution branches to the first line of code that follows the entire switch statement. This has the effect of “jumping out” of the switch

A simple example of the switch

class SampleSwitch {
public static void main(String args[]) {
for(int i=0; i<6; 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;
default:
System.out.println("i is greater than 3.");
}
}
}

Output

i is zero.
i is one.
i is two.
i is three.
i is greater than 3.
i is greater than 3.

Leave a Comment