It is used to take decision based on a single condition
Syntax:
False
True

If condition is True; control will enter the if block
If condition is False; control will execute statement followed by if block
Flow Chart:
True

False
Example: Read marks from user and state only if user is pass.
It is used to take decision based on a single condition
Syntax:
True

False
Flow Chart:
True

False
The if else statement has one condition and two statement blocksTrue block and False block
If condition is True; control will execute the statement in the true block
If condition is False; control will execute the statement in false block
Example: Read marks from user and state whether student is Pass or Fail
It is used to take decision based on two conditions.
Syntax:
Flow Chart:
True

False

If within else
Example:
The Nested if can be inside the if-part or else-part
Syntax:
Flow Chart:
True

False

If within else
Flow Chart:
True

False

If within else
Example:
Example:
A switch statement is used to test many conditions
Syntax:
Flow Chart:

Start

Variable or Expression
Case A Statements

Case A
True
False

Case B Statements

Case B
True
False

Case C Statements

…
True
False

default

End

Default Statements
Flow Chart:

Start

Variable or Expression
Case A
True

Case A Statements
break;

False

Case B
True

Case B Statements
break;

False

…
True

Case C Statements
break;

False

default

End

Default Statements
Example:
-Initialization
-Condition checking
-Execution
-Increment / Decrement
Entry Controlled

Exit Controlled

Condition is checked at the entry of the
loop

Condition is checked at the exit of the
loop

If condition is initially false, the loop
never executes

If condition is initially false, then also the
loop executes at least once

i=1;
while(i==0)
{
System.out.println(“In While loop”);
}
System.out.println(“out of the loop”);

i=1;
do
{
System.out.println(“In While loop”);
} while(i==0);
System.out.println(“out of the loop”);

Output:
Out of the loop

Output:
In while loop
Out of the loop

Example- for, while

Example – do-while
Initialization Statement is used
to initialize a variable/ counter.
The condition statement controls
the execution of loop
The loop executes till
condition statement is true

the
The execution statements are
the main body of a loop
All action statements of loop
are written here
This section is used to
increment or decrement
the variable value
Output
1
i

Variable i in memory
Output
1
i

Variable i in memory

Condition
is True
Output
1
1
i

Variable i in memory

Check it
out here
Check it
out here

Output
1

2
i

Variable i in memory
Output
1
2
i

Variable i in memory

Condition is
checked again
Output
1
2
i

Variable i in memory

Condition
is True
Output
2
i

Variable i in memory

1
2

Check it
out here
Check it
out here
3
i

Variable i in memory

Output
1
2
Output
3
i

Variable i in memory

1
2

Condition is
checked again
Output
3
i

Variable i in memory

1
2

Condition
is True
Output
3
i

Variable i in memory

1
2
3

Check it
out here
Check it
out here
4
i

Variable i in memory

Output
1
2
3
Output
4
i

Variable i in memory

1
2
3

Condition is
checked again
Output
4
i

Variable i in memory

1
2
3

Condition
is True
Output
4
i

Variable i in memory

1
2
3
4

Check it
out here
Check it
out here
5
i

Variable i in memory

Output
1
2
3
4
Output
5
i

Variable i in memory

1
2
3
4

Condition is
checked again
This process will continue till the condition become false

Suppose value of i is 9 now
Output
9
i

Variable i in memory

1
2
3
4
5
6
7
8

Condition
is True
Output
9
i

Variable i in memory

1
2
3
4
5
6
7
8
9

Check it
out here
Check it
out here
10
i

Variable i in memory

Output
1
2
3
4
5
6
7
8
9
Output
10
i

Variable i in memory

1
2
3
4
5
6
7
8
9

Condition is
checked again
Output
10
i

Variable i in memory

1
2
3
4
5
6
7
8
9

Condition
is Still True
Output
10
i

Variable i in memory

1
2
3
4
5
6
7
8
9
10

Check it
out here
Check it
out here
11
i

Variable i in memory

Output
1
2
3
4
5
6
7
8
9
10
Output
11
i

Variable i in memory

1
2
3
4
5
6
7
8
9
10

Condition is
checked again
Output
11
i

Variable i in memory

1
2
3
4
5
6
7
8
9
10

Condition
is FALSE
Output
11
i

Variable i in memory

Skip the body of
the lop and
executes the
statement jast
after the loop

1
2
3
4
5
6
7
8
9
10
Final value of i
after the
complete loop Output
11
i

Variable i in memory

1
2
3
4
5
6
7
8
9
10
Output
11
i

Variable i in memory

1
2
3
4
5
6
7
8
9
10

Final output of
the loop
Initialization Statement is used to initialize a variable/ counter.
The condition statement controls the execution of loop
The loop executes till the condition statement is true
This section is used to increment or decrement the variable value
The execution statements are the main body of a loop
All action statements of loop are written here
Output
1
i

Variable i in memory

Initialization
Output
1
i

Variable i in memory

Condition
is True
Check it
out here
Output
1
1
i

Variable i in memory
Check it
out here

increment
Output
1

2
i

Variable i in memory
Output
1
2
i

Variable i in memory

Condition is
checked again
Output
1
2
i

Variable i in memory

Condition
is True
Output
2
i

Variable i in memory

1
2

Check it
out here
Check it
out here
3
i

Variable i in memory

Output
1
2
Output
3
i

Variable i in memory

1
2

Condition is
checked again
Output
3
i

Variable i in memory

1
2

Condition
is True
Output
3
i

Variable i in memory

1
2
3

Check it
out here
Check it
out here
4
i

Variable i in memory

Output
1
2
3
Output
4
i

Variable i in memory

1
2
3

Condition is
checked again
Output
4
i

Variable i in memory

1
2
3

Condition
is True
Output
4
i

Variable i in memory

1
2
3
4

Check it
out here
Check it
out here
5
i

Variable i in memory

Output
1
2
3
4
Output
5
i

Variable i in memory

1
2
3
4

Condition is
checked again
This process will continue till the condition become false

Suppose value of i is 9 now
Output
9
i

Variable i in memory

1
2
3
4
5
6
7
8

Condition
is True
Output
9
i

Variable i in memory

1
2
3
4
5
6
7
8
9

Check it
out here
Check it
out here
10
i

Variable i in memory

Output
1
2
3
4
5
6
7
8
9
Output
10
i

Variable i in memory

1
2
3
4
5
6
7
8
9

Condition is
checked again
Output
10
i

Variable i in memory

1
2
3
4
5
6
7
8
9

Condition
is Still True
Output
10
i

Variable i in memory

1
2
3
4
5
6
7
8
9
10

Check it
out here
Check it
out here
11
i

Variable i in memory

Output
1
2
3
4
5
6
7
8
9
10
Output
11
i

Variable i in memory

1
2
3
4
5
6
7
8
9
10

Condition is
checked again
Output
11
i

Variable i in memory

1
2
3
4
5
6
7
8
9
10

Condition
is FALSE
Output
11
i

Variable i in memory

Skip the body of
the lop and
executes the
statement just
after the loop

1
2
3
4
5
6
7
8
9
10
Final value of i
after the
complete loop Output
11
i

Variable i in memory

1
2
3
4
5
6
7
8
9
10
Output
11
i

Variable i in memory

1
2
3
4
5
6
7
8
9
10

Final output of
the loop
Initialization Statement is used
to initialize a variable/ counter.
The condition statement controls
the execution of loop
The loop executes till
condition statement is true

the
The execution statements are
the main body of a loop
All action statements of loop
are written here
This section is used to
increment or decrement
the variable value
Output
1
i

Variable i in memory
Output
1
i

Variable i in memory

No condition
checking at entry of
the loop
Output
1
1
i

Variable i in memory

Check it
out here
Check it
out here

Output
1

2
i

Variable i in memory
Output
1
2
i

Variable i in memory

Condition is
checked again
Output
1
2
i

Variable i in memory

Condition
is True
Output
2
i

Variable i in memory

1
2

Check it
out here
Check it
out here
3
i

Variable i in memory

Output
1
2
Output
3
i

Variable i in memory

1
2
Condition is
checked again
Output
3

1
2

i

Variable i in memory

Condition
is True
Output
3
i

Variable i in memory

1
2
3

Check it
out here
Check it
out here
4
i

Variable i in memory

Output
1
2
3
Output
4
i

Variable i in memory

1
2
3

Condition is
checked again
Output
4
i

Variable i in memory

1
2
3

Condition
is True
Output
4
i

Variable i in memory

1
2
3
4

Check it
out here
Check it
out here
5
i

Variable i in memory

Output
1
2
3
4
Output
5
i

Variable i in memory

1
2
3
4

Condition is
checked again
This process will continue till the condition become false

Suppose value of i is 9 now
Output
9
i

Variable i in memory

1
2
3
4
5
6
7
8

Condition
is True
Output
9
i

Variable i in memory

1
2
3
4
5
6
7
8
9

Check it
out here
Check it
out here
10
i

Variable i in memory

Output
1
2
3
4
5
6
7
8
9
Output
10
i

Variable i in memory

1
2
3
4
5
6
7
8
9

Condition is
checked again
Output
10
i

Variable i in memory

1
2
3
4
5
6
7
8
9

Condition
is Still True
Output
10
i

Variable i in memory

1
2
3
4
5
6
7
8
9
10

Check it
out here
Check it
out here
11
i

Variable i in memory

Output
1
2
3
4
5
6
7
8
9
10
Output
11
i

Variable i in memory

1
2
3
4
5
6
7
8
9
10

Condition is
checked again
Output
11
i

Variable i in memory

1
2
3
4
5
6
7
8
9
10

Condition
is FALSE
Output
1
11
2
3
i
4
5
Variable i in memory
6
7
Stop the
8
execution of the
9
loop and
10
executes the
statement just
after the loop
Final value of i
after the
complete loop Output
11
i

Variable i in memory

1
2
3
4
5
6
7
8
9
10
Output
11
i

Variable i in memory

1
2
3
4
5
6
7
8
9
10

Final output of
the loop

java programming- control statements