Pages

Monday, August 14, 2017

Difference Between for Loop and While Loop

While loop iterates until the condition is no longer true. For loop can be used for fixed number of iterations. While loop can be used when you don’t know in advance how many iterations you need.

While loop is like a combination of an if statement and for loop.While loop iterates while a condition is true. When the condition resolved to false, while loop stops.

Syntax of While

while [test_condition]
do
  commands
done

Syntax of for

a) In Bash format

for variable in list_of_items
do
   commands
done   

b) In C format

Max=upper_limit
for ((i=1; i<=max; i++)
do
  commands
done

No comments:

Post a Comment