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

</>
Copy
#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:

  1. The variable i is initialized to 1 before the loop starts.
  2. The loop continues as long as i <= 5, ensuring 5 iterations.
  3. After each iteration, i is incremented by 1.
  4. Inside the loop, printf() prints the value of i.

Output:

1 2 3 4 5

2. 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

</>
Copy
#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:

  1. We declare an integer variable number to store user input.
  2. The first scanf() is used to take an initial input.
  3. The while loop runs as long as number is non-negative.
  4. Inside the loop, the number is printed, and the user is prompted again.
  5. 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

</>
Copy
#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:

  1. We declare two character arrays: password for user input and correctPassword as the predefined password.
  2. The user is prompted to enter a password.
  3. The while loop continues as long as strcmp(password, correctPassword) != 0, meaning the entered password is incorrect.
  4. 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:

  1. Use a for loop when the number of iterations is predetermined.
  2. Use a while loop when the iteration count is unknown and depends on a condition.

By understanding these differences, you can write more efficient and readable C programs.