What is Iteration Statements or Loops in Programming Languages
Iteration Statements or Loops
In C/C++, and all other modern programming languages, iteration statements (also called loops) allow a set of instructions to be executed repeatedly until a certain condition is reached. In other words we can define loops as a loop is a way of repeating a statement a number of times until some way of ending the loop occurs. It might be run for a preset number of times, typically in for loop, repeated as long as an expression is true (a while loop) or repeated until an expression becomes false in a do while loop.
Basically C/C++ provide us 3 types of loops
• The for Loop
• The while Loop
• The do-while Loop
Nested Loops
We can defines as nested loop is a loop within a loop, an inner loop within the body of an outer one. How this works is that the first pass of the outer loop triggers the inner loop, which executes to completion. Then the second pass of the outer loop triggers the inner loop again. This repeats until the outer loop finishes. Of course, a break within either the inner or outer loop would interrupt this process.
Example of Nasted loop
#!/bin/bash
# nested-loop.sh: Nested “for” loops.
outer=1 # Set outer loop counter.
# Beginning of outer loop.
for a in 1 2 3 4 5
do
echo “Pass $outer in outer loop.”
echo “———————”
inner=1 # Reset inner loop counter.
# ===============================================
# Beginning of inner loop.
for b in 1 2 3 4 5
do
echo “Pass $inner in inner loop.”
let “inner+=1″ # Increment inner loop counter.
done
# End of inner loop.
# ===============================================
let “outer+=1″ # Increment outer loop counter.
echo # Space between output blocks in pass of outer loop.
done
# End of outer loop.
exit 0
Popularity: 1% [?]