Spartan025's - C++ Loops
Posted: Mon Oct 16, 2006 8:40 pm
C++ Loops
This explains 2 simple C++ loops , and how to do them right.
First, what is a Loop?
A Loop is a statement that executes over and over as long as
the condition is true. An example would be:
Looking More Closely At A Loop
1. Set i = 1
2. While i is <= n
3. Print i and leave a space in between
4. Add 1 to i.
This looks very confusing but I will explain all.
There are two loop statements you can use for C++.
The While statement, and the for statment.
This diagram shows a basic While statement.
This basically means, If the condition is true, keep going,
if not, the loop ends.
Printing the numbers 1 to N
This program example prints 1 to N in order.
WhileLoop.cpp
How does the program work?
1. A varible with the name n and type int is created.
2. The program prompts for the user to input a number
from the keyboard. Example:10
3. 10 is stored in the variable n.
4. i is initilized to 1.
5. A while statement is used for a loop
5a. While i(1) is less than or equal to n(10)
5b. Print 1
5c. Add 1 to i.
Since i is still onlt 2, and is less than 10,
the loop is executed again.
This keeps going unitl it reaches 10, and i becomes 11 and
the loop is no longer true, so the loop is over.
The Result:
1 2 3 4 5 6 7 8 9 10
*NOTE
There is also such things called Infinite Loops.
This is when the condition is always true and keeps executing.
If this happens, quickly exit the program and look what you did with
your code. You could also use the break statement
to exit the current loop.
Step 5c. is also known as incrementing. since it is common to add 1 to a
variable, a special operator, i++ was made.
Here is a list of the 4 increment/decrement operators
The For Statement
Like the while statment, the for statement is a loop statement.
The for statement is more organized and flexible. The for statement is
organized like this:
The for statment is broken down to this:
for (initializer;condition;increment) {
statements
}
The for statements also works like the while statement, except you can
also declare varibles during a loop and initialize it. This is an
example of a program similar to the print 1 to n, except its 1-10
to save time.
This program prints 1 - 10 just like the other program.
In a for loop, you do not need all 3 steps,
initializer, condition, increment. If you do this, the compiler will ignore it
and the loop will become an infinite loop, so watch how you set up your for loops.
Example
Why C++ Loops are better?
In VB loops are like this
Although this is shorter, the for and while loops are much better, and can do so
much more.
There is also another loop called do-while and will post about at a later time
This basically wraps up the lesson on loops, if i have enough time, I will contribute
another lesson.
This explains 2 simple C++ loops , and how to do them right.
First, what is a Loop?
A Loop is a statement that executes over and over as long as
the condition is true. An example would be:
Code: Select all
// A simple Loop
i = 1 // "i" is initialized to 1
while (i <= n) { //While i is less than or equal to
cout << i << " "; // Print i
i++ // Add 1 to i
}
1. Set i = 1
2. While i is <= n
3. Print i and leave a space in between
4. Add 1 to i.
This looks very confusing but I will explain all.
There are two loop statements you can use for C++.
The While statement, and the for statment.
This diagram shows a basic While statement.
Code: Select all
while (condition) {
c++ statements
}
if not, the loop ends.
Printing the numbers 1 to N
This program example prints 1 to N in order.
WhileLoop.cpp
Code: Select all
#include <iostream>
using namespace std;
int main() {
int n;
int i;
// Have the user enter a number.
cout << "Type in a number and press ENTER: ";
cin >> n;
// The number is stored in the int variable n.
//while loop
i = 1; //i, the loop counter is initilized to 1.
while (i <= n) {
cout << i << " "; // Print i
i++ // Add i to 1
}
return 0;
}
1. A varible with the name n and type int is created.
2. The program prompts for the user to input a number
from the keyboard. Example:10
3. 10 is stored in the variable n.
4. i is initilized to 1.
5. A while statement is used for a loop
5a. While i(1) is less than or equal to n(10)
5b. Print 1
5c. Add 1 to i.
Since i is still onlt 2, and is less than 10,
the loop is executed again.
This keeps going unitl it reaches 10, and i becomes 11 and
the loop is no longer true, so the loop is over.
The Result:
1 2 3 4 5 6 7 8 9 10
*NOTE
There is also such things called Infinite Loops.
This is when the condition is always true and keeps executing.
If this happens, quickly exit the program and look what you did with
your code. You could also use the
Code: Select all
break;
to exit the current loop.
Step 5c. is also known as incrementing. since it is common to add 1 to a
variable, a special operator, i++ was made.
Here is a list of the 4 increment/decrement operators
Code: Select all
i++ // Return result then add 1 to i
++i // Add 1 to i then return result
i-- // Return result then subtract 1 from i
--i // subtract 1 from i then return result.
Like the while statment, the for statement is a loop statement.
The for statement is more organized and flexible. The for statement is
organized like this:
Code: Select all
for (i = 1;i <= 10;i++) {
cout << i << " "
}
for (initializer;condition;increment) {
statements
}
The for statements also works like the while statement, except you can
also declare varibles during a loop and initialize it. This is an
example of a program similar to the print 1 to n, except its 1-10
to save time.
Code: Select all
#include <iostream>
using namespace std;
int main() {
for (int i = 1;i <= 10; i++) {
cout << i << " ";
}
return 0;
}
In a for loop, you do not need all 3 steps,
initializer, condition, increment. If you do this, the compiler will ignore it
and the loop will become an infinite loop, so watch how you set up your for loops.
Example
Code: Select all
for () {
// Infinite loop, oops
}
In VB loops are like this
Code: Select all
'A simple loop
For i = 1 To 10
Print i
Next i
much more.
There is also another loop called do-while and will post about at a later time
This basically wraps up the lesson on loops, if i have enough time, I will contribute
another lesson.