There are 2 main types of loops in C++
the "for" loop and the "while" loop
for
This is the standard form of "for" loop
for( the first value of a certain variable ; the condition which will stop the loop if it is true; change the variable somehow)
this might look a bit complicated so lets show it on example
Lets try to count the sum of numbers from 1 to 100 with a loop.
Lets have a look at the "for" statement
for(i=1;i<=100;i++)
as I mentioned above
for statement consists of 3 smaller statements
i=1 this is the starting value of i
i<=100 when this condition will be true the system will exit the loop
i++ we need to change i every time (increase by 1) so that we can count the sum from 1 to 100 and our loop will not be infinite.
lets try to follow system for a few steps so
from the begining sum=0;
now we enter the loop
i=1;
add i to sum, sum became 1
so now i is less than 100 so the loop will continue
i=2
iis less than 100 so the loop is going on
sum=sum+2;
sum became 3
now i=3
i is still less than 100 so continue;
the while loop
the form
while(condition)
{
dosomething;
}
the while loop has no starting value of a variable or something like this, while has only a condition check the "while" loop will continue until his condition is true;we can count the sum from 1 to 100 using while too.
the "for" loop and the "while" loop
for
This is the standard form of "for" loop
for( the first value of a certain variable ; the condition which will stop the loop if it is true; change the variable somehow)
this might look a bit complicated so lets show it on example
Lets try to count the sum of numbers from 1 to 100 with a loop.
Lets have a look at the "for" statement
for(i=1;i<=100;i++)
as I mentioned above
for statement consists of 3 smaller statements
i=1 this is the starting value of i
i<=100 when this condition will be true the system will exit the loop
i++ we need to change i every time (increase by 1) so that we can count the sum from 1 to 100 and our loop will not be infinite.
lets try to follow system for a few steps so
from the begining sum=0;
now we enter the loop
i=1;
add i to sum, sum became 1
so now i is less than 100 so the loop will continue
i=2
iis less than 100 so the loop is going on
sum=sum+2;
sum became 3
now i=3
i is still less than 100 so continue;
the while loop
the form
while(condition)
{
dosomething;
}
the while loop has no starting value of a variable or something like this, while has only a condition check the "while" loop will continue until his condition is true;we can count the sum from 1 to 100 using while too.
The idea of counting the sum from 1 to 100 is the same its just written in a different way, the condition checks
while the i variable is less than 100 add i to sum. and also increase i by 1 every time.
there is also another way of using the while loop, the loop will begin then the condition will be checked.
The form is the following
do
{
actions;
}
while(condition);
you can create an infinite loops
by writing
for( ; ; )
"for" statement without conditions , or a starting value
2 important operators which can be used in loops
those 2 are "break" and "continue"
the break operator stops the loop no matter other conditions
the continue operator proceeds to the next step.
For making this more clear lets try to count the sum of numbers from 1 to 100 without any conditions in for loop
With the help of "break" operator the loop will exit when i will be bigger than 100.
Now lets count the sum of even numbers from 1 to 100 using the operator "continue".
As you can see basically every step is to add the variable i to the variable sum but the if(i%2==1) is doing the trick. If i modulo 2 is equal to 1 (aka the number is even) we just skip that step with the help of the operator "continue".
IF YOU HAVE ANY QUESTION LEAVE THEM IN COMMENTS, NO QUESTION WILL STAY UNANSWERED.
No comments:
Post a Comment