How To Use While In Dev C++

C while loop - A while loop statement repeatedly executes a target statement as long as a given condition is true. While Statement (C); 2 minutes to read +2; In this article. Executes statement repeatedly until expression evaluates to zero. Syntax while ( expression ) statement Remarks. The test of expression takes place before each execution of the loop; therefore, a while loop executes zero or more times.expression must be of an integral type, a pointer type, or a class type with an. C break and continue Statement In this article, you'll learn about C statements: break and continue. More specifically, what are they, when to use them and how to use them efficiently.

How to use while in dev c downloadLoops are used to repeat a block of code. Being able to have your programrepeatedly execute a block of code is one of the most basic but useful tasksin programming -- many programs or websites that produce extremely complexoutput (such as a message board) are really only executing a single task manytimes. (They may be executing a small number of tasks, but in principle, toproduce a list of messages only requires repeating the operation of reading insome data and displaying it.) Now, think about what this means: a loop lets you write a very simplestatement to produce a significantly greater result simply by repetition.

One Caveat: before going further, you should understand the concept ofC++'s true and false, because it will be necessary when working with loops(the conditions are the same as with if statements). There are three types ofloops: for, while, and do..while. Each of them has their specific uses. Theyare all outlined below.
How to use while in dev c download FOR - for loops are the most useful type. The syntax for a for loop is
The variable initialization allows you to either declare a variable and give it a value or give a value to an already existing variable. Second, the condition tells the program that while the conditional expression is true the loop should continue to repeat itself. The variable update section is the easiest way for a for loop to handle changing of the variable. It is possible to do things like x++, x = x + 10, or even x = random ( 5 ), and if you really wanted to, you could call other functions that do nothing to the variable but still have a useful effect on the code. Notice that a semicolon separates each of these sections, that is important. Also note that every single one of the sections may be empty, though the semicolons still have to be there. If the condition is empty, it is evaluated as true and the loop will repeat until something else stops it.
Example: This program is a very simple example of a for loop. x is set to zero, while x is less than 10 it calls cout<< x <<endl; and it adds 1 to x until the condition is met. Keep in mind also that the variable is incremented after the code in the loop is run for the first time.
WHILE - WHILE loops are very simple. The basic structure is

How To Use While Loop In Dev C++


while ( condition ) { Code to execute while the condition is true} The true represents a boolean expression which could be x 1 or while ( x != 7 ) (x does not equal 7). It can be any combination of boolean statements that are legal. Even, (while x 5 || v 7) which says execute the code while x equals five or while v equals 7. Notice that a while loop is the same as a for loop without the initialization and update sections. However, an empty condition is not legal for a while loop as it is with a for loop.
Example: This was another simple example, but it is longer than the above FOR loop. The easiest way to think of the loop is that when it reaches the brace at the end it jumps back up to the beginning of the loop, which checks the condition again and decides whether to repeat the block another time, or stop and move to the next statement after the block.
DO..WHILE - DO..WHILE loops are useful for things that want to loop at least once. The structure isNotice that the condition is tested at the end of the block instead of the beginning, so the block will be executed at least once. If the condition is true, we jump back to the beginning of the block and execute it again. A do..while loop is basically a reversed while loop. A while loop says 'Loop while the condition is true, and execute this block of code', a do..while loop says 'Execute this block of code, and loop while the condition is true'.
Example: Keep in mind that you must include a trailing semi-colon after the while in the above example. A common error is to forget that a do..while loop must be terminated with a semicolon (the other loops should not be terminated with a semicolon, adding to the confusion). Notice that this loop will execute once, because it automatically executes before checking the condition.

How To Use While In Dev C Full

Quiz yourself
Previous: If Statements
How To Use While In Dev C++Next: Functions
Back to C++ Tutorial Index
Advertising | Privacy policy |Copyright © 2019 Cprogramming.com | Contact | About

In the last tutorial we discussed for loop. In this tutorial we will discuss while loop. As discussed earlier, loops are used for executing a block of program statements repeatedly until the given loop condition returns false.

Syntax of while loop

How To Use While In Dev C File

How while Loop works?

In while loop, condition is evaluated first and if it returns true then the statements inside while loop execute, this happens repeatedly until the condition returns false. When condition returns false, the control comes out of loop and jumps to the next statement in the program after while loop.

Note: The important point to note when using while loop is that we need to use increment or decrement statement inside while loop so that the loop variable gets changed on each iteration, and at some point condition returns false. This way we can end the execution of while loop otherwise the loop would execute indefinitely.

Flow Diagram of While loop

How To Use While In Dev C 2017

While Loop example in C++

Output:

Infinite While loop

A while loop that never stops is said to be the infinite while loop, when we give the condition in such a way so that it never returns false, then the loops becomes infinite and repeats itself indefinitely.
An example of infinite while loop:
This loop would never end as I’m decrementing the value of i which is 1 so the condition i<=6 would never return false.

How To Use While In Dev C Youtube

Example: Displaying the elements of array using while loop

How To Use While In Dev C 5

Output: