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

  • Context: C/C++ 
  • Thread starter Thread starter Saladsamurai
  • Start date Start date
  • Tags Tags
    C++
Click For Summary
SUMMARY

The discussion focuses on troubleshooting a common syntax error in C++ related to the use of if/else statements. The correct syntax requires an if statement immediately preceding an else statement, and it is recommended to always use block statements enclosed in braces for clarity and to prevent bugs. The discussion emphasizes the importance of proper indentation and the correct use of parentheses, noting that the else statement does not require a condition. Following these guidelines enhances code readability and maintainability.

PREREQUISITES
  • Basic understanding of C++ syntax
  • Familiarity with conditional statements in programming
  • Knowledge of block statements and indentation practices
  • Experience with debugging simple C++ code
NEXT STEPS
  • Study C++ control structures, focusing on if/else syntax
  • Learn about best practices for writing clean and maintainable C++ code
  • Explore debugging techniques in C++ using breakpoints
  • Review common programming errors and how to avoid them in C++
USEFUL FOR

New programmers, C++ learners, and anyone seeking to improve their understanding of conditional statements and coding best practices in C++.

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 dependent 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:

Similar threads

  • · Replies 25 ·
Replies
25
Views
1K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
69
Views
11K
  • · Replies 31 ·
2
Replies
31
Views
3K
Replies
81
Views
8K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
89
Views
7K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 40 ·
2
Replies
40
Views
5K