Loops are used to repeat a block of code. Being able to have your program repeatedly execute a block of code is one of the most basic but useful tasks in programming -- many programs or websites that produce extremely complex output (such as a message board) are really only executing a single task many times. (They may be executing a small number of tasks, but in principle, to produce a list of messages only requires repeating the operation of reading in some data and displaying it.) Now, think about what this means: a loop lets you write a very simple statement to produce a significantly greater result simply by repetition.
The syntax for a repeating for loop in C++ follows this pattern for
(StartingPoint; ConditionalExpression; Increment)
{
DoStatementOne;
DoStatementTwo;
}
If there is only a single statement to be repeatedly executed within the loop the curly braces are not required.
The loop will execute as long as the conditional expression is non-zero. The loop begins at Starting point, and each run through the loop the starting value is changed by the amount specified by increment.
An example of what this looks like in practice
for (int x=1;x<20;x++)
{
DoSomething;
DoNextThing;
}
In the above example an integer variable x is declared within the loop definition and is set to the initial value of 1. This is different from Delphi where the variable x would have to be declared using the var statement before the beginning of the code segment. The statement 'x++' indicates that x will be incremented by 1 each time through the loop, and as long as x is less than 20 the loop will continue (terminating when x reaches the value of 20).
The loop could run backwards by substituting 'x--' in place of 'x++'...
for (int x=20; x>0;x--)
{
the code is here;
}
In this case the loop begins at 20, and each iteration the value of x is reduced by one and the loop terminates at 0 (continues as long as x is greater than 0).
Variables can also be used int he conditional section of the loop declaration and the loop can be incremented or decremented by an amount other than 1 using the following syntax...
for (int x=10;x>y;x+=4)
{
code hre;
}
In this example the x begins at 10 and each iteration x is increased by adding four and the loop terminates when x becomes greater than y. Similarly the expression x-=4 would decrease x by four each time throught the loop.
EXAMPLE...... A simple loop ?
#include <iostream>
using namespace std;
int main ()
{
for (int n=10;n>0;n--)
cout<<n<<endl;
return 0;
}
While loop
The while loop executes as long as the conditional expression remains true (therefore it is required that the code within the loop manipulates the conditional expression in such a way as to eventually result in the stopping condition so that the loop does not execute forever...)
int x;
while (x < 10)
{
DoSomething;
x = SomeCodeResult;
}
Once again the syntax for declaring a variable is different than in Delphi in this example as x is declared as an integer just before being used in the loop. The loop code manipulates the variable x and should eventually return a result that makes x greater than or equal to ten so that the loop terminates at some point.
A variation of the code manipulates the variable within the loop declaration itself.
int x = 10;
while (x++ < 20)
{
Dosome code here;
}
Here x is set to the value 10 and then incremented by one each time through the loop with the loop terminating when x reaches the value 20.
DO While loop
C++ also has a 'do - while' loop structure that mimics the 'repeat - until' structure in Delphi.
int x = 10;
do
{
code is here;
x = something;
} while (x < 20);
This is similar to the following Delphi code (with the difference that in Delphi the variable x would be declared as integer at the beginning of the function or procedure)...
x := 10;
repeat
do some code;
x := something;
until (x >= 20);
While loop
The while loop executes as long as the conditional expression remains true (therefore it is required that the code within the loop manipulates the conditional expression in such a way as to eventually result in the stopping condition so that the loop does not execute forever...)
int x;
while (x < 10)
{
DoSomething;
x = SomeCodeResult;
}
Once again the syntax for declaring a variable is different than in Delphi in this example as x is declared as an integer just before being used in the loop. The loop code manipulates the variable x and should eventually return a result that makes x greater than or equal to ten so that the loop terminates at some point.
A variation of the code manipulates the variable within the loop declaration itself.
int x = 10;
while (x++ < 20)
{
Dosome code here;
}
Here x is set to the value 10 and then incremented by one each time through the loop with the loop terminating when x reaches the value 20.
EXAMPLE...... related to while loop?
#include <iostream>
using namespace std;
int main ()
{
int i=0;
while (i<=20)
{
cout<<i<<" ";
i=i+5;
}
cout <<endl;
}
DO While loop
C++ also has a 'do - while' loop structure that mimics the 'repeat - until' structure in Delphi.
int x = 10;
do
{
code is here;
x = something;
} while (x < 20);
This is similar to the following Delphi code (with the difference that in Delphi the variable x would be declared as integer at the beginning of the function or procedure)...
x := 10;
repeat
do some code;
x := something;
until (x >= 20);