well, if you #include <iostream>, use namespace std, and put an int before main, you get... nothing.
Code:
jshao@MCP ~/Programming/C++/test $ g++ test.cpp -Wall -W -ansi -pedantic -o test.exe && ./test.exe
test.cpp: In function `int main()':
test.cpp:9: error: syntax error before `==' token
test.cpp:10: error: syntax error before `else'
that for loop is constructed wrong, and your parenthesis are messed up.
after fixing the loop and parenthesis and making it more readable, and adding comments, it becomes more clear:
Code:
#include <iostream>
using namespace std;
int main()
{
for (int i=0;i<8;i++)
{
if (i%2==0) //if the remainder of i/2 is zero
{
cout<<i+1<<'\t'; //output i+1 and a tab
}
else if (i%3 == 0) //if the remainder of i/3 is zero
{
cout<<i*i<<'\t'; //output i*i and a tab
}
else if (i%5 == 0) //if the remainder of i/5 is zero
{
cout<<2*i-1<<'\t'; //output (2*i)-1 and a tab
}
else //otherwise
{
cout<<i<<'\t'; //just output i
}
}
}
and now when you compile and run that, you get:
Code:
jshao@MCP ~/Programming/C++/test $ g++ test.cpp -Wall -W -ansi -pedantic -o test.exe && ./test.exe
1 1 3 9 5 9 7 7
basically, if i is an even number (in this case, 2,4,6,8), it'll add one to i, and if it's a multiple of three (in this case, only when i is 3), it'll square the nubmer, and when it's a multiple of 5 (in this case only when it's 5), it'll multiply i by 2 and subtract 1. That leaves you with 1 and 7, which just gets printed for what it is.