C while and do.while Loop Loops are used in programming to repeat a specific block of code. In this article, you will learn to create while and do.while loops in C programming.
- C Programming Tutorial
- C Programming useful Resources
- Selected Reading
You may encounter situations, when a block of code needs to be executed several number of times. In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on.
Programming languages provide various control structures that allow for more complicated execution paths.
A loop statement allows us to execute a statement or group of statements multiple times. Given below is the general form of a loop statement in most of the programming languages −
Statement 1 sets a variable before the loop starts (int i = 0). Statement 2 defines the condition for the loop to run (i must be less than 5). If the condition is true, the loop will start over again, if it is false, the loop will end. Statement 3 increases a value (i) each time the code block in the loop has been executed. A for loop terminates when a break, return, or goto (to a labeled statement outside the for loop) within statement is executed. A continue statement in a for loop terminates only the current iteration. If cond-expression is omitted, it is considered true and the for loop will not terminate without a break, return, or goto within statement. The first statement in main sets n to a value of 10. This is the first number in the countdown. Then the while-loop begins: if this value fulfills the condition n0 (that n is greater than zero), then the block that follows the condition is executed, and repeated for as long as the condition (n0) remains being true. The whole process of the previous program can be interpreted according. The 'for' loop is essentially just another kind of loop, much like while. It, however, makes creating a loop which should loop a certain number of times really simple. Perhaps one of the best ways to show off the 'for' loop is to simply cycle through and output all the elements in an array.
C programming language provides the following types of loops to handle looping requirements.
Sr.No. | Loop Type & Description |
---|---|
1 | while loop Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body. |
2 | for loop Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable. |
3 | do..while loop Htttps dev-c.com gtav scripthookv. It is more like a while statement, except that it tests the condition at the end of the loop body. |
4 | nested loops You can use one or more loops inside any other while, for, or do.while loop. |
Loop Control Statements
Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed.
C supports the following control statements. Native instruments the mouth vst free download.
Sr.No. | Control Statement & Description |
---|---|
1 | break statement Terminates the loop or switch statement and transfers execution to the statement immediately following the loop or switch. |
2 | continue statement Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating. |
3 | goto statement Transfers control to the labeled statement. |
The Infinite Loop
A loop becomes an infinite loop if a condition never becomes false. The for loop is traditionally used for this purpose. Since none of the three expressions that form the 'for' loop are required, you can make an endless loop by leaving the conditional expression empty.
When the conditional expression is absent, it is assumed to be true. You may have an initialization and increment expression, but C programmers more commonly use the for(;;) construct to signify an infinite loop.
NOTE − You can terminate an infinite loop by pressing Ctrl + C keys.
![C++ C++](/uploads/1/3/4/2/134258770/804297190.jpg)
For Loop Statement In Dev C Pdf
-->Executes a statement repeatedly until the condition becomes false. For information on the range-based for statement, see Range-based for Statement (C++).
Syntax
for (
init-expression;
cond-expression;
loop-expression)
statement
;
Remarks
Use the for statement to construct loops that must execute a specified number of times.
The for statement consists of three optional parts, as shown in the following table.
for Loop Elements
Syntax Name | When Executed | Description |
---|---|---|
init-expression | Before any other element of the for statement, init-expression is executed only once. Control then passes to cond-expression . | Often used to initialize loop indices. It can contain expressions or declarations. |
cond-expression | Before execution of each iteration of statement , including the first iteration. statement is executed only if cond-expression evaluates to true (nonzero). | An expression that evaluates to an integral type or a class type that has an unambiguous conversion to an integral type. Normally used to test for loop-termination criteria. |
loop-expression | At the end of each iteration of statement . After loop-expression is executed, cond-expression is evaluated. | Normally used to increment loop indices. |
The following examples show different ways to use the for statement.
init-expression
and loop-expression
can contain multiple statements separated by commas. For example:loop-expression
can be incremented or decremented, or modified in other ways.A for loop terminates when a break, return, or goto (to a labeled statement outside the for loop) within
statement
is executed. A continue statement in a for loop terminates only the current iteration.If
cond-expression
is omitted, it's considered true
, and the for loop won't terminate without a break, return, or goto within statement
.Although the three fields of the for statement are normally used for initialization, testing for termination, and incrementing, they're not restricted to these uses. For example, the following code prints the numbers 0 through 4. In this case,
statement
is the null statement:for Loops and the C++ Standard
The C++ standard says that a variable declared in a for loop shall go out of scope after the for loop ends. For example:
By default, under /Ze, a variable declared in a for loop remains in scope until the for loop's enclosing scope ends.
/Zc:forScope enables standard behavior of variables declared in for loops without needing to specify
/Za
.It's also possible to use the scoping differences of the for loop to redeclare variables under
/Ze
as follows:This behavior more closely mimics the standard behavior of a variable declared in a for loop, which requires variables declared in a for loop to go out of scope after the loop is done. When a variable is declared in a for loop, the compiler internally promotes it to a local variable in the for loop's enclosing scope. It's promoted even if there's already a local variable with the same name.
See also
For Loop Statement In Dev C For Free
Iteration Statements
Keywords
while Statement (C++)
do-while Statement (C++)
Range-based for Statement (C++)
Keywords
while Statement (C++)
do-while Statement (C++)
Range-based for Statement (C++)