You are on page 1of 141

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:

False
True
Flow Chart:

True False
The if else statement has one condition and two statement blocks-
True 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:
Start
Flow Chart:

Variable or Expression

Case A Case A Statements


True
False

Case B Case B Statements


True
False

… Case C Statements
True
False

default Default Statements

End
Start
Flow Chart:
Variable or Expression

Case A Case A Statements


True
break;
False

Case B Case B Statements


True
break;
False

… Case C Statements
True break;
False

default Default Statements

End
Example:
-In iti alizati on
-Con diti on ch ecking
-Executi on
-In crement / Decrement
Entry Controlled Exit Controlled
Condition is checked at the entry of the Condition is checked at the exit of the
loop loop
If condition is initially false, the loop If condition is initially false, then also the
never executes loop executes at least once

i=1; i=1;
while(i==0) do
{ {
System.out.println(“In While System.out.println(“In While loop”);
loop”); } while(i==0);
} System.out.println(“out of the loop”);
System.out.println(“out of the
loop”); Output:
In while loop
Output: Out of the 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


the condition statement is true
The execution statements are
the main body of a loop

All action statements of loop


are written here
This section is used
increment todecrement
or variable value
the
Output

Variable i in memory
Output Condition
is True
1

Variable i in memory
Check it
out here
Output
1
1

Variable i in memory
Check it
out Output
here
1
2
i

Variable i in memory
Output Condition is
checked again
1
2

Variable i in memory
Output Condition
is True
1
2

Variable i in memory
Check it
out here
Output
1
2 2
i

Variable i in memory
Check it
out Output
here
1
3 2
i

Variable i in memory
Output Condition is
checked again
1
3 2
i

Variable i in memory
Output Condition
is True
1
3 2
i

Variable i in memory
Check it
out here
Output
1
3 2
3
i

Variable i in memory
Check it
out Output
here
1
4 2
3
i

Variable i in memory
Output Condition is
checked again
1
4 2
3
i

Variable i in memory
Output Condition
is True
1
4 2
3
i

Variable i in memory
Check it
out here
Output
1
4 2
3
i
4

Variable i in memory
Check it
out Output
here
1
5 2
3
i
4

Variable i in memory
Output Condition is
checked again
1
5 2
3
i
4

Variable i in memory
This process will continue till the condition become false

Suppose value of i is 9 now


Output Condition
is True
1
9 2
3
i
4
5
Variable i in memory 6
7
8
Check it
out here
Output
1
9 2
3
i
4
5
Variable i in memory 6
7
8
9
Check it
out Output
here
1
10 2
3
i
4
5
Variable i in memory 6
7
8
9
Output Condition is
checked again
1
10 2
3
i
4
5
Variable i in memory 6
7
8
9
Output Condition
is Still True
1
10 2
3
i
4
5
Variable i in memory 6
7
8
9
Check it
out here
Output
1
10 2
3
i
4
5
Variable i in memory 6
7
8
9
10
Check it
out Output
here
1
11 2
3
i
4
5
Variable i in memory 6
7
8
9
10
Output Condition is
checked again
1
11 2
3
i
4
5
Variable i in memory 6
7
8
9
10
Output Condition
is FALSE
1
11 2
3
i
4
5
Variable i in memory 6
7
8
9
10
Output
1
11 2
3
i
4
5
Variable i in memory 6
7
8
9
Skip the body of
10
the lop and
executes the
statement just
after the loop
Final value of i
after the
complete loop Output

1
11 2
3
i
4
5
Variable i in memory 6
7
8
9
10
Output Final output of
the loop
1
11 2
3
i
4
5
Variable i in memory 6
7
8
9
10
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 Initialization

Variable i in memory
Condition
Output
is True

Variable i in memory
Check it
out here
Output
1
1

Variable i in memory
Check it increment
out Output
here
1
2
i

Variable i in memory
Condition is
Output checked again
1
2

Variable i in memory
Condition
Output
is True
1
2

Variable i in memory
Check it
out here
Output
1
2 2
i

Variable i in memory
Check it
out Output
here
1
3 2
i

Variable i in memory
Condition is
Output checked again
1
3 2
i

Variable i in memory
Condition
Output is True
1
3 2
i

Variable i in memory
Check it
out here
Output
1
3 2
3
i

Variable i in memory
Check it
out Output
here
1
4 2
3
i

Variable i in memory
Condition is
Output checked again
1
4 2
3
i

Variable i in memory
Condition
Output
is True
1
4 2
3
i

Variable i in memory
Check it
out here
Output
1
4 2
3
i
4

Variable i in memory
Check it
out Output
here
1
5 2
3
i
4

Variable i in memory
Output Condition is
checked again
1
5 2
3
i
4

Variable i in memory
This process will continue till the condition become false

Suppose value of i is 9 now


Condition
Output is True
1
9 2
3
i
4
5
Variable i in memory 6
7
8
Check it
out here
Output
1
9 2
3
i
4
5
Variable i in memory 6
7
8
9
Check it
out Output
here
1
10 2
3
i
4
5
Variable i in memory 6
7
8
9
Condition is
Output checked again
1
10 2
3
i
4
5
Variable i in memory 6
7
8
9
Condition
Output is Still True

1
10 2
3
i
4
5
Variable i in memory 6
7
8
9
Check it
out here
Output
1
10 2
3
i
4
5
Variable i in memory 6
7
8
9
10
Check it
out Output
here
1
11 2
3
i
4
5
Variable i in memory 6
7
8
9
10
Condition is
Output checked again
1
11 2
3
i
4
5
Variable i in memory 6
7
8
9
10
Output Condition
is FALSE
1
11 2
3
i
4
5
Variable i in memory 6
7
8
9
10
Output
1
11 2
3
i
4
5
Variable i in memory 6
7
8
9
Skip the body of
10
the lop and
executes the
statement just
after the loop
Final value of i
after the
complete loop Output

1
11 2
3
i
4
5
Variable i in memory 6
7
8
9
10
Output Final output of
the loop
1
11 2
3
i
4
5
Variable i in memory 6
7
8
9
10
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
The execution statements are
the main body of a loop

All action statements of loop


are written here
This section is used
increment todecrement
or variable value
the
Output

Variable i in memory
No condition
Output checking at entry of
the loop
1

Variable i in memory
Check it
out here
Output
1
1

Variable i in memory
Check it
out Output
here
1
2
i

Variable i in memory
Output
1
2

i
Condition is
checked again
Variable i in memory
Output
1
2

i Condition
is True

Variable i in memory
Check it
out here
Output
1
2 2
i

Variable i in memory
Check it
out Output
here
1
3 2
i

Variable i in memory
Output
1
3 2
i Condition is
checked again
Variable i in memory
Output
1
3 2
i
Condition
is True
Variable i in memory
Check it
out here
Output
1
3 2
3
i

Variable i in memory
Check it
out Output
here
1
4 2
3
i

Variable i in memory
Output
1
4 2
3
i Condition is
checked again
Variable i in memory
Output
1
4 2
3
i

Variable i in memory Condition


is True
Check it
out here
Output
1
4 2
3
i
4

Variable i in memory
Check it
out Output
here
1
5 2
3
i
4

Variable i in memory
Output
1
5 2
3
i Condition is
4
checked again
Variable i in memory
This process will continue till the condition become false

Suppose value of i is 9 now


Output
1
9 2
3
i
4
5 Condition
Variable i in memory 6 is True
7
8
Check it
out here
Output
1
9 2
3
i
4
5
Variable i in memory 6
7
8
9
Check it
out Output
here
1
10 2
3
i
4
5
Variable i in memory 6
7
8
9
Output
1
10 2
3
i
4 Condition is
5 checked again
Variable i in memory 6
7
8
9
Output
1
10 2
3
i Condition
4
is Still True
5
Variable i in memory 6
7
8
9
Check it
out here
Output
1
10 2
3
i
4
5
Variable i in memory 6
7
8
9
10
Check it
out Output
here
1
11 2
3
i
4
5
Variable i in memory 6
7
8
9
10
Output Condition is
checked again
1
11 2
3
i
4
5
Variable i in memory 6
7
8
9
10
Output
1
11 2
3
i Condition
4
5 is FALSE
Variable i in memory 6
7
8
9
10
Output
1
11 2
3
i
4
5
Variable i in memory 6
7
Stop the
execution of the 98
loop and
10
executes the
statement just
after the
loop
Final value of i
after the
complete loop Output

1
11 2
3
i
4
5
Variable i in memory 6
7
8
9
10
Output Final output of
the loop
1
11 2
3
i
4
5
Variable i in memory 6
7
8
9
10

You might also like