Loops:Looping statements in C progamming also known as iterative statements.They allows programmer to execute statements or group of statements multiple time until the specific condition becomes false.
When programmer want to execute or use a block of code repeatedly then programmer need to use looping statement in C Programming.
Loop executes the same code multiple times so it saves code and time.
In Programming we some times have to perform the same action or program over and over again this mechanism known as the loop.
sometimes programmer want to break given loop known as loop control.
loop control helps to change execution of a loop from its original sequence.
Loops Introduction in C Language
Advantage using looping in C programming
Following are some of advantage using loops in C program.
i. Reduce the length of program.
ii. Loops reduce the Time complexity and space Complexity.
iii. Less memory space required.
iv. Less burden on programmer to write code or program.
v.Loops are used to make an easy flow of control.
vi. Using Loops programmer can write code to traverse over the elements in program.
vii. Using Loops programmer do not need to write same code again and again.
Given Diagram is the flowchart to understand how looping works in programming.
Let us start learning how loop work with the help of flowchart given in the fig.
The Loop flow chart consist of two Major Parts:
i. Control Statement.
ii. The Body
i. Control Statement:
Control statements are The conditions or criteria that programmer want to implement during the execution of the loop in the Program.
ii. The Body:
The Body part are the statements or instruction that programmer want to repeate multiple times untill certain condition is met.
The flow diagram is to show number series from 1,2,3......10 using loops.
Flow diag. Explanation:
•Step 1: Start the loop or program.
•Step 2: Declare and initilize i=1.
here the value of i is initilized to 1
•Step 3: test i<=10
value of i is 1 so i<=10 is true and control goes to show i statement.
after processing it shows value of i .i.e 1 on screen.
Next i=i+1 is processed, which increase the value of i by 1 hence i becomes 2 i.e i=2.
after that it again test the condition i<=10
i.e 2<=10 which is true then control goes to show i statement.
it show value 2. Next i=i+1 processed and increase the value of i by i here i become 3 i.e. i=3.
again test the condition i<=10 which is true.
The above process repeate until value of i is less or equal to 10 i.e. i<=10
when i becomes i=11 then loop will ternminate and control come out of loop.
•Step 4: Stop the program execution.
• The initialization and declaration in a loop given in flow dig. is performed once, and it is never repeated.
• If the number of iterations is not mentioned in the for loop i.e. i<=10, it will continue to execute infinite times.
Disadvantage of loops in Programming.
• If the condition is not well defined in the loop then loop might lead to unexpected result.
• the control will never come out of loop or exit if the condition is always true.
And due to this program run indefinnitely.
Previous Topic:-->> switch case statement in C || Next topic:-->>goto statement in C.