91-9990449935 0120-4256464 |
C LoopsThe 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 C1) It saves code. 2) It helps to traverse the elements of array (which is covered in next pages). Types of C LoopsThere are three types of loops in C language that is given below:
do-while loop in CIt 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: Flowchart and Example of do-while loopwhile loop in CLike 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: Flowchart and Example of while loopfor loop in CLike 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: Flowchart and Example of for loop
Next Topicdo-while loop in C
|