Bird
Raised Fist0
Bash Scriptingscripting~20 mins

C-style for loop in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
C-style Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of a C-style for loop counting down
What is the output of this Bash script using a C-style for loop?
Bash Scripting
for ((i=3; i>0; i--)); do echo -n "$i "; done
A3 2 1 0
B1 2 3
C3 2 1
D0 1 2 3
Attempts:
2 left
💡 Hint
Remember the loop starts at 3 and decreases until it is greater than 0.
💻 Command Output
intermediate
2:00remaining
Sum of numbers using C-style for loop
What is the value of the variable 'sum' after running this script?
Bash Scripting
sum=0
for ((i=1; i<=5; i++)); do sum=$((sum + i)); done
echo $sum
A15
B10
C5
D0
Attempts:
2 left
💡 Hint
Add numbers from 1 to 5 inclusive.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in this C-style for loop
Which option contains the correct syntax for a C-style for loop in Bash?
Afor ((i=0; i<5; i++)); echo $i; done
Bfor ((i=0; i<5; i++)); do echo $i; done
Cfor (i=0; i<5; i++) do echo $i; done
Dfor ((i=0; i<5; i++)) do echo $i; done
Attempts:
2 left
💡 Hint
Check the placement of semicolons and parentheses.
🔧 Debug
advanced
2:00remaining
Why does this C-style for loop not print anything?
Given this script, why is there no output?
Bash Scripting
for ((i=5; i<5; i++)); do echo $i; done
AThe loop condition i<5 is false at start, so loop never runs.
BThe increment i++ is incorrect syntax.
CThe loop variable i is not initialized.
DThe echo command is missing.
Attempts:
2 left
💡 Hint
Check the loop condition and initial value.
🚀 Application
expert
3:00remaining
Create a C-style for loop to print even numbers from 2 to 10
Which option correctly prints even numbers 2 4 6 8 10 using a C-style for loop in Bash?
Afor ((i=1; i<=10; i++)); do if (( i % 2 == 0 )); then echo -n "$i "; fi; done
Bfor ((i=2; i<10; i+=2)); do echo -n "$i "; done
Cfor ((i=2; i<=10; i++)); do echo -n "$i "; done
Dfor ((i=2; i<=10; i+=2)); do echo -n "$i "; done
Attempts:
2 left
💡 Hint
Use increment of 2 starting at 2 and include 10.

Practice

(1/5)
1. What does the following C-style for loop do in bash?
for ((i=1; i<=3; i++)); do echo $i; done
easy
A. Prints numbers 1, 2, and 3 each on a new line
B. Prints numbers 0, 1, and 2 each on a new line
C. Prints numbers 1, 2, 3, and 4 each on a new line
D. Causes a syntax error

Solution

  1. Step 1: Understand the loop initialization and condition

    The loop starts with i=1 and runs while i is less than or equal to 3.
  2. Step 2: Analyze the increment and output

    Each iteration prints the current value of i, then increases i by 1 until i becomes 4 and stops.
  3. Final Answer:

    Prints numbers 1, 2, and 3 each on a new line -> Option A
  4. Quick Check:

    Loop from 1 to 3 prints 1, 2, 3 [OK]
Hint: Check start, end, and increment values carefully [OK]
Common Mistakes:
  • Starting loop at 0 instead of 1
  • Confusing <= with < in condition
  • Forgetting to increment i
2. Which of the following is the correct syntax for a C-style for loop in bash?
easy
A. for (i=0; i<5; i++) do echo $i; done
B. for ((i=0; i<5; i++)); do echo $i; done
C. for [i=0; i<5; i++] do echo $i; done
D. for {i=0; i<5; i++} do echo $i; done

Solution

  1. Step 1: Identify correct C-style for loop syntax in bash

    Bash requires double parentheses (( )) around the loop control with semicolons separating init, condition, and increment.
  2. Step 2: Check each option

    for ((i=0; i<5; i++)); do echo $i; done uses double parentheses and semicolons correctly; others use incorrect brackets or missing parentheses.
  3. Final Answer:

    for ((i=0; i<5; i++)); do echo $i; done -> Option B
  4. Quick Check:

    Double parentheses and semicolons = correct syntax [OK]
Hint: Look for double parentheses and semicolons inside [OK]
Common Mistakes:
  • Using single parentheses instead of double
  • Using square or curly brackets
  • Missing semicolons between parts
3. What is the output of this bash script?
for ((i=2; i<=6; i+=2)); do echo $i; done
medium
A. 2 3 4 5 6
B. Syntax error
C. 2 4 6 8
D. 2 4 6

Solution

  1. Step 1: Understand loop initialization and increment

    The loop starts at i=2, runs while i is less than or equal to 6, and increments i by 2 each time.
  2. Step 2: List values printed

    Values printed are 2, then 4, then 6. After 6, i becomes 8 which breaks the condition.
  3. Final Answer:

    2 4 6 -> Option D
  4. Quick Check:

    Increment by 2 from 2 to 6 prints 2,4,6 [OK]
Hint: Check increment step and loop condition carefully [OK]
Common Mistakes:
  • Assuming increment is 1 by default
  • Including numbers beyond the condition
  • Confusing <= with <
4. Identify the error in this bash C-style for loop:
for ((i=0; i<5 i++)); do echo $i; done
medium
A. Missing semicolon between condition and increment
B. Using single parentheses instead of double
C. Incorrect variable name
D. Missing do keyword

Solution

  1. Step 1: Check loop syntax parts

    The loop control must have three parts separated by semicolons: initialization, condition, and increment.
  2. Step 2: Locate missing semicolon

    Between 'i<5' and 'i++' there is no semicolon, causing a syntax error.
  3. Final Answer:

    Missing semicolon between condition and increment -> Option A
  4. Quick Check:

    Semicolons separate parts in C-style for loops [OK]
Hint: Check for three semicolon-separated parts inside (( )) [OK]
Common Mistakes:
  • Omitting semicolons between parts
  • Using single parentheses
  • Forgetting do keyword
5. You want to print all odd numbers from 1 to 9 using a C-style for loop in bash. Which script achieves this?
hard
A. for ((i=1; i<10; i++)); do if (( i % 2 == 0 )); then echo $i; fi; done
B. for ((i=0; i<10; i++)); do if (( i % 2 == 0 )); then echo $i; fi; done
C. for ((i=1; i<=9; i+=2)); do echo $i; done
D. All of the above

Solution

  1. Step 1: Analyze for ((i=1; i<=9; i+=2)); do echo $i; done

    for ((i=1; i<=9; i+=2)); do echo $i; done starts at 1, increments by 2, prints 1,3,5,7,9 directly.
  2. Step 2: Analyze options B and C

    Both loop with i++ and check if (( i % 2 == 0 )), which selects even numbers: B prints 0,2,4,6,8; C prints 2,4,6,8.
  3. Final Answer:

    for ((i=1; i<=9; i+=2)); do echo $i; done -> Option C
  4. Quick Check:

    Increment by 2 from 1 to <=9 prints odds 1-9 [OK]
Hint: Use increments or modulo checks to filter odd numbers [OK]
Common Mistakes:
  • Using wrong increment step
  • Starting from 0 without filtering
  • Forgetting to check odd condition
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