C/C++ Troubleshooting a Simple C++ if/else Question for New Programmers

  • Thread starter Thread starter Saladsamurai
  • Start date Start date
  • Tags Tags
    C++
Click For Summary
The discussion focuses on a programming error related to the improper use of if-else statements. The main issue highlighted is that an else statement must directly follow an if statement, and the correct syntax requires that the if statement is properly structured. It emphasizes the importance of using block statements, enclosed in braces, for clarity and to minimize bugs. This practice enhances readability, especially in complex scenarios with multiple dependent statements. Additionally, it points out that parentheses should not be used after an else statement, as else does not evaluate a condition like if does. Overall, the advice encourages beginners to adopt these best practices for better coding habits.
Saladsamurai
Messages
3,009
Reaction score
7
What is wrong with mine? I am super new to programming and am trying to figure this stuff out on my own. The stuff in the tutorial is even too advanced for me.

Here it is:
iffellse.jpg


I am sure it is really easy to see, but I do not understand the error :confused:
 
Technology news on Phys.org
The else statement has no if statement immediately preceding it.

The proper syntax is

if (boolean) statement else statement

You have

if (boolean) statement statement else statement


A simple rule that I use: Always use block statements (i.e., statements enclosed in braces) with an if statement, and never put the statement on the same line as the if. While C and C++ do allow simple statements after an if (or else), many organizations have programming rules that make this construct illegal. The cost of always using the block construct costs is a few extra keystrokes (there is zero performance penalty). The benefits are immense: It is clearer, there is a much reduced chance of introducing a bug, and you can insert break points on the if (or else) statement.
 
In other words, write it like this:

Code:
if (a < 10)
{
    y = a*b*c;
    cout << "blah blah" endl;
}
else
{
    y = a+b+c;
    cout << "blah blah" endl;
}

The use of the curly brackets and indentation really helps you keep track of what's going on, especially when you are using multiple dependant statements, ie:

Code:
if (i > 1)
{
    if (i = 3)
    {
        cout << "i = 3";
    }
}
else
{
     cout << "i is less than or equal to 1";
}

You also put parenthesis around the statement after else, which is not correct. Else does not make a test like if and else if does. Basically when you put something in parenthesis after if or else if you are saying if whatever is in the parenthesis is true, then do whatever is next. The statement inside of the parenthesis is not even testable, on second glace.
 
Last edited:
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 25 ·
Replies
25
Views
722
  • · Replies 2 ·
Replies
2
Views
2K
Replies
69
Views
10K
  • · Replies 31 ·
2
Replies
31
Views
3K
Replies
81
Views
7K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
Replies
89
Views
6K
  • · Replies 40 ·
2
Replies
40
Views
4K