Unit 5 : Control Statements

3rd Semester

Java’s Control Statements

A programming language uses control statements to cause the flow of execution to advance and branch based on changes to the state of a program. Java’s program control statements can be put into the following categories: selection, iteration, and jump.

  •  Selection statements allows 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 allows program to execute in a nonlinear fashion. All of Java’s control
    statements are examined here.

Selection Statements

1. if Statement
The if statement is Java’s conditional branch statement. It can be used to route program execution through two different paths. Here is the general form of the if statement:

if (condition)
{
statement1;
}
else
{
statement2;
}

Here, each statement may be a single statement or a compound statement enclosed in curly braces (that is, a block). The condition is any expression that returns a boolean value. The else clause is optional. The if statement works like this:
If the condition is true, then statement1 is executed. Otherwise, statement2 (if it exists) is executed.





WAP to check whether a person is old or young. A person will be said old if his age is above 35 and young if his age is below 35.

class Man
{
public static void main(String args[])
{
int age= 49;
if(age>=35)
System.out.println("Old");
else
System.out.println("Young");
}
}

Output: Old

2. Nested if
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.





3. The if-else-if Ladder
A common programming construct that is based upon a sequence of nested ifs is the if-else-if ladder. It looks like this:

if (condition1)
statement1;
else if (condition2)
statement2;
………………………..
………………………..
else
statement3;

The if statements are executed from the top to down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.
WAP to calculate division obtained by a student with his percentage mark given.

class Result
{
public static void main(String args[])
{
int mark= 76;
if(mark>=60)
System.out.println("1st Division");
else if(mark>=50 && mark<60)
System.out.println("2nd Division");
else
System.out.println("3rd Division");
}
}

Output: 1st Division





Switch Statements
The switch statement is Java’s multi way 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. Here is the general form of a switch statement:

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

 





WAP to illustrate the use of switch case statements.
Solution:

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.






Iteration Statements

Java’s iteration statements are for, while, and do-while. These statements create what we commonly call loops. As you probably know, a loop repeatedly executes the same set of instructions until a termination condition is met.
1. while loop
The while loop is Java’s most fundamental looping statement. It repeats a statement or block while its
controlling expression is true. Here is its general form:
initialization

while (condition)
{
// body of loop
Increment/ decrement
}

The condition can be any Boolean expression. The body of the loop will be executed as long as the conditional expression is true. When condition becomes false, control passes to the next line of code immediately following the loop. The curly braces are unnecessary if only a single statement is being repeated.
WAP to print the message “Java is good” 10 times using while loop.

class Print
{
public static void main(String args[])
{
int n = 0;
while(n <10)
{
System.out.println("Java is good");
n++;
}
}
}

 





2. do while Loop
If the conditional expression controlling a while loop is initially false, then the body of the loop will not be executed at all. However, sometimes it is desirable to execute the body of a while loop at least once,even if the conditional expression is false to begin with. The do-while loop always executes its body at least once, because its conditional expression is at the bottom of the loop. Its general form is:
initialization

do
{
// body of loop
Increment/ decrement
} while (condition);

WAP to print the message “Java is good” 10 times using do while loop.

class Print
{
public static void main(String args[])
{
int n = 0;
do
{
System.out.println("Java is good");
n++;
} while(n<9);
}
}

 






3. for loop
The general form of the for statement is:

for(initialization; condition; iteration)
 {
 // body
 }

 
The for loop operates as follows:

  •  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. It is important to understand that the initialization expression is only executed once.
  • Next, condition is evaluated. This must be a Boolean expression. It usually tests the loop control variable against a target value. If this expression is true, then the body of the loop is executed. If it is false, the loop terminates.
  •  Next, the iteration portion of the loop is executed. This is usually an expression that increments or decrements the loop control variable. The loop then iterates, first evaluating the conditional expression, 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.

WAP to print the message “Java is good” 10 times using for loop.

class Print
{
public static void main(String args[])
{
int n ;
for(n=0; n<10; n++)
{
System.out.println("Java is good");
}
}
}

Jump Statements

Java offers following jump statements:
 break statement: A break statement takes control out of the loop.
 continue statement: A continue statement takes control to the beginning of the loop.
 goto statement: A goto Statement take control to a desired line of a program.





WAP to check whether a number is prime or not.

<em>class Prime</em>
{
public static void main(String args[])
{
int n=13 , i;
for(i=2; i<n; i++)
{
if(n%i==0)
{
System.out.println(“Number is not Prime”);
break;
}
}
if(i==n)
System.out.println(“Number is Prime”);
}
}

Output:
Number is Prime