Java Decision Making Statements
Using if-else
- It is a two-way decision making statement, which is used along with the construction of an expression.
- It is of Four types :
Simple if
if else loop is use for condition check. The if-then statement is the most basic of all the control flow statements. It tells your program to execute a certain section of code only if a particular test evaluates to true.
Syntax :
if(condition)
{
statement;
}
statement;
if ... elseSyntax :
if(condition)
{
statement;
}
else
{
statement;
}
Nested ifSyntax :
if(condition)
{
if(condition)
{
statement;
}
else
{
statement;
}
}
else
{
statement;
}
else ... if ladderSyntax :
if(condition)
{
statement;
}
else if(condition)
{
statement;
}
else
{
statement;
}
statement;
Using switch
switch case is used to check a number of possible execution paths. A switch can work with the byte, short, char and int primitive data types.
switchSince JDK 1.7 we have facility to take an
String inside the switch case.colon (:)default statement is executed if there is
no match with any case specified in the switch case.
break;
statement.
Syntax :
switch(expression
{
case value1: statement();
break;
case value2: statement();
break;
case value3: statement();
break;
default: statement();
break;
}
Loop
for loop allows a code to be repeatedly executed. A for loop is classified as an iteration statement.
Difference between while and do while is that while statement continually executes a block of statements
while a particular condition is true whereas do while evaluates its expression at the bottom of the loop instead of the top.
Therefore, the statements within the do block are always executed at least once.
- When you want to
repeatsomething for definite number of times, it is called loop. - Java has three basic loops
for()- More than one variable can initialize at a time in this loop statement.
- The increment section should have also one part.
Syntax :
for(initialization; condition;increment/decrement)
{
loop body;
}
while()- Here when the test condition fails, control is transferred out of the loop, otherwise the loop will continue or repeat itself.
- Here the body of the loop may not be executed at all, if the condition is not satisfied for the first time.
Syntax :
initialization;
while(condition)
{
loop body;
}
do-while()- Since the test condition is provided at the bottom of the loop, it is always executed at least once.
- This loop will be executed till the condition is true.
Syntax :
initialization;
do
{
body of loop;
}while(condition);
Enhance for loop
Since JDK 1.5 (tiger) java introduce one more loop called Enhance for loop.
Enhanced for loop allows you to iterate through a collection without having to create an Iterator or without having to calculate beginning and end conditions for a counter variable.
- The enhanced for loop was created as a shortcut for when you don't need the index of the element. It streamlined things.
int a[]= {10,20, 30, 40, 50};
for(int x: a)
{
System.out.println(x);
}



