Loop control statements in C are used to execute a block of code several times until the given condition is true. Whenever we need to execute some statements multiple times, we need to use a loop statement.
Loops in C are used in performing repetitive tasks. Here are some situations where we should use loops:
- We want to find sum of all consecutive integers between 0 to N.
- You want to take N numbers as input from user and store it in an array. Where N is not fixed, it is based on user input.
Types of Loop Statements in CClick on the links below to check detailed description of various loop statements.
|for loop||Repeats a block of code multiple times until a given condition is true. Initialization, looping condition and update expression(increment/decrement) is part of for loop syntax.|
|while loop||Repeats a block of code multiple times until a given condition is true. Unlike for loop, while loop doesn't contain initialization and update expression in it's syntax.|
|do while loop||Similar to while loop, but it tests the condition at the end of the loop body. The block of code inside do while loop will execute at least once.|
Loop Control and Jump Statements in C
Jump statements alter the normal execution path of a program. Jump statements are used when we want to skip some statements inside loop or terminate the loop immediately when some condition becomes true.
C programming language support following jump statements.
|continue||The continue statement is used for skipping part of loop's body.|
|break||The break statement is used to stop the execution of loop and switch case statements.|
|goto||The goto statement is used for jumping from fron statement to another within a function.|