You are on page 1of 12

Conditional Statements

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.

// To check whether number is even or odd


public class Q1
{
public static void main( int n)
{
if(n%2==0)
System.out.println(n+” is even number”);
else
System.out.println(n+” is odd number”);
}
}
c) Multiple branching of control:
switch-case statement implements multiple branching of
control.
import java.util.Scanner;
public class Q1
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int a,b,c;
System.out.println("Enter a digit: ");
a=sc.nextInt();
switch(a)
{
case 1: System.out.println("One"); break;
case 2: System.out.println("Two"); break;
case 3: System.out.println("Threee"); break;
default: System.out.println("Unkown");
}
}
}
if-else (two-way branching statement)
Syntax:
if(condition)
{ Condition is evaluated first. If the
Statement Block-1 condition is true, the statement
} block-1 will be executed and if the
else condition is false, statement block-
{ 2 will be executed.
Statement Block-2
}
The condition n>0 is
if (n>0) checked. If the condition is
System.out.println(“Positive”); true, ‘Positive’ will be
else displayed and otherwise,
System.out.println(“Negative”); ‘Negative’ will be displayed.
Different forms
Syntax: Condition-1 is evaluated first. If
if(condition-1) { the condition is true, the
Statement Block-1
statement block-1 will be
}
else executed. If condition-1 is false,
if(condition-2) { condition-2 is evaluated. If
Statement Block-2 condition-2 is true, statement
} block-2 will be executed. If
else condition is false, condition-3
if(condition-3) {
will be evaluated. If the
Statement Block-3
} condition-3 is true, statement
else { block-3 will be executed and if
Statement Block-4 condition-3 is false, statement-4
} will be executed.
Only if

Syntax: Condition-1 is evaluated first. If


if(condition-1) { the condition is true, the
Statement Block-1 statement block-1 will be
} executed and if the condition-1
is false, statement block-1 will
be ignored and control will go
to next line after the statement
block.
switch-case (multi-way branching statement)
Syntax: Switch-case is a multi-way
switch(control variable) branching statement, where a
{
specific statement block will be
case 1: statement block-1
break; executed based on the control
case 2: statement block-2 variable’s value that matches
break; the case. If no value is matched,
.................. then the block against default
................. will be executed.
default: statement block-n
}
Break statement is used at the
end of each case that is
referred to as case terminator.
It forces the control to exit from
the switch block.
SIMPLE AND COMPOUND STATEMENTS

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.

You might also like