C++ if statements

Status
Not open for further replies.

theopfor

In Runtime
Messages
173
Location
Right behind you
My if statements are having problems. I keep examples of things when learning a new language.
Code:
int n=3;

    if (n==3); //this is an if statement

        cout<<"If statement inside"<<endl;
        cout<<"dot dot dot"<<endl;
        cout<<"We are inside an else if statement"<<endl;

    else if (n>2);     //PROBLEM LINE
        cout<<"else if"<<endl;

    else        //PROBLEM LINE
       cout<<"else"<<endl;
I get this problem in the debugger: main.cpp|18|error: 'else' without a previous 'if'|

Please help.
 
did you right the code.

I think it should look like this. I am not an expert a c++, but I think it should work. Please let me know if it doesn't.

int n = 3;

if(n=3){
cout<<"If statement inside"<<endl;
cout<<"dot dot dot"<<endl;
cout<<"We are inside an else if statement"<<endl;
}
else if(n>2)
cout<<"else if"<<endl;

else //PROBLEM LINE
cout<<"else"<<endl;
 
If you don't put curly brackets in, the if/else/elseif statement will ONLY apply to the single next line after it. e.g.

Code:
if(x>2)
   this line runs *only* if x is greater than 2
   this line *always* runs even if x <= 2 
   so does this line

else if(y>4){
   this line runs only if y > 4
   same with this line
   }
note the code above will error because the if statement isn't linked to the else if statement. This would work

Code:
if(x>2)
   this line runs *only* if x is greater than 2, note no curly brackets!
else if(y>4){
   this line runs only if y > 4
   same with this line
   }
 
As said previously, if you don't use curly braces then the end of the first statement is considered to be the end of the block but you should also remember that the semicolon ends the statement so in your case you have ended your statement early as well.
 
Also, don't put semicolons (;) at the end of an if, if else, or else line like you did in your first example.

If it's a single line under an if statement, e.g.:

Code:
if(x==1)
         cout << "x = 1" << endl;

You had semicolons after the if, if else, and else lines, which causes it to skip what's underneath or in the curly braces.
 
Also, don't put semicolons (;) at the end of an if, if else, or else line like you did in your first example.

If it's a single line under an if statement, e.g.:

Code:
if(x==1)
         cout << "x = 1" << endl;

You had semicolons after the if, if else, and else lines, which causes it to skip what's underneath or in the curly braces.

Yes, this is a much better way of expressing what I just wrote.
 
Now my for loops have issues. The machine just skips right over it. Before it I had a goto thing for demonstration but it just exploded in an infinite madhouse.
Code:
    int iii=0;
    for (;iii>10;){
    cout<<"HI"<<endl;
    iii++;
    }
 
For a start you've set it to continue while iii is greater than 10. Seeing as it starts at 0, that'll never happen.
Code:
for(int iii=0;iii<10;iii++){
cout<<"HI"<<endl;
}
 
Status
Not open for further replies.
Back
Top Bottom