If Else statement without braces

I was just wondering what will happen in the following program. I know there will be a compilation error, however, can someone explain to me how does the compiler arrives to the code having an error? Thanks in advance!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

using namespace std;

int main()
{
    int choice = 2, n1 = 4, n2 = 9;
    if (choice >= 2)
        n2 += n1;
        n1 -= n2;
    else
        n2 -= n1;
        n1 += n2;
    cout << n1 << "," << n2;
    return 0;
}
Last edited on
Please use code tags for all your code, otherwise it's hard to read - http://www.cplusplus.com/articles/jEywvCM9/

Since you can only write one line of code after if statements without needing braces, the else statement won't work because there are 2 lines of code between it and the if statement. The compiler will recognize the else statement as not having an if statement, which in that case you can't have an else statement.
Last edited on
@TarikNeaj

I still do not understand why does the compiler recognise the else statement as not having an if statement. In this case, wouldnt it just do n2 += n1 and then do n1 += n2?
I still do not understand why does the compiler recognise the else statement as not having an if statement


Because it doesn't have an if statement.

The rules are clear; to have an else, you must put the else right after the block of the if. You can't just put it later. Do you understand why this code is bad?
1
2
3
4
5
6
7
8
9
10
11
12
if (x >1)
{
  // HERE IS THE IF BLOCK
  cout << "x is big";
} // THIS IS THE END OF THE IF BLOCK
somefunction();
cin >> eggs;
beans = beans + 1;
else
{
  cout << "x is small";
}

See in this bad code where the else is? It's way after the if block. That's simply forbidden. If you have an else, you must put it right after the if block.


So let's take a look at your code.
1
2
3
4
5
6
    if (choice >= 2)
        n2 += n1; // THIS IS THE ENTIRE IF BLOCK
        n1 -= n2; // THIS CODE IS *NOT* PART OF THE IF BLOCK
    else
        n2 -= n1;
        n1 += n2;

In this code, the if block has a single statement in it. I have added a comment to make it clear where the if block is. So the else is NOT right after the if block.

I'll write your code again, with better formatting, so you can see what you actually wrote.
1
2
3
4
5
6
7
8
9
10
if (choice >= 2)
{
          n2 += n1; // THIS IS THE ENTIRE IF BLOCK
}
n1 -= n2; // THIS CODE IS *NOT* PART OF THE IF BLOCK
else
{
        n2 -= n1;
}
n1 += n2;


Can you see how the else is NOT right after the if block? Looks to me that you've misunderstood white space. This is not python. Tabs and white space do not mean anything. They're just for you to lay out your code. If you want more than one line of code in the if block, you have to use { and } to mark the if block.

I think what you meant to write was this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

using namespace std;

int main()
{
int choice = 2, n1 = 4, n2 = 9;
if (choice >= 2)
{
        n2 += n1;
        n1 -= n2;
}
else
{
        n2 -= n1;
        n1 += n2;
}
cout << n1 << "," << n2;
return 0;
}

See how now the else is right after the if block?
Last edited on
Topic archived. No new replies allowed.