1.

Explain the function of each of the following with an example: (i) break; (ii) continue;

Answer»

(i) A break statement terminates the current loop and proceeds to the first statement that follows the loop. For example: boolean prime=true; for(int i=2; i<n; i++)

{

prime=(n%i==0); 

if (prime) break; 

}

(ii) A continue statement transfers the control to the next iteration leaving all the other statement unexecuted that follows after the continue statement. 

For example : 

for(int i=1; i<20; i++) 

if(i%2==0) 

continue; 

System.out.println(i); 

The above loop will print all the odd numbers



Discussion

No Comment Found

Related InterviewSolutions