How do while loops work




















Print Pyramids and Patterns. Check prime number. Print the Fibonacci series. Reference Materials string. Start Learning C. Explore C Examples. Related Topics C for Loop. Count Number of Digits in an Integer. C break and continue.

Check Whether a Number is Prime or Not. C while and do In programming, loops are used to repeat a block of code until a specified condition is met. C programming has three types of loops. The while loop evaluates the testExpression inside the parentheses. If testExpression is true , statements inside the body of while loop are executed. Then, testExpression is evaluated again. The process goes on until testExpression is evaluated to false. If testExpression is false , the loop terminates ends.

Hence, the body of the while loop is executed. When the test expression becomes true, you stop the loop and go on with the next item in the program.

The three attributes of a test after loop are present. The action part consists of the 6 lines that prompt for data and then displays the total of the two ages. The test is the equality relational comparison of the value in the flag variable to the lower case character of y.

This type of loop control is called an event controlled loop. The flag updating is an event where someone decides if they want the loop to execute again. Using indentation with the alignment of the loop actions and flag update is the normal industry practice.

At this point, it is worth mentioning that good programming always provides for a method to ensure that the loop question will eventually be false so that the loop will stop executing and the program continues with the next line of code.

However, if this does not happen, then the program is in an infinite loop. Infinite loops are a bad thing. Consider the following code:. The programmer assigned a value to the flag before the loop and forgot to update the flag. Every time the test expression is asked it will always be true. Thus, an infinite loop because the programmer did not provide a way to exit the loop he forgot to update the flag.

No matter what the user replies during the flag update, the test expression does not do a relational comparison but does an assignment. Since all non-zero values are treated as representing true, the answer to the text question is true.

Viola, you have an infinite loop. If we know a specific number, such as 32, we can say 5 times, but for a given symbolic variable "NUMBER" which represents any number in the world, how many times is not known a priori before hand. In this case, we could use a while loop to determine that answer:.

The "pseudocode" for such an algorithm is: while the number is bigger than one keep dividing it by two. The advantage to a while loop is that it will go repeat as often as necessary to accomplish its goal. If the action inside the loop does not modify the variables being tested in the loops condition, the loop will "run" forever.



0コメント

  • 1000 / 1000