Sunday, February 1, 2015

Java program to understand the continue statement

The program given below will print the numbers between 0-50 skipping the numbers between 11-39. There is a for loop with the condition i<=50; to print the numbers from 0-50, but inside the for loop there is an if statement with the condition i>10&&i<40. Inside the if statement there is another statement continue;.

That's why the numbers from 11 to 39 will be skipped depending on the condition i>10&&i<40. Actually after printing the number till 10, the program will not print the next numbers continuously till it found 40 in the loop.
public class ContinueTest{
 public static void main(String [] args){
  //for loop to print the number between 0-50
  for(int i=0; i<=50; i++){
   //if statement ti check the condition
   if(i>10&&i<40){
    //statement to continue the loop depending on the condition
    continue;
   }
   //displaying the numbers according to the conditions
   System.out.print(i+", ");
  }
 }
}


No comments:

Post a Comment