Find the output-
public class Test3 {
public static void main(String[] args)
{
int s=5;
for(int i=2;i<7; i++)
{
if(i==3)
continue;
if(i==5)
break;
S=S+1;
}
System.out.println(s);
}
Answers
Answer:
7
Explanation:
I suppose you are familiar with the continue and break statements. The continue statement goes on for the next iteration in the loop, ignoring the rest of the statements, while the break statement bis used to abruptly and immediately exit from the loop. So we are starting with s with a value of 5. So for the for loop, I shall explain what's happening inside the loop for each iteration.
For i = 2,
S will increase by 1, becoming 6, since 2 is neither equal to 3 nor equal to 5
For i = 3,
The control will encounter the if statement where if i = 3, the loop will continue. This means that the control will ignore the rest of the statements and move on for the next iteration. So, the value of S remains same since the statement S = S+1 is ignored.
For i = 4,
S will increase by 1, becoming 7, since 4 is neither equal to 3 nor equal to 5
For i = 5,
The control will encounter the if statement where if i = 5, the loop will break. So, at this stage, the control immediately exits from the loop and goes on to print S as 7.
Hope it helps!