Aspire Faculty ID #13650 · Topic: JAMIA MILLIA ISLAMIA MCA 2023 · Just now
JAMIA MILLIA ISLAMIA MCA 2023

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`



Previous 10 Questions — JAMIA MILLIA ISLAMIA MCA 2023

Nearest first

Next 10 Questions — JAMIA MILLIA ISLAMIA MCA 2023

Ascending by ID
Ask Your Question or Put Your Review.

loading...