Professional Documents
Culture Documents
Normally, statements in a program are executed one after the other in the order in which they
are written. This process is called sequential execution. Various Java statements, which we
will soon discuss, enable you to specify that the next statement to execute is not necessarily
the next one in sequence. This is called transfer of control.
1) If Statement
It checks whether the condition is true or false and if the condition is true then the if
statement is executed.
Syntax:
if (condition)
{
// Executes this block if condition is true
}
Flowchart:
Example of if statement:
Syntax:
if (condition)
{
// Executes this block if condition is true
}
else
{
// Executes this block if condition is false
}
Flowchart:
Syntax:
if (condition_1)
{
// Executes this block if condition is true
}
else if (condition_2)
{
// Executes this block if condition is true
}
.
.
else
{
// Executes when none of the previous conditions is true
}
Flowchart:
Example of nested-if statement:
public class NestedIfExample {
public static void main(String args[]){
int num=3456;
if(num <100 && num>=1) {
System.out.println("Its a two digit number");
}
else if(num <1000 && num>=100) {
System.out.println("Its a three digit number");
}
else if(num <10000 && num>=1000) {
System.out.println("Its a four digit number");
}
else if(num <100000 && num>=10000) {
System.out.println("Its a five digit number");
}
else {
System.out.println("number is not between 1 & 99999");
}
}
}
Homework:
1) Write down a Java program to find the average of two grads and if the average
greater than or equal 50 then print out the average and "passed".
2) Write down a Java program to check if a number is even or odd.
3) Write down a Java program to find the largest of three numbers.