Posts

Showing posts with the label break

switch statement

The Java switch statement executes one statement from multiple conditions. It is like if-else-if ladder statement. switch statement Syntax switch(expression){ case value1: //code to be executed; break; //optional case value2: //code to be executed; break; //optional ...... default: code to be executed if all cases are not matched; } Program public class switch_example { public static void main(String args[]) { int age = 20; switch(age) { case 10: System.out.println("You are child."); break; case 20: System.out.println("You are teenager."); break; case 30: System.out.println("You are matured."); break; case 40: System.out.println("You are old."); break; default: ...

continue keyword in Java

continue keyword in Java Syntax // condition continue ; Program public class continue_demo { public static void main( String args[]) { for ( int i= 1 ; i<= 10 ; i++) { if (i== 6 ) { continue ; } System.out.println(i); } } } Run Output 1 2 3 4 5 7 8 9 10 Explanation The Java continue statement is used to continue loop. It continues the current flow of the program and skips the remaining code at specified condition. continue keyword in innerloop Program public class continue_inner { public static void main( String args[]) { for ( int i= 1 ; i<= 5 ; i++) { for ( int j= 1 ; j<= 5 ; j++) { if (i== 3 && j== 3 ) { continue ; } ...

break keyword

break keyword in Java Syntax // condition break ; Program public class break_demo { public static void main( String args[]) { for ( int i= 1 ; i<= 10 ; i++) { if (i== 6 ) { break ; } System.out.println(i); } } } Run Output 1 2 3 4 5 Explanation The Java break is used to break loop or switch statement. It breaks the current flow of the program at specified condition. break keyword in inner loop Program public class break_inner { public static void main( String args[]) { for ( int i= 1 ; i<= 5 ; i++) { for ( int j= 1 ; j<= 5 ; j++) { if (i== 3 && j== 3 ) { break ; } System.out.println(i + "-" + j); ...

for loop

In this article we will learn about for loop in Java. 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. Three types of for loop 1. Simple for loop Syntax for (initialization ; condition ; increment/decrement ){ //code to be executed } Program public class For_loop { public static void main( String args[]) { for ( int i= 1 ; i<= 5 ; i++) { System.out.println(i); } } } Run Explanation Here, i=1 is an initialization from where the for loop starts. i<=5 condition where for loop ends. i++ is incrementation which increment the i variable in each iteration. Remember that in this for loop variable i is accessible only through this for loop. Outside of for loop variable i is not accessible. 2. for each loop Syntax for ( Type var : array){ //code to be executed } Pr...