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:

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.

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:

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.

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:

 





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

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

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.





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

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






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

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.

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.

Output:
Number is Prime