C Loops
The loops in C language are used to execute a block of code or a part of the program several times.
In other words, it iterates a code or group of code many times.
Why use loops in C language?
Suppose that you have to print table of 2, then you need to write 10 lines of code.
By using the loop statement, you can do it by 2 or 3 lines of code only.
Advantage of loops in C
1) It saves code.
2) It helps to traverse the elements of array (which is covered in next pages).
Types of C Loops
There are three types of loops in C language that is given below:
- do while
- while
- for
do-while loop in C
It iterates the code until condition is false. Here, condition is given after the code. So at least once, code is executed whether condition is true or false.
It is better if you have to execute the code at least once.
The syntax of do-while loop in c language is given below:
while loop in C
Like do while, it iterates the code until condition is false. Here, condition is given before the code. So code may be executed 0 or more times.
It is better if number of iteration is not known by the user.
The syntax of while loop in c language is given below:
for loop in C
Like while, it iterates the code until condition is false. Here, initialization, condition and increment/decrement is given before the code. So code may be executed 0 or more times.
It is good if number of iteration is known by the user.
The syntax of for loop in c language is given below:
No comments:
Post a Comment