| View previous topic :: View next topic |
| Author |
Message |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Sat Apr 11, 2009 10:15 am Post subject: |
|
|
| switch case might be more readable in this case |
|
| Back to top |
|
 |
talkerzero Grandmaster Cheater
Reputation: 1
Joined: 24 Jul 2008 Posts: 560 Location: California
|
Posted: Sat Apr 11, 2009 10:39 am Post subject: |
|
|
| And, it's a good idea to always try to avoid using goto. |
|
| Back to top |
|
 |
manc Grandmaster Cheater
Reputation: 1
Joined: 16 Jun 2006 Posts: 551
|
|
| Back to top |
|
 |
VolatileAce Cheater
Reputation: 0
Joined: 19 Mar 2009 Posts: 30 Location: 0001001100110111
|
Posted: Sat Apr 11, 2009 11:54 am Post subject: |
|
|
| mydogjax wrote: | | Ok, so if how do i make it loop? Whats the syntax for it? |
Say I want to simply print out all even numbers from 0 up to 20.
You could, do this:
| Code: | int i = 0;
cout << i << endl;
cout << (i+=2) << endl; //2
cout << (i+=2) << endl; //4
cout << (i+=2) << endl; //6
cout << (i+=2) << endl; //8
cout << (i+=2) << endl; //10
cout << (i+=2) << endl; //12
cout << (i+=2) << endl; //14
cout << (i+=2) << endl; //16
cout << (i+=2) << endl; //18
cout << (i+=2) << endl; //20 |
However, as you can see, repeated code is extremely ugly (and waste of space), therefore we resort to other approaches.
While using goto is usually discouraged, sometimes it could be useful. (goto require you to make up some sort of name for that place to "go to", in this case the I named it "loop")
| Code: | loop:
if(i <= 20){
cout << i << endl;
i += 2;
goto loop;
} |
If you do it with a while loop, it would look something like this:
| Code: | int i = 0;
while(i <= 20){
cout << i << endl;
i += 2;
} |
There is a variation of the while loop call the do-while loop, this type of while loop is designed so that it will always at least execute once.
Here's a way to do it with the do-while loop.
Notice unlike other loops, there is a semicolon following the while.
| Code: | int i = 0;
do{
cout << i << endl;
i += 2;
}while(i <= 20); |
which could be simplified into:
| Code: | int i = 0;
do{
cout << i << endl;
}while((i+=2) <= 20); |
And last, the for loop, which is basically a "extra featured while loop".
The for loop takes in 3 "parameter", the "initial statement", "condition", and "post statement", I'll explain this after I show you the example:
| Code: | for(int i = 0; i <= 20; i+=2){
cout << i << endl;
} |
Technically you don't have to put a variable declaration in the first statement, in fact, you can even leave it blank. (However, the for loop is mainly used as a counter loop so if you are not going to use these you might as well just stick with a while loop. Also notice the first statement of a for loop will be executed only once) The middle is the condition, its what you would put as if its the condition you put in a while loop. Last is the post statement, this statement is executed every time the loop is going to be restarted again.
Hope this helps, ask if you have further questions. |
|
| Back to top |
|
 |
Black Ghost Advanced Cheater
Reputation: 0
Joined: 05 Aug 2008 Posts: 68
|
Posted: Sat Apr 11, 2009 1:07 pm Post subject: |
|
|
This might be nub Q...
What compiler did u all using ? |
|
| Back to top |
|
 |
manc Grandmaster Cheater
Reputation: 1
Joined: 16 Jun 2006 Posts: 551
|
Posted: Sat Apr 11, 2009 1:24 pm Post subject: |
|
|
| Black Ghost wrote: | This might be nub Q...
What compiler did u all using ? |
http://www.microsoft.com/express/vc/
or you can *cough cough* download (torrent/warez) the full Microsoft Visual Studios |
|
| Back to top |
|
 |
Black Ghost Advanced Cheater
Reputation: 0
Joined: 05 Aug 2008 Posts: 68
|
Posted: Sat Apr 11, 2009 1:42 pm Post subject: |
|
|
@manc
aren't that for c++ .NET? |
|
| Back to top |
|
 |
manc Grandmaster Cheater
Reputation: 1
Joined: 16 Jun 2006 Posts: 551
|
Posted: Sat Apr 11, 2009 1:48 pm Post subject: |
|
|
Sorry I don't know much about the whole .NET schemes.
I have used both the compiler in the link I provided for regular c++ compiling, as well as the full version. They both work fine with standard c/c++ code and don't have anything to do with .NET (as far as I know). |
|
| Back to top |
|
 |
Black Ghost Advanced Cheater
Reputation: 0
Joined: 05 Aug 2008 Posts: 68
|
Posted: Sat Apr 11, 2009 2:03 pm Post subject: |
|
|
k ty =].
rep+ for being helpful and nice. |
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Sat Apr 11, 2009 2:34 pm Post subject: |
|
|
| Black Ghost wrote: | @manc
aren't that for c++ .NET? |
You need to also download the Windows Platform SDK for native C/C++ |
|
| Back to top |
|
 |
|