09. Break and Continue MCQ

break/continue Question 1

int a = 0, i = 0, b;
for (i = 0;i < 5; i++)
{
    a++;
    continue;
}

printf("%d", a);
Solution

There is no statement after continue. So its presence makes no difference. It would work just like a normal for loop.

The value of a and i changes similarly.

a value after the loop is its value at the last iteration i.e. when i = 4; loop breaks at i = 5. In the last iteration a = 5.

OUTPUT:
  5

break/continue Question 2

int a = 0, i = 0, b;
for (i = 0;i < 5; i++)
{
  a++;
  if (i == 3)
    break;
}

print("%d", a);
Solution

The for loop runs until i is 3; after that it breaks out of the loop.

4

break/continue Question 3

int i = 0, j = 0;
for (i = 0;i < 5; i++)
{
  for (j = 0;j < 4; j++)
    if (i > 1)
      break;
  printf("Hi \n");
}
Solution

The i loop runs for i in {0, 1, 2, 3, 4}. The j loops runs for j in {0, 1, 2, 3}. The loop doesn't impact how many times hi is printed.

The break statement only breaks the j loop which does not stops the i loop from printing "Hi".

So "Hi" is printed as many times as i loop runs; 5 times.

OUTPUT:
  Hi
  Hi
  Hi
  Hi
  Hi

break/continue Question 4

int i = 0;
for (i = 0;i < 5; i++)
  if (i < 4)
  {
    printf("Hello");
    break;
  }
Solution

The i loop runs for i in {0, 1, 2, 3, 4}.

As soon as the if statement is true. It prints "Hello" and breaks out of the i loop.

The if statement is true in the first iteration i.e. i = 0.

So "Hello" is printed only once.

OUTPUT:
  Hello

Last updated