Loops In Programming

In the previous tutorial we discussed about conditional statements and their role in programming. Loops are another very basic concept in programming. Loops allow you to iterate over a list of items and perform some operation. Just like conditional statements loops are also very frequently used in the programs you write.

Types of loops

There are three types of loops you will find in all programming languages

  1. For loop
  2. While loop
  3. Do-while loop

The basic job of all the loops is same – they perform some logic over and over again for a finite number of times. The difference in these loops is that they iterate based on different conditions.

For loops are counting loops, they iterate based on a counter

While loops are conditional loops, they iterate based on a condition.

Do-while loops are the same as while loops, they iterate based on a condition. The only difference is that in a while loop the condition is checked first and if the condition is true the loop performs the operation where as in a do-while loop the loop runs first and then checks the condition. Therefore, a do-while loop will always run at least once no matter if the condition true or false.

Syntax of for, while and do-while loops

The syntax of the for, while and do-while loops is almost similar for all programming languages. This is how these loops are written in general.

for( count = 0; count < 10 ; count++)  {      This for loop uses the count variable as its counter. It initializes the count variable to 0 and then increments it by 1 at each step. This loop will run till the count variable is less than 10. Once the value of count becomes 10 the loop will stop.  } 

Syntax for while loop

while(condition)  {      This while loop will run till the condition is true. Once the condition becomes false this loop will stop.  }

Syntax for do-while loop

do  {      This loop will run once and then check the while condition below. It will keep running till the while condition is true. Once the condition becomes false this loop will stop.  } while(condition)

Both conditional statements and loops are the heart and soul of programming. Whichever programming language you start learning you should learn their syntax properly and make sure you know how to use them in your programs.

Quick Links

E-Magazines

@

Total Followers
Study Group Created
Study Group Joined
Following Teacher
Following Organization
Blog Articles Added
Questions Asked
Questions Answered
Jobs Posted
Total Members in Group
Questions asked by members
Tasks added in this Group

Please wait..

Ok

Login to Open ESchool OR Create your account    Login   SignUp