#include <iostream>
usingnamespace std;
int main (int argc, char **argv)
{
for (int i = 1; i <= 20; i++)
if (i != 3 && i != 11 && i != 16)
cout << i << “ “;
return 0;
}
For loops are probably a better option since we're dealing with an iterator ( i ), but there is also the while loop as an option.
Jumper007's code uses the and (&&) signs in his if statement, I used or (||) signs.
They both work in this instance because jumper's code also uses the not equal sign (!=). Basically his code is saying if i doesn't equal 3 and i doesn't equal 11 and i doesn't equal 16 then print out the number.
My version says if i does equal 3 or 11 or 16 then tell them a number is skipped, otherwise print out the number.
1 2 3 4 5 6 7 8 9 10 11 12
int i=0;
while(i<=20)
{
if(i==3 || i==11 || i==16)
{
cout<<"A number was skipped"<<endl;
}
else
{
cout<<i<<endl;
}
}
This is also an example of difference in syntax, mine isn't exactly wrong, but the the extra brackets I used aren't necessary after the if and else functions since there is only one action line after both of them. I prefer using brackets because it helps me organize my code when I'm skimming it.