How to resolve the algorithm Loops/For step by step in the UNIX Shell programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Loops/For step by step in the UNIX Shell programming language
Table of Contents
Problem Statement
“For” loops are used to make some block of code be iterated a number of times, setting a variable or parameter to a monotonically increasing integer value for each execution of the block of code. Common extensions of this allow other counting patterns or iterating over abstract structures other than the integers.
Show how two loops may be nested within each other, with the number of iterations performed by the inner for loop being controlled by the outer for loop. Specifically print out the following pattern by using one for loop nested in another:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Loops/For step by step in the UNIX Shell programming language
Source code in the unix programming language
#!/bin/sh
# Using a while control construct to emulate a for loop
l="1" # Set the counters to one
while [ "$l" -le 5 ] # Loop while the counter is less than five
do
m="1"
while [ "$m" -le "$l" ] # Loop while the counter is less than five
do
printf "*"
m=`expr "$m" + 1` # Increment the inner counter
done
echo
l=`expr "$l" + 1` # Increment the outer counter
done
for i in `jot 5`; do
for j in `jot $i`; do
printf \*
done
echo
done
for (( x=1; $x<=5; x=$x+1 )); do
for (( y=1; y<=$x; y=$y+1 )); do
echo -n '*'
done
echo ""
done
foreach i (`jot 5`)
foreach j (`jot $i`)
echo -n \*
end
echo ""
end
for ((x = 1; x <= 5; x += 1))
do
for ((y = 0; y < x; y += 1))
do
print -n '*'
done
print
done
You may also check:How to resolve the algorithm Singly-linked list/Element definition step by step in the 6502 Assembly programming language
You may also check:How to resolve the algorithm Floyd's triangle step by step in the Python programming language
You may also check:How to resolve the algorithm Subtractive generator step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Dot product step by step in the Cowgol programming language
You may also check:How to resolve the algorithm Super-d numbers step by step in the Nim programming language