Loops in C:
Loop / Iteration: In C and all other programming Languages, Iteration(also called loops) statements allow a set of instructions to be repeatedly executed until a certain condition is reached.
Types of Loops in C Language are :
1. For Loop
2. While Loop
3. Do-While Loop
For Loop: The for loop is used to execute some part of code until the given condition is satisfied. The for loop is used in the case where the number of iterations is known in advance.
syntax :
for (initialization ; condition ; Increment/Decrement)
{
// code to be executed
}
Initialization: It is an assignment statement that is used to set up a loop control variable.
condition: The condition is a relational expression that determines when the loop exits.
incre/decre: Defines the how-to loop control variable changes each time the loop is repeated.
Flow Diagram :
Infinite for Loop: An Infinite Loop is a looping construct that does not terminate the loop and executes the loop forever. It is also called an Indefinite Loop or endless loop. It produces continuous output.
syntax: for ( ; ; )
Example
Output:
While Loop: It allows a part of code to be executed multiple times depending upon a given boolean condition. The while loop is used in the case where the number of iterations is not known in advance. The condition is checked at the start of while block.
syntax:
while(condition)
{
//code to be executed
}
Flow Diagram:
Example:
Output:
No comments:
Post a Comment