Loops in C Programming
These types of statements are used for the repeat execution of a list of statements depending on the value of an integer expression.
The types of iterative statements are as follows:
1. While loop
- This loop has a mechanism of repeating one or more statements while a particular condition is true.
- In this type of loop, the condition is generally executed before any of the statements are executed.
- If the condition is found to be true then only will the statements execute otherwise the condition will be false.
- While loop will keep on executing as long as the condition is true.
Example: Write a program to calculate the sum of first 20 numbers.
int x=0, sum=0;
printf("Sum of the first 20 numbers = %d", sum);
Sum of the first 20 numbers = 210
2. do-while loop
- It is similar to the while loop.
- The only difference is that the test condition here is ended at the end of the loop.
- From this, it is clear that the body of the loop will be executed atleast once.
The do-while loop should always have a semi-colon at the end.
Example: Write a program to calculate the average of the first 10 numbers.
int a=0, sum = 0;
avg = sum/10.0;
printf("The sum of the first 10 numbers are = %d",sum);
printf("\n The average of the first 10 numbers are = %f",avg);
The sum of the first 10 numbers are = 55
The average of the first 10 numbers are = 5.000000
3. For loop
- This loop is similar to the while and do-while loop.
- It is a determinate or definite loop, as the programmer is aware as to how many times does he want the loop to be repeated.
- This loop is initialized only once. When the loop is run, with every iteration the value of the loop is updated and the condition is checked.
- If the condition is true the loop will be executed else the statements are skipped.
for (initialization; condition; increment/decrement)
Example : Write a simple for loop program to print the first 10 numbers
for (i=1; i<=5 ; i++)
printf("%d \n", i);
- The loops inside the loop are called Nested loops. The most popular types of nested loops are 'for' loops as they are very easy to control.
- 'For' loop can be used to control the number of times any statement needs to be executed. An outer loop is used for controlling the number of time the loop should be repeated.
- They should be indented properly so that the reader can easily determine what statements are written within each of them.
Write a program to print the following pattern:
* * *
for (i=1; i<=3; i++)
for(j=1; j<=i; j++)
Loop Control Statements
Fig: break Statement
- The break statement terminates the execution of the nearest enclosing loop in which it appears.
- Whenever the compiler comes across the break statement, the control is passed to the statement that follows the loop in which this statement appears.
- The break statement is used for exiting a loop from the point within its body by passing a normal termination expression.
- This statement appears only in the body of the loop.
- Whenever the compiler encounters this statement the rest of the statements are skipped and control is unconditionally transferred to the loop continuing portion of the nearest enclosing loop.
- It is the opposite of break statement.
- The next iteration of the loop is forced by skipping any code in between itself and the test condition of the loop. It is used for restarting a statement sequence whenever an error occurs.
By using the goto statement, control can be transferred to any part of the program.
- The label should be there in the same function and can appear only one statement before the same function.
- Whenever the goto statement is encountered the control gets transferred immediately to the statements following the label. It breaks the normal sequence of the execution.