C.Module 03
C.Module 03
1. for loop
2. while loop
3. do...while loop
For Loop
The for loop is used when the number of iterations is known in advance. It consists of three parts:
initialization, condition, and increment/decrement.
Syntax:
Example:
#include <stdio.h>
int main() {
int i;
for (i = 1; i <= 5; i++) {
printf("%d\n", i);
}
return 0;
}
While Loop
The while loop runs a block of code as long as the condition remains true. It's used when the
number of iterations is not known.
Syntax:
while (condition) {
// Code to be executed
}
Example:
#include <stdio.h>
int main() {
int i = 1;
while (i <= 5) {
printf("%d\n", i);
i++;
}
return 0;
}
• condition: checked before each iteration; the loop will continue if the condition is true.
Do-While Loop
The do...while loop is similar to the while loop, but it guarantees that the loop will be executed
at least once because the condition is checked after the loop body.
Syntax:
do {
// Code to be executed
} while (condition);
Example:
#include <stdio.h>
int main() {
int i = 1;
do {
printf("%d\n", i);
i++;
} while (i <= 5);
return 0;
}
condition: checked after the loop body, so the loop executes at least once.
An infinite loop occurs when the condition is always true.
while (1) {
// This will run forever
}
Break Statement
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break; // Loop will exit when i is 5
}
printf("%d\n", i);
}
Continue Statement
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue; // Skips the rest of the code when i is 3
}
printf("%d\n", i);
}
#include <stdio.h>
int main() {
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
printf("%d %d\n", i, j);
}
}
return 0;
}
• The goto statement in C transfers control unconditionally to another part of the program.
• It is generally considered bad practice in modern programming.
• It can make code less readable and harder to maintain.
• However, it can be useful for:
Syntax of goto:
goto label;
...
label:
// Code to execute when label is reached
#include <stdio.h>
int main() {
int i = 1;
return 0;
}
Breaking out of Nested Loops with goto:
goto can be useful for exiting deeply nested loops, which is one of its few practical uses.#include
<stdio.h>
int main() {
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (i == 2 && j == 2) {
goto exit_loop; // Exits the nested loop
}
printf("%d %d\n", i, j);
}
}
exit_loop:
printf("Exited from nested loops\n");
return 0;
}
Sometimes, goto is used in error handling, especially when there are multiple error checks
within a function.
#include <stdio.h>
int main() {
int status = 1;
if (status == 1) {
printf("Error occurred!\n");
goto error; // Jump to the error handling section
}
error:
printf("Handling the error here...\n");
return -1;
}
1.1 for Loop:
1. Write a program that prints the factorial of a number n entered by the user.
2. Create a while loop that reads integers from the user until a negative number is entered.
Print the count of positive integers entered.
1. Write a program that prompts the user to enter a password until the correct password
("admin") is entered.
2. Create a do...while loop that prints numbers from 10 down to 1.
2. break Statement
1. Write a program with a for loop that prints numbers from 1 to 20, but stops if the
number is divisible by 7.
2. Create a while loop that reads integers from the user and breaks if the input is equal to
0.
3. continue Statement
1. Write a for loop that prints all the numbers from 1 to 20, but skips numbers that are
divisible by 3.
2. Create a while loop that reads integers from the user and skips printing any number
that is less than 5.
4. goto Statement
1. Write a program that uses goto to print numbers from 1 to 10, skipping the number 5.
2. Create a program that uses goto to jump to an error handling section if an invalid input
is detected.
5. Infinite Loops
1. Write a while loop that repeatedly asks the user for a number and prints it. The loop
should run indefinitely until terminated by the user.
2. Create a for loop that runs indefinitely and prints "Running..." continuously.
6. Nested Loops
1. Write a program that prints a multiplication table (from 1 to 10) using nested loops.
2. Create a program that prints a triangle of stars, with each row having one more star than
the previous row. For example:
*
**
***
****