Control flow statement:
The
statement which controls the flow of execution of a program is called a control
flow statement.
Types of control flow
statement
·
Decision making statements
·
Loop statements
Decision making statements:
Decision
making statements help to whether to execute set of instruction or not based on
the given favourable condition.
Typed
of decision-making statements:
i.
If statement
ii.
If else statement
iii.
If else if / else if ladder statement
iv.
Switch statement
I. If statement:
Syntax:
if
(condition)
{
Statement/task/instruction;
}
Statement
which belongs to if get executed if condition is true
If condition is false condition belongs to
if not execute
The
condition should return Boolean type output
Example:
Class
If1
{
Public
static void main(String [] args){
int
x =100 , y = 20 ;
if
(x>y)
{
System.out.println(“
x is larger”); // “ x is
larger”
}
}
}
II. If else statement:
Syntax:
If(condition)
{
Statement;
}
Else
{
Statement
;
}
If
block get executed if condition is evaluated true. Else block executed only when if condition evaluated too false. In these any one of the blocks
get executed (either if or else block)
Example:
Class IfElse
{
Public
static void main(String [] args)
{
int
x =100 , y = 20 ;
if
(x<y)
{
System.out.println(“
x is larger”);
}
Else
{
System.out.println(“
y is smaller”);
}
}
}
III. If else if / else if ladder statement
Syntax:
If(condition)
{
Statement1;
}
Else
if (condition)
{
Statement2
;
}
Else
if( condition)
{
Statement3;
}
.
.
.
.
Else
{
Statement;
}
·
Else if ladder is used when we have multiple condition to check with
if statement.
·
The condition written inside if must be of Boolean type
·
We can have any number of else if block with
condition
·
Having else block at end is not mandatory
·
If any of the condition become true that block
will get executed & immediately control go out of if else ladder
·
If the first condition returns false control
goes to second condition block. If second block is false control goes to third
condition block these will repeat until anyone of the condition block return
true.
·
If none
of the condition true else block get executed.
IV. Switch statement:
Syntax:
Switch(value/expression)
Case(value/expression):
{
Statement;
}
Break;
Case(value/expression):
{
Statement;
}
Break;
Default
{
Statement;
}
Break;
·
For switch we can pass data expression which are
of type of byte , short, int, char & string only. We can not pass data or
expression of type long , float, double, Boolean.
·
In switch we can have any number of case block&
default block.
·
Using default block is not mandatory.
·
For a case we can provide value or expression but not a variable
Control flow in switch:
§
A value given to switch is compared with the
value present in case using equality operator. If a case is evaluated false
control goes to next case block.
§
If case evaluated true statement of that case
executed. If break statement is not use for the statements of below case are
also executed.
§
If all cases are evaluated false default block
get executed.
Break:
·
It is a key word
·
It is control transferred statement.
·
Break statement can be use inside switch & loops.
Using break in switch:
·
When we use break statement after the case block.
If case evaluated true control goes outside the switch block.
·
Using break is optional after every case block
Example:
Import
java.util.Scanner;
Class
Switch{
Public
static void main(String [] args)
{
Scanner s = new
scanner(System.in) ;
System.out.println(“
Enter alphabate”);
Char
vowel = s.next().charAt(0) ;
Switch(vowel)
Case(‘a’):
{
Syatem.out.println(“it’s
vowel);
}
Case(‘e’):
{
Syatem.out.println(“it’s
vowel);
}
Case(‘i’):
{
Syatem.out.println(“it’s
vowel);
}
Case(‘o’):
{
Syatem.out.println(“it’s
vowel);
}
Case(‘u’):
{
Syatem.out.println(“it’s
vowel);
}
Default
:
{
Syatem.out.println(“it’s
not vowel);
}
If
we provide any char input value respective
case block get value if none of them evaluated true default block get executed.