3 How many times the for loop in the below program runs 3 po
3. How many times the for loop in the below program runs? (3 points)
int main(){
int j;
for(j=-2;j<=10;j++){
if(j%2==0){
continue;
}
if(j==2){
break;
}
if(j==5){
j+=3;
}
}
return 0;
}
a) 5
b) 13
c) 10
d) 12
Solution
Program:
import java.io.*;
class FL
{
public static void main(String args[])
{
int j;
for(j=-2;j<=10;j++)
{
if(j%2==0)
{
System.out.println(\"j =\"+j);
continue;
}
if(j==2)
{
break;
}
if(j==5)
{
j+=3;
}
}
}
}
Output:
j = -2
j = 0
j = 2
j = 4
j = 10
Answer) Option c) 10 times
Explanation : for loop is initially set j = - 2 it increments it\'s value by 1 until it reaches to 5 i.e value of j up to 5 is [ j= -2 ;-1 ; 0 ; 1 ;2 ; 3 ; 4 ; 5 ] after reaching j value to 5 it is added to 3 then it becomes 8 after that the value of j is incemented by 1 i.e value of j after reaches to 8 is [ j= 9; 10 ] here the total no of times that for loop is executed is 10

