Control Statement in Java :
The Java compiler runs the code from top to bottom. The statements in the code are executed in the order that they occur in the code. Java, on the other hand, includes statements for controlling the flow of Java code.
hierarchy of control statement |
Decision Making Statements :
Decision-making statements, as the title indicates, decide which statements to execute and when. Decision-making statements evaluate the Boolean expression and control the flow of the program based on the condition's result. In Java, there are 2 types of decision-making statements: If and switch statements.
1. if statement :
To evaluate a condition in Java, use the "if" statement. Depending on the circumstances, the program's control is diverted. The If statement's condition returns a Boolean value, either true or false.
Example :package basicProgram;
public class Testclass
{
public static void main(String[] args)
{
int a=10,b=15;
if(b>a)
{
System.out.println(" b is greater than a");
}
}
}
Output: b is greater than a
package basicProgram;
public class Testclass
{
public static void main(String[] args)
{
int a=10,b=15;
if(b>a)
{
System.out.println(" b is greater than a");
}
}
}
Output: b is greater than a
1.1. if-else statement :
The if-else is an extension to the if-statement, which uses another block of code, i.e., else block. The else block is executed if the condition of the if-block is evaluated as false.
Example :package basicProgram;
public class TestClass
{
public static void main(String[] args)
{
int a=10,b=15;
if(b>a)
{
System.out.println(" b is greater than a");
}
else
{
System.out.println(" a is greater than b");
}
}
}
Output: a is greater than b
package basicProgram;
public class TestClass
{
public static void main(String[] args)
{
int a=10,b=15;
if(b>a)
{
System.out.println(" b is greater than a");
}
else
{
System.out.println(" a is greater than b");
}
}
}
Output: a is greater than b
1.2.if else-if else statement :
The if-else-if statement is made up of an if statement and several else-if statements. In other words, a decision tree is created by a sequence of if-else statements in which the compiler can enter the block of code where the condition is true. At the end of the chain, we may also define an else statement.
Example :package basicProgram;
public class TestClass
{
public static void main(String[] args)
{
int a=10,b=10;
if(b>a)
{
System.out.println(" b is greater than a");
}
else if(b<a)
{
System.out.println(" a is greater than b");
}
else
{
System.out.println("Both a and b are same");
}
}
}
Output : Both a and b are same
package basicProgram;
public class TestClass
{
public static void main(String[] args)
{
int a=10,b=10;
if(b>a)
{
System.out.println(" b is greater than a");
}
else if(b<a)
{
System.out.println(" a is greater than b");
}
else
{
System.out.println("Both a and b are same");
}
}
}
Output : Both a and b are same
2. switch statement :
Switch statements in Java are similar to if-else-else statements. A single case is run based on the variable that is being switched in the switch statement, which contains additional blocks of code called cases. Instead of using if-else-if statements, you can use the switch statement. It also improves the program's readability.
There are a few things to keep in mind about the switch statement:
- Integers, string, shorts, bytes, chars, and enumerations can all be used as case variables.
- Cases cannot be duplicate.
- When any of the cases does not match the value of expression, the default statement is executed. It is optional.
- When the condition is satisfied, the break statement terminates the switch block. If it isn't used, the next case is run.
- We must remember that the case expression will be of the same type as the variable when using switch statements.
Example :package basicProgram;
public class SwitchCaseProgram
{
public static void main(String[] args)
{
int day=4;
switch (day)
{
case 1:
System.out.println("Day is Sunday ");
break;
case 2:
System.out.println("Day is Monday ");
break;
case 3:
System.out.println("Day is Tuesday ");
break;
case 4:
System.out.println("Day is Wednesday ");
break;
case 5:
System.out.println("Day is Thursday ");
break;
case 6:
System.out.println("Day is Friday ");
break;
case 7:
System.out.println("Day is Saturday ");
break;
default:
System.out.println("Invalid Day ");
break;
}
}
}
Output: Day is Wednesday
package basicProgram;
public class SwitchCaseProgram
{
public static void main(String[] args)
{
int day=4;
switch (day)
{
case 1:
System.out.println("Day is Sunday ");
break;
case 2:
System.out.println("Day is Monday ");
break;
case 3:
System.out.println("Day is Tuesday ");
break;
case 4:
System.out.println("Day is Wednesday ");
break;
case 5:
System.out.println("Day is Thursday ");
break;
case 6:
System.out.println("Day is Friday ");
break;
case 7:
System.out.println("Day is Saturday ");
break;
default:
System.out.println("Invalid Day ");
break;
}
}
}
Output: Day is Wednesday
loop statements :
In programming, we may need to run a block of code several times while particular conditions are true. Loop statements are used to repeatedly execute a series of instructions. The set of instructions will only be executed if a certain condition is true.
1. for loop :
In a single line of code, we may initialize the loop variable, verify the condition, and increment/decrement. We only use the for loop when we know exactly how many times we want to run a block of code.
Example :package basicProgram;
public class HelloWorld
{
public static void main(String[] args)
{
for (int num=1; num<=5; num++)
{
System.out.println(num);
}
}
}
Output : 1
2
3
4
5
package basicProgram;
public class HelloWorld
{
public static void main(String[] args)
{
for (int num=1; num<=5; num++)
{
System.out.println(num);
}
}
}
Output : 1
2
3
4
5
2. for each loop :
To explore data structures such as arrays or collections, Java provides an improved for loop. The loop variable does not need to be updated in the for-each loop.
Example :package basicProgram;
public class HelloWorld
{
public static void main(String[] args)
{
int[] arr= {1,2,3,4,5};
for (int i : arr)
{
System.out.println(i);
}
}
}
Output : 1
2
3
4
5
package basicProgram;
public class HelloWorld
{
public static void main(String[] args)
{
int[] arr= {1,2,3,4,5};
for (int i : arr)
{
System.out.println(i);
}
}
}
Output : 1
2
3
4
5
3. while loop :
The while loop can also be used to repeatedly iterate over a set of statements. If we don't know the number of iterations, then we should use a while loop. Unlike for loop, the initialization and increment/decrement doesn't take place inside the loop statement in while loop. Because the condition is tested at the start of the loop, it's also known as the entry-controlled loop. The loop body will be executed if the condition is true; else, the statements after the loop will be executed.
Example :package basicProgram;
public class HelloWorld
{
public static void main(String[] args)
{
int i=1;
while (i<=5)
{
System.out.println(i);
i++;
}
}
}
Output : 1
2
3
4
5
package basicProgram;
public class HelloWorld
{
public static void main(String[] args)
{
int i=1;
while (i<=5)
{
System.out.println(i);
i++;
}
}
}
Output : 1
2
3
4
5
4. do while loop :
After executing the loop statements, the do-while loop checks the condition at the end of the loop. We can use a do-while loop when the number of iterations is unknown and we need to run the loop at least once.
Example :package basicProgram;
public class HelloWorld
{
public static void main(String[] args)
{
int i=1;
do
{
System.out.println(i);
i++;
}
while (i<=5);
}
}
Output : 1
2
3
4
5
package basicProgram;
public class HelloWorld
{
public static void main(String[] args)
{
int i=1;
do
{
System.out.println(i);
i++;
}
while (i<=5);
}
}
Output : 1
2
3
4
5
Jump Statements :
Jump statements are used to transfer the control of the program to the specific statements. In other words, jump statements transfer the execution control to the other part of the program. There are two types of jump statements in Java, i.e., break and continue.
1. break :
the break statement is used to break the current flow of the program and transfer the control to the next statement outside a loop or switch statement.
Example :package basicProgram;
public class HelloWorld
{
public static void main(String[] args)
{
for (int i = 1; i <= 5; i++)
{
if(i==4)
{
break;
}
System.out.println(i);
}
}
}
Output : 1
2
3
package basicProgram;
public class HelloWorld
{
public static void main(String[] args)
{
for (int i = 1; i <= 5; i++)
{
if(i==4)
{
break;
}
System.out.println(i);
}
}
}
Output : 1
2
3
2. continue :
Unlike break statement, the continue statement doesn't break the loop, whereas, it skips the specific part of the loop and jumps to the next iteration of the loop immediately..
Example :package basicProgram;
public class HelloWorld
{
public static void main(String[] args)
{
for (int i = 1; i <= 5; i++)
{
if(i==4)
{
continue;
}
System.out.println(i);
}
}
}
Output : 1
2
3
5
package basicProgram;
public class HelloWorld
{
public static void main(String[] args)
{
for (int i = 1; i <= 5; i++)
{
if(i==4)
{
continue;
}
System.out.println(i);
}
}
}
Output : 1
2
3
5