1: An Introduction to the While Loop
The while
loop in C is used to execute a block of code repeatedly as long as a specified condition is true.
-
Syntax:
while (condition) {
// Statements to be executed
// Loop continues until condition becomes false
}
-
Example:
int count = 1;
while (count <= 5) {
printf("Count: %dn", count);
count++;
}
2: Introduction to Do-While Loop
The do-while
loop is similar to the while
loop, but it guarantees that the block of code is executed at least once before checking the condition.
-
Syntax:
do {
// Statements to be executed
// Loop continues until condition becomes false
} while (condition);
-
Example:
int num;
do {
printf("Enter a positive number: ");
scanf("%d", &num);
} while (num <= 0);printf("You entered: %dn", num);
3: Introduction to For Loop
The for
loop is used when the number of iterations is known or fixed.
-
Syntax:
for (initialization; condition; increment/decrement) {
// Statements to be executed
}
-
Example:
int i;
for (i = 1; i <= 5; i++) {
printf("Iteration: %dn", i);
}
4: Nested Loops
You can use loops inside other loops, known as nested loops, to perform more complex repetitive tasks.
- Example:
int rows = 3;
int cols = 4;
int i, j;for (i = 1; i <= rows; i++) {
for (j = 1; j <= cols; j++) {
printf("(%d, %d) ", i, j);
}
printf("n");
}
5: Loop Control Statements (break, continue)
Loop control statements allow you to control the flow of loops.
-
break
Statement:- Terminates the loop immediately when encountered.
int i;
for (i = 1; i <= 10; i++) {
if (i == 5) {
break;
}
printf("%d ", i);
}
-
continue
Statement:- Skips the current iteration of the loop and proceeds to the next iteration.
int i;
for (i = 1; i <= 5; i++) {
if (i % 2 == 0) {
continue;
}
printf("%d ", i);
}
6: Practice and Examples
- Practice Exercise:
- Write a program to print the multiplication table of a given number using a
for
loop. - Use a
while
loop to prompt the user to enter positive numbers until they enter a negative number. - Implement a
do-while
loop to simulate a simple guessing game where the user has to guess a number between 1 and 10.
- Write a program to print the multiplication table of a given number using a
#include <stdio.h>
int main() {
// Example: Multiplication table using for loop
int num, i;
printf(“Enter a number to display its multiplication table: “);
scanf(“%d”, &num);
for (i = 1; i <= 10; i++) {
printf(“%d x %d = %dn”, num, i, num * i);
}
// Example: Prompting user to enter positive numbers until negative number is entered using while loop
int positiveNum;
while (1) {
printf(“Enter a positive number (enter a negative number to stop): “);
scanf(“%d”, &positiveNum);
if (positiveNum < 0) {
printf(“Negative number entered. Stopping…n”);
break;
}
}
// Example: Guessing game using do-while loop
int secretNumber = 7;
int guess;
do {
printf(“Guess the secret number (1-10): “);
scanf(“%d”, &guess);
if (guess < secretNumber) {
printf(“Too low! Try again.n”);
} else if (guess > secretNumber) {
printf(“Too high! Try again.n”);
} else {
printf(“Congratulations! You guessed the secret number.n”);
}
} while (guess != secretNumber);
return 0;
}
Practice implementing different types of loops and loop control statements to strengthen your understanding of repetitive programming tasks in C. Loops are essential for executing tasks efficiently and handling repetitive operations in real-world applications.