0
0
Bash Scriptingscripting~10 mins

C-style for loop in Bash Scripting - Interactive Code Practice

Choose your learning style9 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 C-style for loop in bash.

Bash Scripting
for (( i=1; i<=5; i[1] )); do
  echo $i
done
Drag options to blanks, or click blank then click option'
A+
B++
C--
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using '--' which decreases the variable.
Using '+' or '*' which are not valid increment operators in this syntax.
2fill in blank
medium

Complete the code to print numbers from 10 down to 1 using a C-style for loop in bash.

Bash Scripting
for (( i=10; i[1]1; i-- )); do
  echo $i
done
Drag options to blanks, or click blank then click option'
A<
B<=
C>=
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' or '<=' which would not work for counting down.
Using '>' which would cause the loop to never run.
3fill in blank
hard

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

Bash Scripting
for (( i=0; i[1]5; i++ )); do
  echo $i
done
Drag options to blanks, or click blank then click option'
A<
B<=
C>
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' causes the loop to print 5, which is not desired.
Using '>' or '>=' causes the loop to never run.
4fill in blank
hard

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

Bash Scripting
for (( i=[1]; i[2]10; i+=2 )); do
  echo $i
done
Drag options to blanks, or click blank then click option'
A2
B1
C<=
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Starting at 1 prints odd numbers.
Using '<' excludes 10 from the output.
5fill in blank
hard

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

Bash Scripting
for (( [1]=5; [2][3]0; [1]-- )); do
  echo $[1]
done
Drag options to blanks, or click blank then click option'
Ai
Bj
C>=
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in initialization and condition.
Using '>' excludes 0 from the output.