How does while loop work




















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. Skip to content Overview A do while loop is a control flow statement that executes a block of code at least once, and then repeatedly executes the block, or not, depending on a given boolean condition at the end of the block.

Understanding Iteration in General — do while The concept of iteration is connected to possibly wanting to repeat an action. The basic form of the do while loop is as follows: do some statements or action some statements or action some statements or action update the flag while the answer to the question is true In most programming languages the question called a test expression is a Boolean expression. Understanding Iteration in General — repeat until The concept of iteration is connected to possibly wanting to repeat an action.

The basic form of the repeat until loop is as follows: repeat some statements or action some statements or action some statements or action update the flag until the answer to the question becomes true In most programming languages the question called a test expression is a Boolean expression.

An Example Do Output "What is your age? Infinite Loops 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.

In while loop, a condition is evaluated before processing a body of the loop. If a condition is true then and only then the body of a loop is executed. After the body of a loop is executed then control again goes back at the beginning, and the condition is checked if it is true, the same process is executed until the condition becomes false. Once the condition becomes false, the control goes out of the loop. After exiting the loop, the control goes to the statements which are immediately after the loop.

The body of a loop can contain more than one statement. If it contains only one statement, then the curly braces are not compulsory. It is a good practice though to use the curly braces even we have a single statement in the body. In while loop, if the condition is not true, then the body of a loop will not be executed, not even once.

It is different in do while loop which we will see shortly. The above program illustrates the use of while loop. In the above program, we have printed series of numbers from 1 to 10 using a while loop. A do…while loop in C is similar to the while loop except that the condition is always executed after the body of a loop.

It is also called an exit-controlled loop. As we saw in a while loop, the body is executed if and only if the condition is true. In some cases, we have to execute a body of the loop at least once even if the condition is false. This type of operation can be achieved by using a do-while loop. In the do-while loop, the body of a loop is always executed at least once.

After the body is executed, then it checks the condition. If the condition is true, then it will again execute the body of a loop otherwise control is transferred out of the loop.

Similar to the while loop, once the control goes out of the loop the statements which are immediately after the loop is executed. The critical difference between the while and do-while loop is that in while loop the while is written at the beginning. In do-while loop, the while condition is written at the end and terminates with a semi-colon ;.

Pointers in C. Find roots of a quadratic equation. 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.



0コメント

  • 1000 / 1000