Flow Control by Durga Sir
Flow Control by Durga Sir
Flow Control by Durga Sir
1. Introduction
2. Selection statements
if-else
Switch
Case Summary
fall-through inside a switch
default case
3. Iterative Statements
While loop
Unreachable statement in while
Do-while
Unreachable statement in do while
For Loop
Initilizationsection
Conditional check
Increment and decrement section
Unreachable statement in for loop
For each
Iterator Vs Iterable(1.5v)
Difference between Iterable and Iterator
4. Transfer statements
Break statement
Continue statement
Labeled break and continue statements
Do-while vs continue (The most dangerous combination)
Introduction:
Flow control describes the order in which all the statements will be
executed at run time.
Diagram:
Selection statements:
if-else:
syntax:
int x=0;
if(x) {
System.out.println("hello");
} else {
System.out.println("hi");
}}}
OUTPUT:
D:\Java>javac ExampleIf.java
found : int
required: boolean
if(x)
Example 2:
int x=10;
if(x=20) {
System.out.println("hello");
} else {
System.out.println("hi");
}}}
OUTPUT:
D:\Java>javac ExampleIf.java
found : int
required: boolean
if(x=20)
Example 3:
int x=10;
if(x==20) {
System.out.println("hello");
} else {
System.out.println("hi");
}}}
OUTPUT:
Hi
Example 4:
boolean b=false;
if(b=true)
System.out.println("hello");
}else{
System.out.println("hi");
}}}
OUTPUT:
Hello
Example 5:
boolean b=false;
if(b==true) {
System.out.println("hello");
} else {
System.out.println("hi");
}}}
OUTPUT:
Hi
Both else part and curly braces are optional. Without curly
braces we can take only one statement under if, but it should not
be declarative statement.
Example 6:
if(true)
System.out.println("hello");
}}
OUTPUT:
Hello
Example 7:
if(true);
}}
OUTPUT:
No output
Example 8:
if(true)
int x=10;
}}
OUTPUT:
D:\Java>javac ExampleIf.java
int x=10;
int x=10;
Example 9:
if(true){
int x=10;
}}}
OUTPUT:
D:\Java>javac ExampleIf.java
D:\Java>java ExampleIf
Example 10:
OUTPUT:
Hello
Hi
Syntax:
switch(x) {
case 1:
action1
case 2:
action2
default:
default action
Until 1.4 version the allow types for the switch argument are
byte, short, char, int but from 1.5 version on wards the
corresponding wrapper classes (Byte, Short, Character, Integer)
and "enum" types also allowed.
Diagram:
Example 1:
int x=10;
switch(x) {
System.out.println("hello");
}}}
OUTPUT:
D:\Java>javac ExampleSwitch.java
System.out.println("hello");
Every case label should be "compile time constant" otherwise we will
get compile time error.
Example 2:
int x=10;
int y=20;
switch(x) {
case y: System.out.println("20");
}}}
OUTPUT:
D:\Java>javac ExampleSwitch.java
case y:
Example 3:
public class ExampleSwitch{
int x=10;
switch(x) {
case y: System.out.println("20");
}}}
OUTPUT:
10
20
But switch argument and case label can be expressions, but case
label should be constant expression.
Example 4:
int x=10;
switch(x+1){
case 10:
case 10+20:
case 10+20+30:
}}}
OUTPUT:
No output.
Example 5:
byte b=10;
switch(b) {
}}}
OUTPUT:
found : int
required: byte
case 1000:
Example:
byte b=10;
switch(b+1) {
}}}
OUTPUT:
Example 6:
switch(x) {
}}}
OUTPUT:
D:\Java>javac ExampleSwitch.java
CASE SUMMARY:
Diagram:
FALL-THROUGH INSIDE THE SWITCH:
Within the switch statement if any case is matched from that case
onwards all statements will be executed until end of the switch
(or) break. This is call "fall-through" inside the switch.
The main advantage of fall-through inside a switch is we can
define common action for multiple cases.
Example 7:
int x=0;
switch(x) {
case 0: System.out.println("0");
case 1: System.out.println("1");
break;
case 2: System.out.println("2");
default: System.out.println("default");
}}}
OUTPUT:
0 1 2 default
1 default
DEFAULT CASE:
Within the switch we can take the default only once
If no other case matched then only default case will be executed
Within the switch we can take the default anywhere, but it is
convension to take default as last case.
Example 8:
int x=0;
switch(x) {
default: System.out.println("default");
case 0: System.out.println("0");
break;
case 1: System.out.println("1");
case 2: System.out.println("2");
}}}
OUTPUT:
0 1 2 default
2 0
ITERATIVE STATEMENTS:
While loop:
if we don't know the no of iterations in advance then best loop
is while loop:
Example 1:
while(rs.next())
Example 2:
while(e.hasMoreElements())
----------
----------
----------
Example 3:
while(itr.hasNext())
----------
----------
----------
while(1) {
System.out.println("hello");
}}}
OUTPUT:
D:\Java>javac ExampleWhile.java
found : int
required: boolean
while(1)
Curly braces are optional and without curly braces we can take
only one statement which should not be declarative statement.
Example 2:
while(true)
System.out.println("hello");
}}
OUTPUT:
while(true);
}}
OUTPUT:
No output.
Example 4:
while(true)
int x=10;
}}
OUTPUT:
D:\Java>javac ExampleWhile.java
int x=10;
int x=10;
Example 5:
int x=10;
}}}
OUTPUT:
No output.
Example 6:
while(true) {
System.out.println("hello");
System.out.println("hi");
}}
OUTPUT:
D:\Java>javac ExampleWhile.java
System.out.println("hi");
Example 7:
while(false) {
System.out.println("hello");
System.out.println("hi");
}}
OUTPUT:
D:\Java>javac ExampleWhile.java
Example 8:
int a=10,b=20;
while(a<b) {
System.out.println("hello");
System.out.println("hi");
}}
OUTPUT:
Example 9:
while(a<b) {
System.out.println("hello");
System.out.println("hi");
}}
OUTPUT:
D:\Java>javac ExampleWhile.java
System.out.println("hi");
Example 10:
while(a<20) {
System.out.println("hello");
System.out.println("hi");
}}
OUTPUT:
D:\Java>javac ExampleWhile.java
Note:
Example 11:
int a=10;
while(a<20) {
System.out.println("hello");
System.out.println("hi");
}}
OUTPUT:
Syntax:
Example 1:
do
System.out.println("hello");
while(true);
}}
Output:
do;
while(true);
}}
Output:
Compile successful.
Example 3:
do
int x=10;
while(true);
}}
Output:
D:\Java>javac ExampleDoWhile.java
int x=10;
int x=10;
Example 4:
do{
int x=10;
}while(true);
}}
Output:
Compile successful.
Example 5:
do
while(true)
System.out.println("hello");
while(true);
}}
Output:
while(true)
System.out.println("hello");
while(true);
}}
Output:
Example 6:
do
while(true);
}}
Output:
D:\Java>javac ExampleDoWhile.java
while(true);
Example 7:
do {
System.out.println("hello");
while(true);
System.out.println("hi");
}}
Output:
D:\Java>javac ExampleDoWhile.java
System.out.println("hi");
Example 8:
do {
System.out.println("hello");
while(false);
System.out.println("hi");
}}
Output:
Hello
Hi
Example 9:
int a=10,b=20;
do {
System.out.println("hello");
while(a<b);
System.out.println("hi");
}}
Output:
Example 10:
int a=10,b=20;
do
System.out.println("hello");
while(a>b);
System.out.println("hi");
}}
Output:
Hello
Hi
Example 11:
do
System.out.println("hello");
while(a<b);
System.out.println("hi");
}}
Output:
D:\Java>javac ExampleDoWhile.java
System.out.println("hi");
Example 12:
do
System.out.println("hello");
while(a>b);
System.out.println("hi");
}}
Output:
D:\Java>javac ExampleDoWhile.java
D:\Java>java ExampleDoWhile
Hello
Hi
For Loop:
This is the most commonly used loop and best suitable if we know
the no of iterations in advance.
Syntax:
Curly braces are optional and without curly braces we can take
only one statement which should not be declarative statement.
Initilization section:
This section will be executed only once. Here usually we can
declare loop variables and we will perform initialization.
We can declare multiple variables but should be of the same type
and we can't declare different type of variables.
Example:
Example 1:
int i=0;
for(System.out.println("hello u r sleeping");i<3;i++){
}}}
Output:
D:\Java>javac ExampleFor.java
D:\Java>java ExampleFor
Hello u r sleeping
Conditional check:
int i=0;
for(System.out.println("hello");i<3;System.out.println("hi")){
i++;
}}}
Output:
D:\Java>javac ExampleFor.java
D:\Java>java ExampleFor
Hello
Hi
Hi
Hi
All 3 parts of for loop are independent of each other and all
optional.
Example:
for(;;){
System.out.println("hello");
}}}
Output:
Example 1:
for(int i=0;true;i++){
System.out.println("hello");
System.out.println("hi");
}}
Output:
D:\Java>javac ExampleFor.java
System.out.println("hi");
Example 2:
for(int i=0;false;i++){
System.out.println("hello");
System.out.println("hi");
}}
Output:
D:\Java>javac ExampleFor.java
for(int i=0;false;i++){
Example 3:
for(int i=0;;i++){
System.out.println("hello");
System.out.println("hi");
Example 4:
int a=10,b=20;
for(int i=0;a<b;i++){
System.out.println("hello");
System.out.println("hi");
}}
Output:
Hello (infinite times).
Example 5:
for(int i=0;a<b;i++){
System.out.println("hello");
System.out.println("hi");
}}
Output:
D:\Java>javac ExampleFor.java
System.out.println("hi");
Example 1:
Example:
Output:
D:\Java>javac ExampleFor.java
D:\Java>java ExampleFor
10
20
30
40
50
Example 2:
Example 3:
Write equivalent code by For Each loop for the following for loop.
for(int i=0;i<10;i++)
System.out.println("hello");
}}}
Output:
D:\Java>javac ExampleFor1.java
D:\Java>java ExampleFor1
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Transfer statements:
Break statement:
We can use break statement in the following cases.
Example 1:
class Test{
int x=0;
switch(x)
case 0: System.out.println("hello");
break ;
case 1: System.out.println("hi");
Output:
D:\Java>javac Test.java
D:\Java>java Test
Hello
Inside loops:
We can use break statement inside loops to break the loop based
on some condition.
Example 2:
class Test{
break;
System.out.println(i);
}}
Output:
Output:
D:\Java>javac Test.java
D:\Java>java Test
Example:
class Test{
int x=10;
l1 : {
System.out.println("begin");
if(x==10)
break l1;
System.out.println("end");
System.out.println("hello");
Output:
D:\Java>javac Test.java
D:\Java>java Test
Begin hello
These are the only places where we can use break statement. If we
are using anywhere else we will get compile time error.
Example:
class Test{
int x=10;
if(x==10)
break;
System.out.println("hello");
Output:
Compile time error.
D:\Java>javac Test.java
break;
Continue statement:
We can use continue statement to skip current iteration and
continue for the next iteration.
Example:
Output:
D:\Java>javac Test.java
D:\Java>java Test
Example:
class Test {
int x=10;
if(x==10);
continue;
System.out.println("hello");
Output:
continue;
Syntax:
Example:
class Test {
l1:
for(int i=0;i<3;i++) {
for(int j=0;j<3;j++) {
if(i==j)
break;
System.out.println(i+"........."+j);
Break:
1.........0
2.........0
2.........1
Break l1:
No output.
Continue:
0.........1
0.........2
1.........0
1.........2
2.........0
2.........1
Continue l1:
1.........0
2.........0
2.........1
Output:
6
8
10
Example 1:
class Test {
while(true) {
System.out.println("hello");
System.out.println("hi");
Output:
D:\Enum>javac Test.java
System.out.println("hi");
Example 2:
class Test {
if(true) {
System.out.println("hello");
} else {
System.out.println("hi");
}}}
Output:
Hello