Control Statements
Control Statements
A control statement in java is a statement that determines whether the other statements will be executed or
not. It controls the flow of a program. An ‘if’ statement in java determines the sequence of execution
between a set of two statements.
Java’s program control statements can be put into the following categories: selection, iteration, and jump.
1. Selection statements: allow your program to choose different paths of execution based upon the
outcome of an expression or the state of a variable.
2. Iteration statements: enable program execution to repeat one or more statements (that is, iteration
statements form loops).
3. Jump statements: allow your program to execute in a nonlinear fashion.
The if statement determines whether a code should be executed based on the specified condition.
Syntax:
1 if (condition) {
2 Statement 1; //executed if condition is true
3 }
Statement 2; //executed irrespective of the condition
4
Output:
If statement!
Hello World!
If..else statement
In this statement, if the condition specified is true, the if block is executed. Otherwise, the else block is
executed.
Example:
1
2 public class Main
3 {
public static void main(String args[])
4 {
5 int a = 15;
6 if (a > 20)
7 System.out.println("a is greater than 10");
8 else
System.out.println("a is less than 10");
9 System.out.println("Hello World!");
10 }
11 }
12 }
13
Output:
a is less than 10
Hello World!
Nested if statement
An if present inside an if block is known as a nested if block. It is similar to an if..else statement, except
they are defined inside another if..else statement.
Syntax:
1
2 if (condition1) {
3 Statement 1; //executed if first condition is true
4 if (condition2) {
5 Statement 2; //executed if second condition is true
6}
else {
7}Statement 3; //executed if second condition is false
8}
9
Example:
1
2
public class Main
3 {
4 public static void main(String args[])
5 {
6 int s = 18;
if (s > 10)
7 {
8 if (s%2==0)
9 System.out.println("s is an even number and greater than 10!");
10 else
11 System.out.println("s is a odd number and greater than 10!");
}
12 else
13 {
14 System.out.println("s is less than 10");
15 }
16 System.out.println("Hello World!");
}
17 }
18
19
Output:
s is an even number and greater than 10!
Hello World!
Switch statement
A switch statement in java is used to execute a single statement from multiple conditions. The switch
statement can be used with short, byte, int, long, enum types, etc.
Example:
1 public class Music {
public static void main(String[] args)
2 {
int instrument = 4;
3 String musicInstrument;
// switch statement with int data type
4 switch (instrument) {
case 1:
5 musicInstrument = "Guitar";
break;
6 case 2:
musicInstrument = "Piano";
7 break;
case 3:
8 musicInstrument = "Drums";
break;
9 case 4:
musicInstrument = "Flute";
10 break;
case 5:
11 musicInstrument = "Ukelele";
break;
12 case 6:
musicInstrument = "Violin";
13 break;
case 7:
14
musicInstrument = "Trumpet";
break;
15
default:
musicInstrument = "Invalid";
16
break;
}
17
System.out.println(musicInstrument);
18 }
}
19
20
21
22
23
24
25
26
27
28 1
2 public class whileTest
29 3 {
4 public static void main(String args[])
30 5 {
int i = 5;
6 while (i <= 15)
31 7 {
8 System.out.println(i);
32 9 i = i+2;
10 }
11 }
}
12
Output:
Flute
Looping Statements
Statements that execute a block of code repeatedly until a specified condition is met are known
as looping statements. Java provides the user with three types of loops:
While
Known as the most common loop, the while loop evaluates a certain condition. If the condition is
true, the code is executed. This process is continued until the specified condition turns out to be
false.
The condition to be specified in the while loop must be a Boolean expression. An error will be
generated if the type used is int or a string.
Syntax:
Example
1 while (condition)
2{
3 statementOne;
4}
Do..while
The do-while loop is similar to the while loop, the only difference being that the condition in the
do-while loop is evaluated after the execution of the loop body. This guarantees that the loop is
executed at least once.
Syntax:
1
do{
2 //code to be executed
}while(condition);
3
Example:
10
11
12
13
Output:
20
For
The for loop in java is used to iterate and evaluate a code multiple times. When the number of
iterations is known by the user, it is recommended to use the for loop.
Syntax:
for (initialization;
condition;
increment/decrement)
{
statement;
}
Example
1
public class forLoop
2 {
3 public static void main(String args[])
4 {
5 for (int i = 1; i <= 10; i++)
6 System.out.println(i);
}
7
}
8
Output:
5
6
7
8
9
10
Branching Statements
Branching statements in java are used to jump from a statement to another statement, thereby the
transferring the flow of execution.
Break
The break statement in java is used to terminate a loop and break the current flow of the
program.
Example:
1
2 public class Test
3 {
public static void main(String args[])
4 {
5 for (int i = 5; i < 10; i++)
6 {
7 if (i == 8)
8 break;
System.out.println(i);
9 }
10 }
11 }
12
Output:
5
6
7
Continue
To jump to the next iteration of the loop, we make use of the continue statement. This statement
continues the current flow of the program and skips a part of the code at the specified condition.
Example:
1
2 public class Main
3 {
4 public static void main(String args[])
{
5 for (int k = 5; k < 15; k++)
6 {
7 // Odd numbers are skipped
8 if (k%2 != 0)
9 continue;
// Even numbers are printed
10 System.out.print(k + " ");
11 }
12 }
13 }
14
Output:
6 8 10 12 14