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

  • Thread starter Thread starter Saladsamurai
  • Start date Start date
  • Tags Tags
    C++
AI Thread 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:
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top