Control Statements
The control statements are used to control the flow of execution of the program. Java contains the following
Types of control statements:
1) Selection Statements / Decision making statements: if, if-else, switch.
2) Repetition Statements / Looping Statements: while, do-while, for.
3) Branching Statements / Jumping Statements: break, continue, and return.
Selection Statements / Decision making statements: As the name suggests, decision-making statements decide
which statement to execute and when. Decision making statements evaluate the Boolean expression and control
the program flow depending upon the result of the condition provided.
There are two types of decision making statements in Java, i.e.
1. If statement
2. switch statement.
1) If Statement: In Java, the "if" statement is used to evaluate a condition. The control of the program is diverted
depending upon the specific condition. The condition of the If statement gives a Boolean value, either true or false.
In Java, there are four types of if statements given below.
1. Simple if statement
2. if-else statement
3. Nested if-else statement
4.else if ladder
SIMPLE IF STATMENT Syntax:
If(expression)
{
Statements;
}
Java program to illustrate If statement
class IfDemo
{
public static void main(String args[])
{
int i = 10;
if (i > 15) {
System.out.println("10 is less than 15");}
System.out.println("I am Not in if");
}
}
Output: I am Not in if
, THE IF...ELSE STATEMENT
The if. ..else statement is an extension of the simple if statement.
Syntax
if(test expression)
{
True block statement(s)
}
Else
{
False block statement(s)
}
Statement x;
If the test expression is true, then the true-block statement (s) immediately following the if statement, are executed;
otherwise, the false -block statement (s) are executed. In either case, either true– block or false – block will be
executed, not both.
Java program to illustrate if-else statement
class IfElseDemo {
public static void main(String args[])
{
int i = 10;
if (i < 15)
System.out.println("i is smaller than 15");
Else
System.out.println("i is greater than 15");
}
}
Output: i is smaller than 15
NESTING O IF ELSE STATEMENTS
When a series of decisions are involved, we may have to use more than one if…else statement in nested form
The control statements are used to control the flow of execution of the program. Java contains the following
Types of control statements:
1) Selection Statements / Decision making statements: if, if-else, switch.
2) Repetition Statements / Looping Statements: while, do-while, for.
3) Branching Statements / Jumping Statements: break, continue, and return.
Selection Statements / Decision making statements: As the name suggests, decision-making statements decide
which statement to execute and when. Decision making statements evaluate the Boolean expression and control
the program flow depending upon the result of the condition provided.
There are two types of decision making statements in Java, i.e.
1. If statement
2. switch statement.
1) If Statement: In Java, the "if" statement is used to evaluate a condition. The control of the program is diverted
depending upon the specific condition. The condition of the If statement gives a Boolean value, either true or false.
In Java, there are four types of if statements given below.
1. Simple if statement
2. if-else statement
3. Nested if-else statement
4.else if ladder
SIMPLE IF STATMENT Syntax:
If(expression)
{
Statements;
}
Java program to illustrate If statement
class IfDemo
{
public static void main(String args[])
{
int i = 10;
if (i > 15) {
System.out.println("10 is less than 15");}
System.out.println("I am Not in if");
}
}
Output: I am Not in if
, THE IF...ELSE STATEMENT
The if. ..else statement is an extension of the simple if statement.
Syntax
if(test expression)
{
True block statement(s)
}
Else
{
False block statement(s)
}
Statement x;
If the test expression is true, then the true-block statement (s) immediately following the if statement, are executed;
otherwise, the false -block statement (s) are executed. In either case, either true– block or false – block will be
executed, not both.
Java program to illustrate if-else statement
class IfElseDemo {
public static void main(String args[])
{
int i = 10;
if (i < 15)
System.out.println("i is smaller than 15");
Else
System.out.println("i is greater than 15");
}
}
Output: i is smaller than 15
NESTING O IF ELSE STATEMENTS
When a series of decisions are involved, we may have to use more than one if…else statement in nested form