Your AI powered learning assistant

While Loop in C | C Programming While Loop with Examples | C Tutorial for Beginners in Hindi

While Executes Repeatedly; If Executes Once Both if and while depend on a condition that can be true or false. If executes its block once when the condition is true; otherwise it skips. While keeps executing as long as the condition remains true and stops immediately when it becomes false. Its behavior is precisely “continue only while the condition is true.”

Counting from 1 to 10 with While Initialize i = 1, set the condition i <= 10, print i, and then do i++. The loop prints 1, 2, 3, ... , 10 because after each print it increments and rechecks the condition. When i becomes 11, the condition fails and control exits the loop. Using if with the same check would print only once, since it does not repeat.

User-Defined Limits: Series up to N In series problems, N is the limit taken from the user and defines how far the loop should run. Replace a fixed bound with N so while(i <= N) prints from 1 up to that limit. Changing only the expression yields new series, such as i * i for 1, 4, 9, 16, 25 up to N. Initialization, condition, and increment/decrement are compulsory, and the step direction depends on the sequence.

Multiplication Tables with n * i A number’s table is produced by printing n * i while i runs from 1 to 10. With n = 5, the outputs are 5, 10, 15, ... , 50. This reuses the same loop skeleton used for counting and squares by changing only the printed expression. A dry run—manually stepping through values—makes each step and result clear.

Single-Statement Bodies, Post-Decrement, and “Infinite” Loops Without curly braces, while controls only the next single statement; if the update is outside, the loop can run forever. Embedding the update in the statement, such as printing i then using post-decrement (i--), prints the current value first and updates afterward, producing 10, 9, 8, ... down to 1. Any nonzero number used as the condition is treated as true, so while(10) appears infinite; with a 2-byte integer that cycles through 32767 to -32768 and eventually to 0, the loop stops when the value becomes zero. A loop is “infinite” only when nothing ever makes its condition false.