Choosing Between For and While Loops in C
The choice between a for loop and a while loop in C depends on the nature of the iteration. Use a for loop when the number of iterations is known beforehand, making it ideal for counting loops. Use a while loop when the number of iterations is unknown and depends on a condition evaluated at runtime.
Examples of Choosing Between For and While Loops
1. Using a For Loop When Iteration Count is Known
In this example, we print numbers from 1 to 5 using a for loop. Since we know that we need exactly 5 iterations, a for loop is the best choice.
main.c
#include <stdio.h>
int main() {
    // Using a for loop to print numbers from 1 to 5
    for (int i = 1; i <= 5; i++) {
        printf("%d ", i);
    }
    
    return 0;
}Explanation:
- The variable iis initialized to1before the loop starts.
- The loop continues as long as i <= 5, ensuring 5 iterations.
- After each iteration, iis incremented by1.
- Inside the loop, printf()prints the value ofi.
Output:
1 2 3 4 52. Using a while Loop When Iteration Depends on a Condition
In this example, we repeatedly take user input until they enter a negative number. Since the number of iterations is not known beforehand, a while loop is the best choice.
main.c
#include <stdio.h>
int main() {
    int number;
    // Ask for input at least once
    printf("Enter a number (negative to stop): ");
    scanf("%d", &number);
    // Loop until the user enters a negative number
    while (number >= 0) {
        printf("You entered: %d\n", number);
        
        // Ask for input again
        printf("Enter a number (negative to stop): ");
        scanf("%d", &number);
    }
    printf("Loop terminated because a negative number was entered.\n");
    return 0;
}Explanation:
- We declare an integer variable numberto store user input.
- The first scanf()is used to take an initial input.
- The whileloop runs as long asnumberis non-negative.
- Inside the loop, the number is printed, and the user is prompted again.
- When the user enters a negative number, the loop terminates.
Output (Sample Run):
Enter a number (negative to stop): 10
You entered: 10
Enter a number (negative to stop): 20
You entered: 20
Enter a number (negative to stop): -1
Loop terminated because a negative number was entered.3. Using a While Loop for User Authentication
In this example, we repeatedly ask the user for a password until they enter the correct one. The number of attempts is unknown, making a while loop a better choice.
main.c
#include <stdio.h>
#include <string.h>
int main() {
    char password[20];
    char correctPassword[] = "Csecure";
    // Ask user for password
    printf("Enter password: ");
    scanf("%s", password);
    // Keep asking until correct password is entered
    while (strcmp(password, correctPassword) != 0) {
        printf("Incorrect password. Try again: ");
        scanf("%s", password);
    }
    printf("Access granted.\n");
    return 0;
}Explanation:
- We declare two character arrays: passwordfor user input andcorrectPasswordas the predefined password.
- The user is prompted to enter a password.
- The whileloop continues as long asstrcmp(password, correctPassword) != 0, meaning the entered password is incorrect.
- When the user enters the correct password, the loop exits and prints “Access granted.”
Output (Sample Run):
Enter password: hello
Incorrect password. Try again: Csecure
Access granted.Conclusion
The decision between using a for loop or a while loop depends on the nature of the iteration:
- Use a forloop when the number of iterations is predetermined.
- Use a whileloop when the iteration count is unknown and depends on a condition.
By understanding these differences, you can write more efficient and readable C programs.
