C Special Loops
There are two special loops in C:
- Nested loop – a loop inside of another loop, also called a loop within a loop.
- The forever loop – a controlled infinite loop
Here is a sample code for the nested loop:
/*
*
* nested.c
* Program to demonstrate the use of nested loops
* by Mark Virtue, 2001.
*
*/
#include <stdio.h>
main() {
int i, j;
for (i = 1; i <= 5; i = i + 1)
for (j = 1; j <= 3; j = j + 1)Â Â Â // nested loop
printf("Here we are in the inner loop with i = %d and j = %dn", i, j);
fflush(stdin);
getchar();
}
Nested loops are often used in the multi-dimensional arrays.
And here is the forever loop:
/*
*
* forever.c
* Program to demonstrate the use of the "forever" loop
* by Mark Virtue, 2001.
*
*/
#include <stdio.h>
main() {
char type;
int album;Â Â Â // boolean
for (;;) {Â Â Â // A for loop that has no arguments will repeat the loop forever
printf("Album or single (a for album, s for single)? ");
scanf("%c", &type);
while (getchar() != 'n' && getchar() != EOF)Â Â Â // nested loop
printf("");
if (type == 'a' || type == 's')
break;
printf("Errorn");
}
album = type == 'a';
if (album)
printf("Albumn");
else
printf("Singlen");
fflush(stdin);
getchar();
}
The forever loops can only used in the for loops, and it’s similar to the while loop for input validation.
C break and continue
break and continue are two keywords that used in loops. They control the flow inside the loops. Below shows how these keywords work inside a block of code:
// break
while (condition1) {
statements...;
if (condition2)
break;
statements...;
}
// continue
while (condition1) {
statements...;
if (condition2)
continue;
statements...;
}
The break statement is used to skip a certain condition in a loop, discard the statements below the break statement and end the loop. For example, the loop continues to execute normally in the above code until the condition increases to condition2, it discards the “statements…” below the break statement and ends the loop on condition2.
Just as the break statement, the continue statement skips a certain condition in a loop and discards the statement below the continue statement. The difference from the break statement is that the continue statement continues to executed the loop once the specified condition is skipped. For example, the loop continues to execute normally in the above code until the condition increases to condiion2, it discards the “statements…” below the continue statement and continues the execution at condition3.
C do…while Loop
The do…while loop is the third type of loop for C and other programming languages. This type of loop is not very often used. It’s similar to the while loop. Here is the format of the do…while loop:
do {
statements...;
}
while (condition);
Below are the execution steps:
- All the statements are executed
- The condition is checked
- The statements are executed one more time if the condition is true
- Repeat the above steps until the condition becomes false
The difference between do…while loop and while loop is that while loop checks the condition before the statements are executed whereas the do…while loop checks the condition after the statement are executed. This means that do…while loop executes the statements at least once, while the while loop might not execute the statements at all based on its condition.
The following is the sample code for the do…while loop:
/*
*
* dowhile.c
*
* Program to demonstrate the use of the do...while loop
*
* by Mark Virtue, 2001.
*
*/
#include <stdio.h>
main() {
char type;
int album;Â Â Â // Boolean
do {
printf("Album or single (a for album, s for single)? ");
while (getchar() != 'n' && getchar() != EOF)
printf("");
scanf("%c", &type);
if (type != 'a' && type != 's')
printf("Errorn");
}
while (type != 'a' && type != 's');
album = type == 'a';Â Â Â // Convert to boolean
if (album)
printf("Albumn");
else
printf("Singlen");
fflush(stdin);
getchar();
}
Note that it’s the same code as the while loop. The difference is that the type variable is not initialized anymore since it uses do…while loop.
C for Loop
The for loop is used when the number of time the loop repeated is specified. The format of the for loop is:
for (initialization; condition; increment) {
statements;
}
The following steps show how the for loop is executed:
- Initialize variable, the value usually is 0
- Check the condition
- If the condition is true, statements are executed
- The value of the variable increased by 1
- Loop from #2 to #4 until the condition becomes false
Here is the sample code for the for loop:
/*
*
* for.c
*
* Program to demonstrate the use of the for loop
*
* by Mark Virtue, 2001.
*
*/
#include <stdio.h>
main() {
int counter;
int total = 0;
for (counter = 1; counter <= 100; counter = counter + 1) {
total = total + counter;
}
printf("The sum of all numbers from 1 to 1000 is %dn", total);
fflush(stdin);
getchar();
}
Note that the increment of the for loop can also be counter++ instead of counter = counter + 1.
C while Loop
The while loop is often used when it doesn’t know how many time the loop will be repeated. Here is the sample code for the while loop:
/*
*
* while.c
*
* Program to demonstrate the use of the while loop
*
* by Mark Virtue, 2001.
*
*/
#include <stdio.h>
main() {
char type = 'x';
int album;Â Â Â // Boolean
while (type != 'a' && type != 's') {
printf("Album or single (a for album, s for single)? ");
scanf("%c", &type);
album = type == 'a';Â Â Â // Determine if type is equal to 'a'
if (type != 'a' && type != 's')
printf("Errorn");
// Dummy statement to flush the input
while (getchar() != 'n' && getchar() != EOF) {
printf("");
}
}
if (album)
printf("Albumn");
else
printf("Singlen");
fflush(stdin);
getchar();
}
As you can see in the code above, format of the while loop is the same as the format of the if statement, except the condition is repeatedly used until the condition is true instead of used only once.
Below is the sample code for the while loop which is specified the number of repeated loops:
/*
*
* while2.c
*
* Program to demonstrate the use of the while loop
*
* by Mark Virtue, 2001.
*
*/
#include <stdio.h>
main() {
int counter = 1;
int total = 0;
while (counter <= 1000) {
total = total + counter;
counter = counter + 1;
}
printf("The sum of all numbers from 1 to 1000 is %dn", total);
fflush(stdin);
getchar();
}
C Loop Introduction
A loop is the same as an if statement, except it is used for repetition of a block of code based on a certain condition. There are two types of loops:
- Loops that specify how many times the code will be repeated when the loop start
- Loops that do not know how many times the code will be repeated (For example, user input from the keyboard)
Note that an invalid condition could cause an infinite loop, a loop that never stops. This can have some side effects, such as OS freeze or unresponsive application.