Java Control Statements

Java Control Statements:

Types of java control statements:

(A). Decision Making statements in java:

  1. The if statement: The if statement is used to execute a block of code if a certain condition is true.
				
					int x = 10;
if (x > 5) {
System.out.println("x is greater than 5");
}

				
			

In the above example, the if statement checks if the value of x is greater than 5. If the condition is true, the block of code inside the if statement is executed.

The if statement can also be used with an else statement to execute a block of code if the condition is false.

				
					int x = 2;
if (x > 5) {
System.out.println("x is greater than 5");
} else {
System.out.println("x is less than or equal to 5");
}

				
			

In the above example, the if statement checks if the value of x is greater than 5. If the condition is false, the block of code inside the else statement is executed.

 

Types of if statements :

In Java, there are four types of if statements:

  1. Simple if statement: The simple if statement is used to execute a block of code if a certain condition is true.
				
					int x = 10;
if (x > 5) {
System.out.println("x is greater than 5");
} 

				
			
  1. if-else statement: The if-else statement is used to execute one block of code if the condition is true, and another block of code if the condition is false.
				
					int age = 20;
if (age >= 18) {
System.out.println("You are eligible to vote");
} else {
System.out.println("You are not eligible to vote");
}

				
			
  1. else-if statement: The else-if statement is used to execute a block of code if the first condition is false and the second condition is true.
				
					int x = 10;
if (x < 5) {
System.out.println("x is less than 5");
} else if (x < 10) {
System.out.println("x is less than 10");
} else {
System.out.println("x is greater than or equal to 10");
} 

				
			

In the above example, the first condition is false, so the program checks the second condition. The second condition is also false, so the block of code inside the else statement is executed.

  1. Nested if statement: The nested if statement is used to execute an if statement inside another if
				
					int x = 10;
int y = 5;
if (x > 5) {
   if (y > 2) {
System.out.println("x is greater than 5 and y is greater than 2");
   }
}

				
			

In the above example, the outer if statement checks if x is greater than 5. If the condition is true, the inner if statement checks if y is greater than 2. If both conditions are true, the block of code inside the inner if statement is executed.

  1. The switch statement: The switch statement is used to execute a block of code depending on the value of a variable.
				
					int day = 2;
switch (day) {
   case 1:
System.out.println("Sunday");
break;
   case 2:
System.out.println("Monday");
break;
   // and so on...
}

				
			

In the above example, the switch statement checks the value of the day variable and executes the corresponding block of code. If day is equal to 1, the output will be “Sunday”. If day is equal to 2, the output will be “Monday”, and so on.

These decision-making statements are essential for creating complex programs by allowing the program to make decisions based on certain conditions.

(B). Loops statements in java:

In Java, there are three types of loop statements:

  1. for loop: The for loop is used to execute a block of code a specific number of times.
				
					for (int i = 1; i<= 10; i++) {
System.out.println(i);
}

				
			

In the above example, the for loop is used to print the numbers from 1 to 10.

  1. while loop: The while loop is used to execute a block of code as long as the condition is true.
				
					int i = 1;
while (i<= 10) {
System.out.println(i);
i++;
}

				
			

In the above example, the while loop is used to print the numbers from 1 to 10.

  1. do-while loop: The do-while loop is used to execute a block of code at least once, and then as long as the condition is true.
				
					int i = 1;
do {
System.out.println(i);
i++;
} while (i<= 10);

				
			

In the above example, the do-while loop is used to print the numbers from 1 to 10.

Loop statements can also be nested, meaning that you can use a loop statement inside another loop statement.

				
					for (int i = 1; i<= 3; i++) {
   for (int j = 1; j <= 3; j++) {
System.out.println(i + " " + j);
   }
} 

				
			

In the above example, the outer for loop is executed three times, and the inner for loop is executed nine times (three times for each iteration of the outer for loop).

 

(C). Jump Statements:

In Java, there are three types of jump statements:

  1. break statement: The break statement is used to exit a loop or switch statement.
				
					for (int i = 1; i<= 10; i++) {
   if (i == 5) {
break;	
   }
System.out.println(i);
}

				
			

In the above example, the for loop is used to print the numbers from 1 to 10. However, the if statement inside the loop checks if i is equal to 5. If the condition is true, the break statement is executed, and the loop is exited.

  1. continue statement: The continue statement is used to skip the current iteration of a loop.
				
					for (int i = 1; i<= 10; i++) {
   if (i == 5) {
continue;
   }
System.out.println(i);
}

				
			

In the above example, the for loop is used to print the numbers from 1 to 10. However, the if statement inside the loop checks if i is equal to 5. If the condition is true, the continue statement is executed, and the current iteration of the loop is skipped.

  1. return statement: The return statement is used to exit a method and return a value.
				
					public static int add(int a, int b) {
   return a + b;
}

				
			

In the above example, the add method is used to add two numbers and return the result. When the return statement is executed, the method is exited, and the result is returned to the calling code.

Jump statements can also be labeled, meaning that you can use a label to specify which loop or switch statement you want to exit or continue.

				
					for (int i = 1; i<= 3; i++) {
   for (int j = 1; j <= 3; j++) {
      if (i == 2 && j == 2) {
         break outer;
      }
System.out.println(i + " " + j);
   }
}

				
			

In the above example, the outer for loop is labeled as outer. The inner for loop checks if i is equal to 2 and j is equal to 2. If the condition is true, the break statement is executed with the label outer, and both loops are exited.

Join To Get Our Newsletter
Spread the love