Spartan025's - C++ Loops

Post here about scripting and programming for HaloPC (audio, network, ai, etc.)
Post Reply
Spartan025





Posts: 13
Joined: Fri Oct 13, 2006 3:45 pm
Location: Learning C++

Spartan025's - C++ Loops

Post by Spartan025 »

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:

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
}
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.

Code: Select all

while (condition) {
	c++ statements
	}
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

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;
}
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

Code: Select all

break;
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

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.
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:

Code: Select all

for (i = 1;i <= 10;i++) {
cout << i << " "
}
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.

Code: Select all

#include <iostream>
using namespace std;

int main() {
for (int i = 1;i <= 10; i++) {
cout << i << " ";
}
return 0;
}
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

Code: Select all

for () {
// Infinite loop, oops
}
Why C++ Loops are better?
In VB loops are like this

Code: Select all

 'A simple loop 
For i = 1 To 10
Print i
Next i
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.
Last edited by Spartan025 on Sun Mar 18, 2007 12:32 pm, edited 2 times in total.
User avatar
dos mes





Posts: 2158
Joined: Thu Dec 29, 2005 9:58 pm
Location: Syracuse, NY

Post by dos mes »

Too bad you don't give credit to whoever actually wrote this...
Spartan025





Posts: 13
Joined: Fri Oct 13, 2006 3:45 pm
Location: Learning C++

Post by Spartan025 »

what r u takin abiut, i wirote it myself
User avatar
dos mes





Posts: 2158
Joined: Thu Dec 29, 2005 9:58 pm
Location: Syracuse, NY

Post by dos mes »

Except for the fact that I got almost the exact if not the exact same tutorial on a google search a few months ago, when I started learning C++...

And there are other reasons...
stephen10101





Posts: 140
Joined: Sat Jul 29, 2006 9:11 pm
Location: Klein, TX

Post by stephen10101 »

this does seem oddly familiar to something i have seen as well...
Spartan025





Posts: 13
Joined: Fri Oct 13, 2006 3:45 pm
Location: Learning C++

Post by Spartan025 »

why can't you accept me for who i am! :(

Code: Select all

#include <iostream>
using namespace std;

int main() {
        cout << "Initiate Control Room Console" << endl;
        cout << "Initiate functions......" << endl
        system("PAUSE");
        return 0;
}
Post Reply