Java Loops & Conditions

There are basically two types of Control Structures. They are the Decision Control structures and Repetition Control structures. These structures determines the order in which the statements in the program are executed.


Decision Control structures:

These structures executes a specific block of code if the given condition is true. There are three different types of decision control structures. They are the following.

  1. If statement
  2. if-else statement
  3. if-else if statement

 

If Statement:

This statement will execute a specific piece or block of code only when the given boolean statement returns true inside the brackets.

Sample Program:


public class LoopsandConditions {

	public static void main(String[] args) {
	  		
	  int a = 20;
	  
	  // If there is only one statement under if, then no need of flower brackets.
	  if(a == 20)
            System.out.println("a is equal to Twenty");
	  
	  if(a < 30){
            System.out.println("a is equal to " + a);
            System.out.println("a is less than 30"); 	  
          }
      
          // The below condition returns false and the statements inside it are not executed.
 	  if(a > 30){
            System.out.println("Is a greater than 30?");
	    System.out.println("Really?");
	  }    
	}
}

Output:

a is equal to Twenty
a is equal to 20
a is less than 30

 

If-else Statement:

This is similar to If statement, and the only difference is that if the boolean value is false this statement will execute the piece of code in the else part.

Sample Program:


public class LoopsandConditions {

	public static void main(String[] args) {
	  
		
	  int a = 20;	  
	  if(a == 30){
            System.out.println("a is equal to 30");		
	  } else {
	    System.out.println("a is not equal to 30");	
	  }
	}
}

Output:

a is not equal to 30

 

If-else if Statement:

This statement will be having additional else if condition to check and executes its piece of code if true is returned. We can have n number of else if statements.

Sample Program:


public class LoopsandConditions {

	public static void main(String[] args) {
	  
		
	  int a = 20;	  
	  if(a == 30){
	    System.out.println("a is equal to 30");		
	  } else if(a == 20){
	    System.out.println("a is equal to 20");	
	  } else if(a == 40){
	    System.out.println("a is equal to 40");	
	  } else {
	    System.out.println("a is some value");
	  }
	}
}

Output:

a is equal to 20

 

If-else can be used in another if-else, if-else if, if-else if else statements as well. This is said to be Nested If else statements. It can be used in any combinations.

 

Switch Statement:

Switch statement is used to offer multiple blocks of code within different types of case statements to execute based on the value passed through the switch. Switch initially evaluates the value and executes block of code inside the matching case statement.

If the case value is matched with the passed switch value, then the block of code inside the case statement is keep on executed and the succeeding cases are also executed. To avoid this, Break statement needs to be added.

If none of the cases matches the values then, the code inside the default statement is executed.

The value defined in the case must be equal to the datatype of the variable present in the switch.

Sample Program:


public class LoopsandConditions {
	public static void main(String[] args) {
	  
	  System.out.println("Output for value - 1");
	  execute(1);
	  System.out.println("Output for value - 4");
	  execute(4);
	  System.out.println("Output for value - 46");
	  execute(46);	  
	}	
	
	private static void execute(int b){	  
		switch(b){		
		case 1:
		case 2:
		  System.out.println("I may be 1 or 2");		  
		case 3:
		  System.out.println("I am 3");
		  break;
		case 4:
		  System.out.println("I am 4");
		  break;	
		default:
		  System.out.println("I am default");		
		}		
	}
}

Output:

Output for value – 1
I may be 1 or 2
I am 3
Output for value – 4
I am 4
Output for value – 46
I am default

 

Repetition Control structures:

These structures executes a specific block of code until it meets the given criteria. There are three different types of repetition control structures. They are the following.

  1. while loop
  2. do-while loop
  3. for loop

 

While loop:

While loop is used to execute certain block of code until the condition defined in it is satisfied.

Sample Program:


public class LoopsandConditions {
	public static void main(String[] args) {
		int a = 10;
		while(a<16){
		  System.out.println("a is " + a);
			
		  // Need to increment the value of a, else infinite loop occurs
		  a++;			
		}	  
	}	
}

Output:

a is 10
a is 11
a is 12
a is 13
a is 14
a is 15

 

do while loop:

This one is similar to while loop. The only difference is that we will be having one more statement called ‘do’ which will be having the block of code and it executes atleast once before the loop starts.

Sample Program:


public class LoopsandConditions {
	public static void main(String[] args) {
		int a = 16;
		do {
		  System.out.println("a is " + a);
			
		  // Need to increment the value of a, else infinite loop occurs
		  a++;			
		}while(a<16);  
	}	
}

Output:

a is 16

Note: Even though the condition inside the loop fails, the do statement executed the block of code inside it once.

 

For loop:

For loop is similar to the while loop. It also allows to execute a block of code a number of times. The below program is equivalent to the while loop program given above.

Sample Program:


public class LoopsandConditions {
	public static void main(String[] args) {
		int a;
		for(a=10; a<16;a++){
		  System.out.println("a is " + a);
		}
	}	
}

Output:

a is 10
a is 11
a is 12
a is 13
a is 14
a is 15

Posted on July 1, 2014 in Java for Beginners

Share the Story

Response (1)

  1. Arsla
    February 17, 2016 at 10:23 pm · Reply

    How To Choose Fantastic beat ! I would like to apprentice while you amend your wiesbte, how can i subscribe for a blog site? The account helped me a acceptable deal. I had been a little bit acquainted of this your broadcast provided bright clear idea

Leave a reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to Top