Decision Making - Branching - Looping
Decision Making - Branching - Looping
For example,
if it is raining, you will take an umbrella. In the same
way, you can incorporate the decision making
techniques in the Java programming language. The
decision making technique can be implemented in the
Java programs by using the following conditional
constructs:
The if construct
The if…else construct
Nesting of if ... else Statements
The else if Ladder
The switch construct
The ?: Operator.
The if statement
It is one of the simplest decision-making
statement which is used to decide whether a
block of Java code will execute if a certain
condition is true.
Example
//Java Program to demonstate the use of if statem
ent.
public class IfExample
{
public static void main(String[] args)
{
//defining an 'age' variable
int age=20;
//checking the age
if(age>18)
{
System.out.println("Age is greater than 18");
}
System.out.println(“Part of Main()”);
}
}
Java if-else Statement
An if….else statement includes two blocks that are if
block and else block. It is the next form of the control
statement, which allows the execution of Java code in a
more controlled way. It is used when you require to check
two different conditions and execute a different set of
codes. The else statement is used for specifying the
execution of a block of code if the condition is false.
Syntax
if (condition)
{
// block of code will execute if the condition is true
}
else
{
// block of code will execute if the condition is false
}
If the condition is true, then the statements inside if
block will be executed, but if the condition is false,
then the statements of the else block will be
executed.
//A Java Program to demonstrate the use of
else statement.
//It is a program of odd and even number.
public class IfElseExample
{
public static void main(String[] args)
{
//defining a variable
int number=13;
//Check if the number is divisible by 2 or not
if(number%2==0)
{
System.out.println("even number");
}
else
{
System.out.println("odd number");
}
}
}
Leap Year Example
A year is leap, if it is divisible by 4 and 400. But,
not by 100.
Syntax:
if(condition)
{
//code to be executed
if(condition)
{
//code to be executed
}
}
//Java Program to demonstrate the use of Nested If Statement.
Syntax:
if(condition1)
{
//code to be executed if condition1 is true
}
else if(condition2)
{
//code to be executed if condition2 is true
}
else if(condition3)
{
//code to be executed if condition3 is true
}
...
else
{
//code to be executed if all the conditions are false
}
//Java Program to demonstrate the use of If else-if ladder.
//It is a program of grading system for fail, D grade, C grade, B grade, A grade a
nd A+.
public class IfElseIfExample
{
public static void main(String[] args)
{
int marks=65;
if(marks<50){
System.out.println("fail");
}
else if(marks>=50 && marks<60){
System.out.println("D grade");
}
else if(marks>=60 && marks<70){
System.out.println("C grade");
}
else if(marks>=70 && marks<80){
System.out.println("B grade");
}
else if(marks>=80 && marks<90){
System.out.println("A grade");
}else if(marks>=90 && marks<100){
System.out.println("A+ grade");
}
else{
System.out.println("Invalid!");
}
}
}
The switch construct
The Java switch statement executes one
statement from multiple conditions. It is
like if-else-if ladder statement. The switch
statement works with byte, short, int, long,
enum types, String and some wrapper types like
Byte, Short, Int, and Long. Since Java 7, we can
use strings in the switch statement.
case value2:
//code to be executed;
break; //optional
......
default:
code to be executed if all cases are not matched;
}
//Java Program to demonstrate the example of Switch statement
//where we are printing month name for the given number
public class SwitchMonthExample
{
public static void main(String[] args)
{
//Specifying month number
int month=7;
String monthString="";
//Switch statement
switch(month)
{
//case statements within the switch block
case 1: monthString= “1 - January"; break;
case 2: monthString= “2 - February"; break;
case 3: monthString= "3 - March"; break;
case 4: monthString= "4 - April"; break;
case 5: monthString= "5 - May"; break;
case 6: monthString= "6 - June"; break;
case 7: monthString= "7 - July"; break;
case 8: monthString= "8 - August"; break;
case 9: monthString= "9 - September"; break;
case 10: monthString= "10 - October"; break;
case 11: monthString= "11 - November"; break;
case 12: monthString= "12 - December"; break;
Example:
public class IfElseTernaryExample
{
public static void main(String[] args)
{
int number=13;
//Using ternary operator
String output=(number%2==0)?"even number":
"odd number";
System.out.println(output);
}
}
public class MathLibraryExample {
public static void main(String[] args) {
int i = 7;
int j = -9;
double x = 72.3;
double y = 0.34;
// Truncating and Rounding functions You can round off a floating point
//number to the nearest integer with round()
System.out.println(x + " is approximately " + Math.round(x));
System.out.println(y + " is approximately " + Math.round(y));
// Comparison operators
}
}
Working with Loop Constructs
In computer programming, loops are used to
repeat a block of code. A looping statement
enables you to execute the same statements for
a certain number of times.
Initialization Expression
Test Expression
Increment/Decrement Expression
How does a For loop work?
1. Initialization is done
2. The flow jumps to Condition
3. Condition is tested.
1. If the Condition yields true, the flow goes into the Body
2. If the Condition yields false, the flow goes outside the
loop
7. The for loop has ended and the flow has gone
outside.
Example1
for(i=5;i>0;i--)
{
for(j=1;j<=i;j++)
{
System.out.print(j);
}
System.out.println();
}
}
}
Break and Continue
At times, after certain iterations, you need to
exit from the loop. To achieve this, Java
provides the break statement. The break
statement stops the execution of the remaining
statements within the body of the loop.
Syntax:
while (condition){
//code to be executed
Increment / decrement statement
}
How Does a While loop execute?
3. Condition is tested.
1. If Condition yields true, the flow goes into the Body.
2. If Condition yields false, the flow goes outside the loop
7. The while loop has ended and the flow has gone
outside.
// Java program to illustrate while loop
class WhileLoopDemo {
public static void main(String args[])
{
int x = 1, sum = 0;
5. Condition is tested.
1. If Condition yields true, go to Step 6.
2. If Condition yields false, the flow goes outside the
loop
do
{
System.out.println("value of x : " + x );
x++;
}while( x < 20 );
}
}
For-each (Enhanced) Loop
The Java for-each loop or enhanced for loop is
introduced since J2SE 5.0. It provides an
alternative approach to traverse the array or
collection in Java. It is mainly used to traverse
the array or collection elements. The advantage
of the for-each loop is that it eliminates the
possibility of bugs and makes the code more
readable. It is known as the for-each loop
because it traverses each element one by one.
labelname:
for(initialization; test-condition; incr/decr)
{
// code to be executed.
}
continue and break in Labelled loop
The continue and break statements can also be used with a label like
this:
Syntax:
continue labelname;
// It is called labelled continue statement.
// Here, labelname represents the name of loop.
Similarly,
break labelname;
// It is called labelled break statement.
package javaProgram;
public class LabelledLoopEx
{
public static void main(String[] args)
{
// Outer loop.
outer: for(int i = 1; i < 5; i++)
{
System.out.println(i);
// Inner loop.
for(int j = 1; j < 3; j++)
{
System.out.println(j);
if(i == j)
continue outer;
}
}
}
}
package javaProgram;
public class LabelledLoopEx2
{
public static void main(String[] args)
{
// Outer loop.
outer: for(int i = 1; i < 3; i++)
{
System.out.println("i: " +i);
// Inner loop.
int j = 1;
while(j < 3)
{
System.out.println("j: " +j);
int x = i + j;
if(x > 2)
break outer;
j++;
}
}
System.out.println("Jumping out of both labelled loops");
}
}