🔀 Control Flow & Output
Why it matters: Tracing a loop, a switch without break, or a short-circuited condition by hand is the core skill behind most C output questions.
Subtopics
Each subtopic has its own page: idea, why, how, example, play, check. Mark each one understood when it clicks. 0 of 4 done.
1if/else and the three loops
2switch and fall-through
3Short-circuit evaluation and ?:
4printf, return values and format specifiers
Formulas
Iterations of for(i=a; i<b; i+=s)
⌈(b − a) / s⌉ (when b > a, s > 0)
Halving loop for(i=n; i>1; i/=2)
⌊log₂ n⌋ iterations
Doubling loop for(j=1; j<n; j*=2)
⌈log₂ n⌉ iterations
Nested loops, inner depends on outer
sum the inner counts: Σ over i of (inner count)
Triple loop i ≤ j < k ≤ n
C(n+1, 3) = (n−1)·n·(n+1) / 6
Switch fall-through
output = every statement from the matched label to the first break
What GATE asks from this
Question types seen repeatedly in past papers. More stars means more frequent.
★★★
Trace a loop with integer or short-circuit side effects
Keep a table of the variables after each iteration; watch out for i++ inside &&.
★★★
How many times is a statement executed?
Count iterations of nested loops or fork-style repeats.
★★★
Does this loop terminate?
Look at whether the loop variable can reach the exit value (for example integer division stuck at 1).
Ratings are approximate, based on recurring patterns in past GATE CSE papers.
Previous year questions
10 real questions from the official GATE CSE papers (2013 to 2025), newest first, each with the official answer key and a worked solution. Questions are the property of the GATE organising institutes; this copy is for your personal study.
10 questions · 0 correct of 0 attempted · marks 0 / 18
Year:
GATE 2025 CS2 · Q33NAT1 mark
Consider the following C code segment.
int x=126,y=105;
do {
if(x>y) x=x-y;
else y=y-x;
} while(x!=y);
printf("%d",x);The output of the given C code segment is ______.GATE 2025 CS1 · Q63NAT2 marks
Consider the following C program:
#include <stdio.h>
int gate (int n) {
int d, t, newnum, turn;
newnum = turn = 0; t=1;
while (n>=t) t *= 10;
t /=10;
while (t>0) {
d = n/t;
n = n%t;
t /= 10;
if (turn) newnum = 10*newnum + d;
turn = (turn + 1) % 2;
}
return newnum;
}
int main () {
printf ("%d", gate(14362));
return 0;
}The value printed by the given C program is ______.GATE 2024 CS1 · Q18MCQ1 mark
Consider the following C program:
#include <stdio.h>
int main(){
int a = 6;
int b = 0;
while(a < 10) {
a = a / 12 + 1;
a += b;
}
printf("%d", a);
return 0;
}Which one of the following statements is CORRECT?GATE 2021 Set 1 · Q37MCQ2 marks
Consider the following ANSI C program.
#include <stdio.h>
int main()
{
int i, j, count;
count = 0;
i = 0;
for (j = -3; j <= 3; j++)
{
if ((j >= 0) && (i++))
count = count + j;
}
count = count + i;
printf("%d", count);
return 0;
}Which one of the following options is correct?GATE 2021 Set 1 · Q48NAT2 marks
Consider the following ANSI C function:
int SimpleFunction(int Y[], int n, int x)
{
int total = Y[0], loopIndex;
for (loopIndex = 1; loopIndex <= n - 1; loopIndex++)
total = x * total + Y[loopIndex];
return total;
}Let Z be an array of 10 elements with Z[i] = 1, for all i such that 0 ≤ i ≤ 9. The value returned by SimpleFunction(Z, 10, 2) is ______.GATE 2019 Set 2 · Q52NAT2 marks
Consider the following C program:
#include <stdio.h>
int main(){
float sum = 0.0, j = 1.0, i = 2.0;
while (i/j > 0.0625){
j = j + j;
sum = sum + i/j;
printf("%f\n", sum);
}
return 0;
}The number of times the variable sum will be printed, when the above program is executed, is ______.GATE 2018 · Q32MCQ2 marks
Consider the following C code. Assume that unsigned long int type length is 64 bits.
unsigned long int fun(unsigned long int n){
unsigned long int i, j = 0, sum = 0;
for (i = n; i > 1; i = i/2) j++;
for ( ; j > 1; j = j/2) sum++;
return(sum);
}The value returned when we call fun with the input 240 isGATE 2014 Set 1 · Q41MCQ2 marks
Consider the following C function in which size is the number of elements in the array E:
int MyX(int *E, unsigned int size)
{
int Y = 0;
int Z;
int i, j, k;
for(i = 0; i < size; i++)
Y = Y + E[i];
for(i = 0; i < size; i++)
for(j = i; j < size; j++)
{
Z = 0;
for(k = i; k <= j; k++)
Z = Z + E[k];
if (Z > Y)
Y = Z;
}
return Y;
}The value returned by the function MyX is theGATE 2014 Set 1 · Q42MCQ2 marks
Consider the following pseudo code. What is the total number of multiplications to be performed?
D = 2
for i = 1 to n do
for j = i to n do
for k = j + 1 to n do
D = D * 3GATE 2013 Code A · Q31MCQ2 marks
Consider the following function:
int unknown(int n){
int i, j, k=0;
for (i=n/2; i<=n; i++)
for (j=2; j<=n; j=j*2)
k = k + n/2;
return (k);
}The return value of the function isCommon traps
if (x = 5) assigns, then tests 5 (true).A for loop step runs before the next test, so after a break the step has not run.
In
if ((j >= 0) && (i++)) i is incremented only when j ≥ 0.A switch with no break prints all later cases.