Machine-translated by Google. Questions are authored in English.
Top

Discussion

What is the output of this C code?

void main()

    {

        int i = 0;

        int j = 0;

        for (i = 0;i < 5; i++)

        {

            for (j = 0;j < 4; j++)

            {

                if (i > 1)

                    continue;

                    printf("Hi \n");

            }

        }

    }

 

  • A.Hi is printed 9 times
  • B.Hi is printed 8 times
  • C.Hi is printed 7 times
  • D.Hi is printed 6 times

Answer: B

Correct answer: Hi is printed 8 times

Here the printf is inside the inner loop, and continue skips it. For i equal to 0 and 1 the condition i > 1 is false, so all four inner passes print - \(2 \times 4 = 8\) lines. From i = 2 onwards continue fires on every inner pass and nothing more is printed.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.