Aspire's Library

A Place for Latest Exam wise Questions, Videos, Previous Year Papers,
Study Stuff for MCA Examinations - NIMCET

Previous Year Question (PYQs)



What is the output of the following C code segment?

int i;
for(i = 0; i <= 2; i++)
{
    switch(i)
    {
        case 1: printf("%2d", i);
        case 2: printf("%2d", i); continue;
        default: printf("%2d", i);
    }
}





Solution

Solution:

Let’s analyze iteration by iteration —

**Iteration 1:** `i = 0`  
→ `switch(0)` → goes to `default:` → prints `0`.

**Iteration 2:** `i = 1`  
→ executes `case 1:` → prints `1`  
→ no `break`, so it falls through to `case 2:` → prints `1`  
→ encounters `continue;` → skips remaining statements in the `for` loop body and proceeds to next iteration.

**Iteration 3:** `i = 2`  
→ executes `case 2:` → prints `2`  
→ `continue;` moves control to the next iteration, but loop ends because `i <= 2` condition fails.

Final output:  
`0 1 1 2`





Online Test Series,
Information About Examination,
Syllabus, Notification
and More.

Click Here to
View More


Online Test Series,
Information About Examination,
Syllabus, Notification
and More.

Click Here to
View More

Ask Your Question or Put Your Review.

loading...