In C programming, a loop is a block of code that executes repeatedly until a specified condition is met. C provides three types of loops:
for loop: A for loop is used to execute a block of code a specified number of times. It consists of three parts: initialization, condition, and increment/decrement. The syntax for a for loop is as follows:
cfor (initialization; condition; increment/decrement) { // code to be executed }
while loop: A while loop is used to execute a block of code as long as a specified condition is true. The syntax for a while loop is as follows:
- c
while (condition) { // code to be executed }
do-while loop: A do-while loop is similar to a while loop, except that the code inside the loop is executed at least once, even if the condition is initially false. The syntax for a do-while loop is as follows:
cdo { // code to be executed } while (condition);
In C, variables are strongly typed, which means that the data type of a variable must be specified at the time of declaration. Some of the basic data types in C include:
- int: Used to store integer values.
- float: Used to store floating-point values (real numbers with decimal points).
- double: Used to store double-precision floating-point values (larger range and precision than float).
- char: Used to store a single character.
C also supports user-defined data types, such as structures and unions, which can be used to create more complex data structures.
Here's an example of a for loop in C:
c#include <stdio.h>
int main() {
int i;
for (i = 1; i <= 10; i++) {
printf("%d ", i);
}
return 0;
}
This program will output the numbers 1 to 10, separated by spaces:
1 2 3 4 5 6 7 8 9 10
In this example, the for loop iterates from 1 to 10 (inclusive) and prints each number using the printf()
function. The variable i
is initialized to 1, the loop condition i <= 10
is checked before each iteration, and i
is incremented by 1 after each iteration. The loop continues to execute as long as the condition is true (i.e., as long as i
is less than or equal to 10). Once the condition becomes false, the loop terminates and program execution continues with the next statement after the loop.
An example of a while loop in C:
c#include <stdio.h>
int main() {
int i = 1;
while (i <= 10) {
printf("%d ", i);
i++;
}
return 0;
}
This program will output the numbers 1 to 10, separated by spaces:
1 2 3 4 5 6 7 8 9 10
In this example, the while loop iterates from 1 to 10 (inclusive) and prints each number using the printf()
function. The variable i
is initialized to 1 before the loop, and the loop continues to execute as long as the condition i <= 10
is true. Inside the loop, the value of i
is printed using printf()
, and then incremented by 1 using the i++
statement. The loop continues to execute until the condition becomes false, at which point program execution continues with the next statement after the loop.
An example of a do-while loop in C:
c#include <stdio.h>
int main() {
int i = 1;
do {
printf("%d ", i);
i++;
} while (i <= 10);
return 0;
}
This program will output the numbers 1 to 10, separated by spaces:
1 2 3 4 5 6 7 8 9 10
In this example, the do-while loop is similar to the while loop example I provided earlier, except that the loop condition is checked at the end of the loop instead of the beginning. This means that the loop body will always execute at least once, even if the condition is initially false. In this case, the loop continues to execute as long as i
is less than or equal to 10. Inside the loop, the value of i
is printed using printf()
, and then incremented by 1 using the i++
statement. The loop continues to execute until the condition becomes false, at which point program execution continues with the next statement after the loop.