C While Loop

C While Loop: Repeating Code Efficiently

In programming, you often need to execute a block of code multiple times. Writing the same code over and over again is inefficient and prone to errors. This is where loops come in.

Loops allow you to execute a specific block of code repeatedly as long as a specified condition remains true. The while loop is one of the most fundamental looping structures in the C programming language.


1. How the while Loop Works

The while loop checks a condition before it executes the code block. If the condition evaluates to true (a non-zero value), the code inside the loop runs. After the block runs, the condition is checked again. This process repeats until the condition becomes false (evaluates to 0).

Syntax:

while (condition) {
  // Code to be executed
  // Make sure to update a variable here so the condition eventually becomes false!
}

Basic While Loop Example

#include <stdio.h>

int main() { int i = 1; // 1. Initialization

// 2. Condition check while (i <= 5) { printf("The value of i is: %d\n", i); // 3. Increment/Update (Crucial step!) i++; }

printf("Loop finished!\n"); return 0; }

The Three Pillars of a Good Loop:

  1. Initialization: Setting a starting value for your counter variable before the loop starts (e.g., int i = 1;).
  2. Condition: The test that determines if the loop should keep running (e.g., i <= 5).
  3. Update: Changing the counter variable inside the loop so the condition will eventually fail (e.g., i++).

2. The Danger of Infinite Loops

An infinite loop is a loop that never stops running. This happens when the loop condition never evaluates to false. In most cases, this is a bug caused by forgetting to update the counter variable.

An infinite loop will freeze your program and consume CPU resources until the program is forcefully terminated.

Infinite Loop Example (DO NOT RUN!)

#include <stdio.h>

int main() { int x = 1;

while (x < 5) { printf("This will print forever!\n"); // We forgot x++ here! x will ALWAYS be 1. }

return 0; }

Pro Tip: Always double-check that your while loop has a clear path to finishing. Verify that your variables are incrementing or decrementing correctly.


3. The do...while Loop

The do...while loop is a variant of the standard while loop. The key difference is that a do...while loop checks its condition at the bottom of the loop, not the top.

This guarantees that the code inside the loop will execute at least once, regardless of whether the condition is true or false initially.

Syntax:

do {
  // Code to be executed
} while (condition);

The do...while Example

#include <stdio.h>

int main() { int i = 10;

// Even though 10 is NOT less than 5, this will run once! do { printf("i is %d\n", i); i++; } while (i < 5);

return 0; }

When to use do...while: It is extremely useful when taking user input. You want to prompt the user at least once, and then keep asking while the input is invalid.


4. Practical Example: Sum of Numbers

Let's use a while loop to write a program that calculates the sum of all positive integers entered by a user until they enter a negative number or zero.

Real World Example: Calculating Sum

#include <stdio.h>

int main() { int number = 1; // Initialized to a positive value to enter the loop int sum = 0;

printf("Enter numbers to add (enter 0 or negative to stop):\n");

while (number > 0) { scanf("%d", &number); if (number > 0) { sum = sum + number; number--; } }

printf("The total sum is: %d\n", sum); return 0; }


5. Best Practices & GSC Optimization

To write clean, robust C code and rank well in technical assessments, consider these tips:


Exercise 1 of 2

?

What guarantees that a loop's block of code will execute at least once?

Exercise 2 of 2

?

What is the most common cause of an infinite loop?