How to Java programming- Control statements

This guide explains you about Java control statements. The Java statements were executed line by line from the top to bottom in an order. If we want any statements skipped or a statement executed more than once, we will achieve this using control structures. There are 3 types of control statements.

1.Decision-making statements,
2.Repetition statements,
3. Branching statements.

 

1.Decision-making statements,

The Java if statement tests the condition. It executes the if block if the condition is true.

Java IF-else-if ladder Statement

The if-else-if ladder statement executes one condition from multiple statements.

If else if ladder
	public static void main(String[] args){
		 int marks = 65;
	 
		if(marks<50){
			System.out.println("fail"); 
		 } else if(marks>=50 8& marks<60){ 
			System.out.println("D grade"); 
		 } else if(marks>=60 88 marks<70){
			System.out.println("C grade"); 
		 } else if(marks>=70 88 marks<80){
			System.out.println("B grade"); 
		 } else if(marks>=80 88 marks<90){
			System.out.println("A grade"); 
		 } else if(marks>=90 88 marks<100){
			System.out.println("A+ grade"); 
		 }else{ System.out.println("Invalid!"); 
                 }
	}
Output
     C grade

 

Java Switch Statement.

The Java switch statement executes one statement from multiple conditions. It is like if-else-if ladder statement.The java switch statement is fall-through. It means it executes all statement after the first match if a break statement is not used with switch cases.

Switch Statement
package FirstPackage; 
public class MyFirstClass {
	/* si 
	public static void main(String[] args) { 
	// TODO Auto-generated method stub 
		int number=20; 
		switch(number){
			case 10:
				System.out.println("10");
			break; 
			case 20: 
				System.out.println("20");
			break;
			case 30:
				System.out.println("30");
			break; 
			default:
				System.out.println("Not in 10, 20 or 30"); 
		}
		 
	}
}
Output
     20

The java switch statement is fall-through. It means it executes all statement after the first match if a break statement is not used with switch cases.

 

2.Repetition statements.

Java For Loops.

The Java for loop is used to iterate a part of the program several times. If the number of iteration is fixed, it is recommended to use for loop.

Java Simple For Loop

We can initialize a variable, check the condition and increment/decrement value.

For loop
	package FirstPackage; 
	public class MyFirstClass{
		public static void main(String[] args) {
			// TODO Auto-generated method stub 
			//Simple for loop 
			for(int i=1;i<=5;i++){
				System.out.println(i);
			}
		}
	}
Output
     1
     2
     3
     4
     5
Java For each Loop

The for-each loop is used to traverse array or collection in java. It is easier to use than simple for a loop because we don’t need to increment value and use subscript notation.

For each loop works on elements basis, not index. It returns element one by one in the defined variable.

For each loop
	package FirstPackage; 
	public class MyFirstClass {
		public static void main(String[] args){
			// TODO Auto-generated method stub 
			//Simple for each loop 
			int arr[]={12,23,44,56,78};
			for(int i:arr){ 
				System.out.println(i); 
			} 
		}
	} 

Output

     12
     23
     44
     56
     78

 

Java While Loop

The Java while loop is used to iterate a part of the program several times. If the number of iteration is not fixed, it is recommended to use while loop.

While loop
	package FirstPackage; 
	public class MyFirstClass { 
		public static void main(String[] args) { 
			// TODO Auto-generated method stub 
			//Simple while loop 
			int i=1; 
			while(i<=5){
				System.out.println(i); 
				i++;
			}
		}
	}
Output
     1
     2
     3
     4
     5

 

Java do-while Loop

The Java do-while loop is used to iterate a part of the program several times. If the number of iteration is not fixed and you must have to execute the loop at least once, it is recommended to use a do-while loop.

Do while loop
	public class MyFirstClass { 
		public static void main(String[] args) {
			// TODO Auto-generated method stub 
			//Simple do while loop 
			int 1=1; 
			do{
				System.out.println(i);
				i++; 
			}while(i <= 5); 
		} 
	}
Output
     1
     2
     3
     4
     5

 

3. Branching statements.

Java Break Statement

The Java break is used to break loop or switch statement. It breaks the current flow of the program at specified condition. In the case of an inner loop, it breaks the only inner loop.

Break statement
	public class MyFirstClass {
		public static void main(String[] args) {
			 // TODO Auto-generated method stub 
			 //Break statement 
			 for(int i=1;i<=10;i++){
				if(i==5){
					break; 
				} 
				System.out.println(i); 
			}
		} 
	}
Output
     1
     2
     3
     4

 

Java Continue Statement

The Java continue statement is used to continue the loop. It continues the current flow of the program and skips the remaining code at specified condition. In the case of an inner loop, it continues the only inner loop.

Continue statement
	public class MyFirstClass{
		public static void main(String[] args){
		// T000 Auto-generated method stub 
		//Continue statement 
			for(int i=1;i<=5;i++){
				if(i==3){
					continue;
					System.out.println(i); 
				}	
			}
		}
	}
Output
     1
     2
     3
     4
     5

Anwar Yakkiparamban

Anwar Yakkiparamban is the founder of Lauyou Learning. Prior to Lauyou learning, Anwar worked at ARD Engineering & Development, Qatar. He holds bachelor degree in Electronics and Communication Engineering from Govt. Engineering College Idukki.

You may also like...