While-Statement-In-C
INTRODUCTION
The while statement is a control flow statement used in C programming that enables you to repeatedly run a block of code while a specific condition is proper.
The while statement's basic syntax is as follows:
while (condition) {
// Code to be executed repeatedly
}
Components of while loop
- The while statement is introduced with the while keyword.
- You give a condition that decides that the loop should continue or not inside the parenthesis. Any expression that tests to be true or false can satisfy this requirement.
- The central part of the while loop is a piece of code encased in curly braces. As long as the criterion is valid, the sentences will be repeated.
EXAMPLE 1
#include <stdio.h>
int main() {
int num = 1;
while (num <= 10) {
printf("%d ", num);
num++;
}
printf("\nLoop finished!");
return 0;
}
OUTPUT
![WHILE STATEMENT IN C](https://static.tutorialandexample.com/c/while-statement-in-c1.png)
- We define and initialize the integer variable num, which will be used as our counter inside of the primary function.
- The while keyword introduces the while loop, immediately followed by the parameter enclosed in parentheses. The loop will continue to run in this example as long as the value of num is below or equal to 10, as the condition is num = 10.
- The current value of a number is printed to the terminal using printf inside the while loop. Then, using the num++ statement, we increase the value of num by 1.
- The condition remains true until num equals 11, when the loop ends. We print "Loop finished!" after the while loop to denote the conclusion of the loop.
EXAMPLE 2
#include <stdio.h>
int main() {
int number = 1;
while (number <= 50) {
printf("%d ", number);
if (number % 10 == 0) {
printf("\n");
}
number++;
}
return 0;
}
OUTPUT
![WHILE STATEMENT IN C](https://static.tutorialandexample.com/c/while-statement-in-c2.png)
- In the primary function we declare and initialize a variable with an integer number to 1, the current value.
- Using the while keyword and the condition number = 50, the while loop is established, ensuring that it runs as long as the number is below or equal to 50.
- We first use printf inside the while loop to output the present value of a number. The modulo operator% is then used to see if the value is divisible by 10. If number% 10 == 0 is true, it indicates that the number is divisible by 10, and we display the character for a new line, "n."
- The number++ command is then used to increase the value of the integer by 1.
- The cycle repeats until the number hits 51 when the condition is no longer valid, and the cycle ends.
ADVANTAGES
- Flexibility is made possible via the while loop, which lets you repeatedly run a block of code depending on a condition. This condition may be depend on user input, estimations, or changes in the program's state, among other things.
- Using the while loop, you can repeat a series of sentences if a particular condition holds. It makes it appropriate for situations where you must act until a specific condition is met without knowing how many iterations will be required.
- A while loop permits dynamic looping, unlike a for loop, which necessitates a predetermined number of repeats. As a result, the number of iterations may vary depending on the runtime circumstances.
- While it's not necessarily a good idea, it is possible to make a while loop indefinitely purposely. It is helpful for operating servers or programs that continually monitor user input / events until expressly halted.
DISADVANTAGES
- While loops have the potential to result in infinite loops, which is one of their real downsides, it is possible for the while loop to never end if the condition inside it needs to be correctly managed or updated. Your program can become inaccessible or crash as a result of this.
- To prevent off-by-one errors, it's crucial to make sure that the condition inside the while loop is defined correctly. A poorly written condition could lead to unexpected behaviour, such as ignoring the initial or last repetition or repeatedly running the loop.
- While the while loop's flexibility can be useful, it lacks iteration control. It might occasionally make it more challenging to comprehend and control the loop flow.
- While loops are susceptible to logical errors since they depend on conditions, this is a problem. It can lead to improper behaviour or undesired results if the condition is correctly defined or if the parameters included must be updated within the loop.
CONCLUSION
While loops enable you to repeat a block of code depending on a condition, they also give you freedom and control in programming. Loops can be practical and straightforward, but being aware of any dangers is crucial. These include the potential for logical mistakes, off-by-one errors, the risk of infinity loops, and difficulties managing complicated loop conditions. You may maximize the benefits of while loops while minimizing any potential negatives by carefully controlling the loop circumstances, checking the termination requirements, and thoroughly testing the code.