unit1 _ Java Control Statements
unit1 _ Java Control Statements
Java
Java compiler executes the code from top to bottom. The statements in
the code are executed according to the order in which they appear.
However, Java provides statements that can be used to control the flow of
Java code. Such statements are called control flow statements. It is one of
the fundamental features of Java, which provides a smooth flow of
program.
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., If statement and 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. if-else-if ladder
4. Nested if-statement
1) Simple if statement:
It is the most basic statement among all control flow statements in Java. It
evaluates a Boolean expression and enables the program to enter a block
of code if the expression evaluates to true.
1. if(condition) {
2. statement 1; //executes when condition is true
3. }
Student.java
Student.java
Output:
x + y is greater than 20
2) if-else statement
1. if(condition) {
2. statement 1; //executes when condition is true
3. }
4. else{
5. statement 2; //executes when condition is false
6. }
Student.java
Output:
x + y is greater than 20
3) if-else-if ladder:
1. if(condition 1) {
2. statement 1; //executes when condition 1 is true
3. }
4. else if(condition 2) {
5. statement 2; //executes when condition 2 is true
6. }
7. else {
8. statement 2; //executes when all the conditions are false
9. }
Student.java
Output:
Delhi
4. Nested if-statement
1. if(condition 1) {
2. statement 1; //executes when condition 1 is true
3. if(condition 2) {
4. statement 2; //executes when condition 2 is true
5. }
6. else{
7. statement 2; //executes when condition 2 is false
8. }
9. }
Student.java
Output:
Delhi
Switch Statement:
In Java, Switch statements are similar to if-else-if statements. The switch
statement contains multiple blocks of code called cases and a single case
is executed based on the variable which is being switched. The switch
statement is easier to use instead of if-else-if statements. It also enhances
the readability of the program.
1. switch (expression){
2. case value1:
3. statement1;
4. break;
5. .
6. .
7. .
8. case valueN:
9. statementN;
10. break;
11. default:
12. default statement;
13. }
Student.java
Output:
While using switch statements, we must notice that the case expression
will be of the same type as the variable. However, it will also be a
constant value. The switch permits only int, string, and Enum type
variables to be used.
Loop Statements
In programming, sometimes we need to execute the block of code
repeatedly while some condition evaluates to true. However, loop
statements are used to execute the set of instructions in a repeated order.
The execution of the set of instructions depends upon a particular
condition.
1. for loop
2. while loop
3. do-while loop
Calculation.java
Output:
Calculation.java
Output:
Java
C
C++
Python
JavaScript
1. while(condition){
2. //looping statements
3. }
The flow chart for the while loop is given in the following image.
Calculation .java
Output:
0
2
4
6
8
10
1. do
2. {
3. //statements
4. } while (condition);
The flow chart of the do-while loop is given in the following image.
Consider the following example to understand the functioning of the do-
while loop in Java.
Calculation.java
Output:
BreakExample.java
Output:
0
1
2
3
4
5
6
break statement example with labeled for loop
Calculation.java
Output:
0
1
2
3
4
5
Output:
0
1
2
3
5
1
2
3
5
2
3
5