1. Flow Control in JAVA:
In javathere isdifferentflowcontrol statement:
Statement type Keyword
Looping While,do-while,for
Decision making If, if else, switch case
branching Break, continue
1. while:
While statement continually execute a block of statement while a
condition remains true.
While(expression)
{
Statements
}
2.do- while:
Instead of evaluating the condition at top.do while evaluate at the
bottom. Statement associated with do-while loo are executed at least
once.
2. Do
{
Statements
}
While(expression)
3.for loop:
Compact way to iterate over a range of values .
For(initialization;termination;increment)
{
Statements;
}
4.If:
It enables program to selectively execute other statements.
If(condition is true)
{
Execute the block
}
5.if-else:
3. Else block is executed if the if part is false:
e.g
If(testscore>=90)
{
Grade=’A’;
}
Else if(testscore>=76)
{
Grade=’B’;
}
Else if (testscore>=60)
{
Grade=’C’;
Else
{
Grade=’F’;
}
System.out.println(grade);
}
4. 6. switch:
It conditionallyperforms statements based on integer
expression.
Switch statement make decision based on single value.
7.Break:
Break statement is used to terminate the execution in switch,for,while
or do-while loop;
System.out.println(“starting”);
For(int n=0;n<7;++n)
{
System.out.println(“inloop:’+n);
If(n==2)
{
Continue;
}
System.out.println(“servivedin first loop”);
If(n==4)
{
Break;
}
System.out.println(“servivedsecond loop”);
}
}
System.out.println(“end of loop”);
Output:
In loop:0
Survived first
Survived second
In loop :1
Survived first
Survived second
In loop:2
In loop:3
Survived first
Survived second
In loop 4
Survived first
End of loop