Saturday, January 31, 2015

Java program to understand the break operation

The java program given below will print the numbers from 0-9, though in the for-loop there is given i<=50. According this condition, the program should print the numbers from 0-50.

But inside the for-loop there is an if statement with the condition i==10, that means when the value of i will 10 then the statement break; will be executed.

That's why after printing the numbers from 0-9 the loop will be stopped, though in the loop I have told to print the numbers from 0-50.
break operation,java program,break operation in java

//java program to understand the break operation
public class breakOp{
 public static void main(String [] args){
  //for loop to print 0-50
  for(int i=0; i<=50; i++){
   //if statement to check the value of i
   if(i==10){
    //stopping the loop if the condition is true
    break;
   }
   //displaying the data stored in i
   System.out.print(i+", ");
  }
 }
}


No comments:

Post a Comment