Professional Documents
Culture Documents
Chapter-7
Flow of Control
Normally the flow of control is in chronological order, i.e. the
order in which it appears in the program. It means the
statement that appears first, is executed first. Statement
appearing next, is executed next one after the other.
Sometimes, we need to execute a set of statements if a
condition is true and if the condition is false, another set of
statements is executed. Example: if-else statement and
switch-case statement
Flow of control takes place in following ways:
a) Normal flow of control
b) Conditional flow of control
c) Multiple branching of control
Ways of flow of control
a) Normal flow of control
It is normal procedure where the control keeps on executing each and every statement
of the program on the basis of top-down approach. As soon as one statement has
been executed, the control moves to the next line for further processing. At the end, it
returns back to the system after displaying the result of the program.
// Calculate area of trainagle
import java.io.*;
public class P2
{
public static void main(String args[]) throws IOException
{
InputStreamReader ir=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(ir);
int r;
System.out.println("Enter the radius of circle: ");
r=Integer.parseInt(br.readLine());
double area=3.14*r*r;
System.out.println("Area : "+area);
}
}
b) Conditional flow of control
Sometimes, it may happen that you want to operate a block of
statements when the given condition holds true. If the condition is false,
the current block will be ignored and control moves to execute another
block. Thus, in this situation, control executes a specific block of
statements based on given condition, which is called conditional flow of
control. Example: if statement, if-else statement, if and only if
statement, if-else-if statement, nested if statement.
Simple statement:
Example:
System.out.println(“Good Day”);
Compound statement:
A compound statement is a collection of one or more than one
statements marked within curly braces is said to be compound
statement.
Example:
double D=b*b-4*a*c;
if(D>=0)
{
System.out.println(“Roots are real and distinct”);
r1=(-b+Math.sqrt(D))/(2*a);
r2=(-b-Math.sqrt(D))/(2*a);
}
switch-case (multi-way branching statement)
Syntax: Fall Through:
switch(control variable) If the break statement is not
{ used after a case then the
case 1: statement block-1 control enter into another case
break;
case 2: statement block-2
for execution. This condition is
break; called fall through.
..................
................. Default Case:
default: statement block-n This is a special case in the
} switch case structure. It is
executed when no case is
available for a given value of
switch variable.
Difference between if-else and switch-case
(important)
if-else switch-case
It is two way branching It is multi-way branching
statement statement
If-else can be implemented Switch statement can be
on any data type implemented only on integer and
char data type.
Any type of comparison can Equality comparison is only
be done in if-else statement. checked for control variable with
the case.