Bird
Raised Fist0
Cprogramming~10 mins

Why loops are needed in C - Test Your Understanding

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print numbers from 1 to 5 using a loop.

C
#include <stdio.h>

int main() {
    int i = 1;
    while ([1] <= 5) {
        printf("%d\n", i);
        i++;
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
Ai
B5
C0
D10
Attempts:
3 left
💡 Hint
Common Mistakes
Using a fixed number instead of the loop variable in the condition.
Not using the variable that changes inside the loop.
2fill in blank
medium

Complete the code to sum numbers from 1 to 10 using a for loop.

C
#include <stdio.h>

int main() {
    int sum = 0;
    for (int i = 1; i [1] 10; i++) {
        sum += i;
    }
    printf("Sum is %d\n", sum);
    return 0;
}
Drag options to blanks, or click blank then click option'
A<=
B>
C<
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of <=, which stops at 9.
Using == which only runs once.
3fill in blank
hard

Fix the error in the loop condition to print numbers from 0 to 4.

C
#include <stdio.h>

int main() {
    int count = 0;
    while (count [1] 5) {
        printf("%d\n", count);
        count++;
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
A==
B>=
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using > or >= which causes the loop not to run.
Using == which runs only once.
4fill in blank
hard

Fill both blanks to create a loop that prints even numbers from 2 to 10.

C
#include <stdio.h>

int main() {
    for (int num = [1]; num [2] 10; num += 2) {
        printf("%d\n", num);
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
A2
B1
C<=
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Starting from 1 instead of 2.
Using > instead of <= which skips printing 10.
5fill in blank
hard

Fill all three blanks to create a loop that prints numbers from 5 down to 1.

C
#include <stdio.h>

int main() {
    for (int i = [1]; i [2] 1; i[3]) {
        printf("%d\n", i);
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
A5
B>=
C--
D++
Attempts:
3 left
💡 Hint
Common Mistakes
Using ++ instead of -- which counts up instead of down.
Using < instead of >= which causes the loop to not run.

Practice

(1/5)
1. Why do we use loops in C programming?
easy
A. To repeat a block of code multiple times easily
B. To store multiple values in a variable
C. To create new data types
D. To stop the program immediately

Solution

  1. Step 1: Understand the purpose of loops

    Loops allow repeating the same code without writing it again and again.
  2. Step 2: Compare options with loop purpose

    Only To repeat a block of code multiple times easily describes repeating code multiple times easily, which is the main reason for loops.
  3. Final Answer:

    To repeat a block of code multiple times easily -> Option A
  4. Quick Check:

    Loops repeat code = To repeat a block of code multiple times easily [OK]
Hint: Loops repeat actions to avoid rewriting code [OK]
Common Mistakes:
  • Confusing loops with variables
  • Thinking loops create new data types
  • Believing loops stop the program
2. Which of the following is the correct syntax to start a for loop in C?
easy
A. for (int i = 0; i < 5; i++) { }
B. loop (i = 0; i < 5; i++) { }
C. for i = 0 to 5 { }
D. for i in range(5): { }

Solution

  1. Step 1: Recall C for loop syntax

    The correct for loop in C uses parentheses with initialization, condition, and increment separated by semicolons.
  2. Step 2: Check each option

    for (int i = 0; i < 5; i++) { } matches the correct C syntax: for (int i = 0; i < 5; i++) { }.
  3. Final Answer:

    for (int i = 0; i < 5; i++) { } -> Option A
  4. Quick Check:

    C for loop syntax = for (init; condition; increment) [OK]
Hint: C for loops use semicolons inside parentheses [OK]
Common Mistakes:
  • Using Python or other language syntax
  • Missing semicolons in for loop
  • Using wrong keywords like 'loop'
3. What will be the output of this C code?
int i;
for(i = 1; i <= 3; i++) {
    printf("%d ", i);
}
medium
A. 0 1 2 3
B. 1 2 3 4
C. 1 2 3
D. 3 2 1

Solution

  1. Step 1: Analyze loop range and iterations

    The loop starts at i=1 and runs while i is less than or equal to 3, so it runs for i=1, 2, 3.
  2. Step 2: Determine printed output

    Each iteration prints the current i followed by a space, so output is "1 2 3 "
  3. Final Answer:

    1 2 3 -> Option C
  4. Quick Check:

    Loop prints 1 to 3 = 1 2 3 [OK]
Hint: Check loop start, end, and printed values carefully [OK]
Common Mistakes:
  • Starting loop from 0 instead of 1
  • Including extra numbers beyond 3
  • Printing in reverse order
4. Identify the error in this C loop code:
int i = 0;
for(i = 0; i < 5; i++)
    printf("%d", i)
medium
A. Incorrect loop condition
B. Missing semicolon after printf statement
C. Variable i is not declared
D. for loop syntax is wrong

Solution

  1. Step 1: Check syntax inside the loop

    The printf statement is missing a semicolon at the end, which is required in C.
  2. Step 2: Verify other parts

    The loop condition and variable declaration are correct, and the for loop syntax is valid.
  3. Final Answer:

    Missing semicolon after printf statement -> Option B
  4. Quick Check:

    Missing semicolon causes syntax error [OK]
Hint: Every statement in C ends with a semicolon [OK]
Common Mistakes:
  • Ignoring missing semicolon errors
  • Thinking loop condition is wrong
  • Assuming variable is undeclared
5. You want to print all even numbers from 2 to 10 using a loop in C. Which loop is best and why?
hard
A. Use nested loops to check each number from 2 to 10
B. Use a while loop starting at 1, increment by 1 until 10 and print if even
C. Use a do-while loop starting at 10, decrement by 2 until 2
D. Use a for loop starting at 2, increment by 2 until 10

Solution

  1. Step 1: Understand the task

    We need to print even numbers from 2 to 10, which are 2,4,6,8,10.
  2. Step 2: Evaluate loop options

    Use a for loop starting at 2, increment by 2 until 10 uses a for loop starting at 2 and increments by 2, directly printing even numbers efficiently.
  3. Final Answer:

    Use a for loop starting at 2, increment by 2 until 10 -> Option D
  4. Quick Check:

    For loop with step 2 prints evens directly [OK]
Hint: Increment by 2 in for loop to print evens directly [OK]
Common Mistakes:
  • Using nested loops unnecessarily
  • Starting from 1 and checking each number
  • Using do-while with wrong direction
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard