Challenge - 5 Problems
C-style Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2: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
Attempts:
2 left
💡 Hint
Remember the loop starts at 3 and decreases until it is greater than 0.
✗ Incorrect
The loop starts at 3 and decrements by 1 each time until i is no longer greater than 0. So it prints 3, 2, 1 with spaces.
💻 Command Output
intermediate2: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
Attempts:
2 left
💡 Hint
Add numbers from 1 to 5 inclusive.
✗ Incorrect
The loop adds 1+2+3+4+5 which equals 15.
📝 Syntax
advanced2: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?
Attempts:
2 left
💡 Hint
Check the placement of semicolons and parentheses.
✗ Incorrect
Option B correctly uses double parentheses with semicolon before 'do'. Option B misses semicolon, B misses 'do' keyword, C uses wrong parentheses.
🔧 Debug
advanced2: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
Attempts:
2 left
💡 Hint
Check the loop condition and initial value.
✗ Incorrect
Since i starts at 5 and the condition is i<5, it is false immediately, so the loop body never executes.
🚀 Application
expert3: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?
Attempts:
2 left
💡 Hint
Use increment of 2 starting at 2 and include 10.
✗ Incorrect
Option D uses i starting at 2, increments by 2, and includes 10, printing 2 4 6 8 10. Option D also prints correct numbers but uses an if condition inside the loop, which is valid but less direct. Option D stops before 10, missing 10. Option D prints all numbers 2 to 10.