Flow Control in Java Programs

Flow Control in Java Programs
if Statement
The purpose of the if statement is to make decisions, and execute different parts of your program depending on a boolean true/false value.
The ‘if’ statement has this form, where condition is true or false
if (condition)
{
… // Do this clause if the condition is true.
}
Alternative forms of if statements –
if (condition)
{
… // Do this clause if the condition is true
}
else
{
… // Do this clause if the condition is false
}
Always make sure that the number of if clause should not exceed the number of else clause, because it will result in ‘dangling else problem’, in such case make use of if…else if…else construct as shown below –
if (condition 1)
{
… // Do this clause if the condition is true
}
else if (condition 2)
{
… // Do this clause if the condition is false
}
else
{
… // Do this clause if the condition is false
}
switch…case Statement
The if statement allows you to select one of two sections of code to execute based on a boolean value (only two possible values). The switch statement allows you to choose from many statements based on an integer (including char)
switch (expr)
{
case c1: … break;
case c2: … break;
…
default:
}
switch
The switch keyword is followed by a parenthesized integer expression, which is followed by the cases, all enclosed in braces.. The switch statement executes the case corresponding to the value of the expression. Normally the code in a case clause ends with a break statement, which exits the switch statement and continues with the statement following the switch. If there is no corresponding case value, the default clause is executed. If no case matched and there is no default clause, execution continues after the end of the switch statement.
case
The case keyword is followed by an integer constant and a colon. This begins the statements that are executed when the switch expression has that case value.
default
If no case value matches the switch expression value, execution continues at the default clause. This is the equivalent of the “else” for the switch statement. It is written after the last case be convention, and typically isn’t followed by break because execution just continues out the bottom of switch if this is the last clause.
break
The break statement causes execution to exit to the statement after the end of the switch. If there is no break, execution falls through into the next case. Flowing directly into the next case is almost always an error.
A switch statement can often be rewritten as a substitute for ‘if’ statement in a straightforward manner. For example, the preceding switch statement could be written as follows. When one of a number of blocks of code is selected based on a single value, the switch statement is generally easier to read. The choice of if or switch should be based on which is more readable.
Limitations of switch case
Java’s switch statement, which was taken directly from C++ to increase its attractiveness to C++ programmers, is not well loved.
• No ranges. It doesn’t allow ranges, eg case 90-100:. Many other languages do.
• Integers only. It requires integers and doesn’t allow useful types like String. Many other languages do.
• Error-prone. It is error-prone and a common source of bugs – forgetting break or default silently ignores errors. Some languages have eliminated these dangerous situations.
• Fall through. Another disadvantage of switch…case is that in absence of break statement in the respective cases will cause the flow control to cascade down and execute all the cases that does not contain ‘break’ statements until it reaches end of switch or a case that implements a break. This is called fall through.
Iteration through Loops
The purpose of loop or iterative statements is to repeat a block of Java statements several times depending upon the condition mentioned within the loop. There are two kinds of looping statements in Java.
1. Entry Controlled Loop
2. Exit Controlled
An Entry Controlled loop which is also known as Pre Tested Loop the condition of the loop is first checked and then the loop body is executed, which means if the condition is false the loop doesn’t get executed even for once.
E.g. a. for LOOP
b. while LOOP
An Exit Controlled loop which is also known as Post Tested Loop the condition is checked at the end of the loop body, which means even if the condition is false the loop gets executed at least for once.
E.g. a. do…while LOOP
If the body of a loop has more than one statement, you must put the statements inside braces. If there is only one statement, it is not necessary to use braces { }. However, many programmers think it is a good idea to always use braces to indicate the scope of statements. Always using braces allows the reader to relax and not worry about the special single statement case.
while statement
The while statement is used to repeat a block of statements while some condition is true. The condition must become false somewhere in the loop, otherwise it will never terminate.
Syntax:
initialization;
while (condition)
{
Body;
update;
}
//… While loop to display squares of natural nos. less than 10
int i = 1;
while (i <=10)
{
System.out.println(i*i);
i++;
}
for statement
Many loops consist of three operations surrounding the body:
(1) initialization of a variable,
(2) testing a condition, and
(3) updating a value before the next iteration.
The for loop groups these three common parts together into one statement, making it more readable and less error-prone than the equivalent while loop. For repeating code a known number of times, the for loop is the right choice.
There are three clauses in the ‘for’ statement.
1. The initialization statement is done before the loop is started, usually to initialize an iteration variable.
2. The condition/test expression is tested before each time the loop is done. The loop isn’t executed if the boolean expression is false (the same as the while loop).
3. The update statement is done after the body is executed. It typically increments an iteration variable.
Syntax:
for (initialization ; condition/test expression; update)
{
body
}
//… For loop to to display squares of natural nos. less than 10
for (int i = 1; i <= 10; i++)
{
System.out.println(i*i);
}
do…while statement
When you want to test at the end to see whether something should be repeated, the do…while statement is the natural choice
Syntax
do
{
. . .
} while (condition);
Always remember that a do…while statement has a semi-colon at its end.
The for loop is shorter, and combining the initialization, test, and increment in one statement makes it easier to read and verify that it’s doing what you expect. The for loop is better when you are counting something. If you are doing something an indefinite number of times, while loop may be the better choice.

Comments