0
0
Bash Scriptingscripting~20 mins

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

Choose your learning style9 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.