Society & Lifestyle
|Computing Articles||Share This Page|
The DO Statement
|by Sachin Mehta|
Last time, we embarked on a journey of loops. So let's continue from where we left.
The while statement discussed is an entry-controlled loop structure, in which the loop will not be executed if the test condition comes out to be false. But in some situations it might be necessary to execute the loop, even if the test-condition fails. So this kind of loops are called the exit-controlled loop structure. This takes the form:
ch is assumed to be a variable of type character. Now the loop is executed first under no test and the input by the user is outputted. Then the entered value would be tested by the condition for further advancement.
The FOR Statement:
The for loop is an entry-controlled loop that provides a more concise loop control structure. The general form of the for loop is:
for (initialization; test-condition ; increment)
The execution of the for statement is as follows:
The for loop will be executed 10 times. Each time a value equal to the current value of i will be printed out.
Additional Features of for Loop
1. More than one variable can be initialized at a time in the for statement. For example:
2. Like the initialization section, the increment section may also have more than one part.
3. It is also permissible to use expressions in the assignment statements of initialization and increment sections. For example
4. One or more sections in the for statement can be omitted, if necessary. For example
Both the initialization and increment sections are omitted in the for statement. In such cases, the sections are left blank. However, the semicolons separating the sections must remain. If the test-condition is not present, the for statement sets up an infinite loop.
|More by : Sachin Mehta|
|Views: 1808 Comments: 0|
|Top | Computing Articles|