Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old May 26, 2009, 11:09 AM   #1
pongster08
macrumors newbie
 
Join Date: May 2009
For Loop problem

Hi im trying to make a for loop in Xcode
Please look at code below, when i run it all i get is
__________________________________

[Session started at 2009-05-26 12:09:18 -0400.]
9
The Debugger has exited with status 0.
___________________________________




#include <iostream>
#include <cmath>
using namespace std;

int main()
{
int i;
for (i=0; i<9; i++);
{
cout << i;
}


}

________________

Thank you!
pongster08 is offline   0 Reply With Quote
Old May 26, 2009, 11:14 AM   #2
mesa
Banned
 
Join Date: Jan 2005
Location: Washington, DC
The output is correct. You are looping over nothing (";") until i is incremented to 9. Then you print out the value of i. You need to remove the semicolon before the block.
mesa is offline   0 Reply With Quote
Old May 26, 2009, 11:17 AM   #3
pongster08
Thread Starter
macrumors newbie
 
Join Date: May 2009
Thank you!

easy and clear ur response was. Thank you!
pongster08 is offline   0 Reply With Quote
Old May 26, 2009, 01:46 PM   #4
HyperSnake
macrumors member
 
Join Date: Jan 2009
Location: Switzerland
Code:
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
int i;
for (i=0; i<9; i++);
{
	cout << i;
}


}
Take away the semi-colon after the paranthesis at the end of the for line.

Ben.
__________________
My App Store app: Vittigheder - Danish humor and jokes.
HyperSnake is offline   0 Reply With Quote
Old May 26, 2009, 02:54 PM   #5
autorelease
macrumors regular
 
Join Date: Oct 2008
Location: Achewood, CA
Everyone makes this mistake when learning, so I thought I'd explain a little more as to what this is doing.

A semicolon by itself basically means "do nothing." It is an empty statement. It is the same as curly braces with nothing inside them. {}

So if you put a semicolon after the first line of a for loop (or a while loop, or an if statement, etc.), what you're really writing is
Code:
for (i=0; i<9; i++)
{
}
{
	cout << i;
}
The code in bold is what the loop is actually doing. The cout has been "pushed out" of the for loop, and it will only execute once, after the entire loop is finished.
__________________
*** -[NSAutoreleasePool autorelease]: Cannot autorelease an autorelease pool
autorelease is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
auto reply email loop problem tomf OS X 10.8 Mountain Lion 1 Apr 25, 2014 09:42 AM
Recovery Loop | Cant get iPhone 5 6.1.3 out of loop Fatush iPhone Tips, Help and Troubleshooting 1 Sep 19, 2013 09:14 PM
iPhone 3GS boot loop & battery charge problem Kokiron iPhone Tips, Help and Troubleshooting 20 Jun 30, 2013 06:42 PM
iPhone: iPHone 4 stuck in DFU loop then boot loop then DFU loop! urbanmacUser Jailbreaks and iOS Hacks 1 Jun 7, 2013 04:30 PM
iPhone 4 Stuck on restore loop, Help! I'm stuck on anger and frustration loop! vegas60 iPhone Tips, Help and Troubleshooting 3 Nov 8, 2012 08:49 AM

Forum Jump

All times are GMT -5. The time now is 04:18 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC