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

  • C/C++
  • Thread starter Saladsamurai
  • Start date
  • Tags
    C++
In summary, the else statement in this code is not testing anything, it is just saying whatever is following it is going to happen.
  • #1
Saladsamurai
3,020
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
  • #2
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.
 
  • #3
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:

1. What is the purpose of using an if/else statement in C++?

An if/else statement allows the programmer to create a decision-making structure in their code. It allows certain instructions to be executed if a specific condition is met, and a different set of instructions to be executed if the condition is not met.

2. How do you write a simple if/else statement in C++?

The basic syntax for an if/else statement in C++ is:

if (condition) { // code to be executed if condition is true } else { // code to be executed if condition is false }

The condition can be any Boolean expression, and the code within the curly braces will only be executed if the condition evaluates to true.

3. Can an if/else statement have more than one condition?

Yes, an if/else statement can have multiple conditions by using the else if keyword. This allows for a series of conditions to be evaluated, and the code within the corresponding block will be executed if the condition is true. The final else statement can be used as a default option if none of the previous conditions are met.

4. How do you handle situations where there are more than two possible outcomes?

In C++, you can use nested if/else statements to handle situations with more than two possible outcomes. This means that an if/else statement is placed within another if/else statement, allowing for multiple conditions to be evaluated and different instructions to be executed based on the outcomes.

5. Are there any other ways to create decision-making structures in C++?

Yes, there are other ways to create decision-making structures in C++. The switch statement, for example, allows for multiple possible execution paths based on the value of a variable. It is often used when there are a large number of possible outcomes, as it can be more efficient than using multiple if/else statements.

Similar threads

  • Programming and Computer Science
Replies
31
Views
2K
  • Programming and Computer Science
2
Replies
69
Views
4K
  • Programming and Computer Science
2
Replies
49
Views
3K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
2
Views
351
  • Programming and Computer Science
Replies
5
Views
852
  • Programming and Computer Science
3
Replies
81
Views
5K
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
17
Views
1K
Back
Top