How to Write an If-Else Statement in C++ for Balloon Objects?

  • Context: C/C++ 
  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    C++ Data
Click For Summary

Discussion Overview

The discussion revolves around writing if-else statements in C++ for balloon objects and a string comparison scenario. Participants share their code snippets, suggest improvements, and clarify syntax and logic related to conditional statements.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant initially presents an if-else statement to classify balloon objects based on boolean variables isBalloon and isRed.
  • Another participant suggests that the condition can be simplified by omitting the comparison to true or false.
  • There is a discussion about the necessity of using else if for clarity and good practice in structuring the code.
  • A participant expresses confusion about the syntax and structure of nested if statements, particularly regarding the identification of the second statement.
  • One participant shares a different if-else scenario involving a string comparison, questioning why a simpler condition cannot be used.
  • Clarifications are made regarding the need for explicit comparisons in if statements to avoid ambiguity.
  • Participants discuss the syntactical correctness of the proposed code snippets and the importance of including conditions in else if statements.

Areas of Agreement / Disagreement

Participants generally agree on the importance of clear syntax and the need for explicit conditions in if statements. However, there is disagreement on the best practices for structuring nested if statements and the necessity of certain comparisons.

Contextual Notes

Some participants note that the definition of "statement" can be recursive, complicating the identification of specific statements within nested structures. Additionally, there is an acknowledgment of the limitations of the initial assignment in the string comparison example.

ineedhelpnow
Messages
649
Reaction score
0
Write an if-else statement to describe an object. Print "Balloon" if isBalloon is true and isRed is false. Print "Red balloon" if isBalloon and isRed are both true. Print "Not a balloon" otherwise. End with newline.

Sample program:

Code:
#include <iostream>
using namespace std;

int main() {
   bool isRed = false;
   bool isBalloon = false;

   <STUDENT CODE>

   return 0;
}
Below, do not type an entire program. Only type the portion indicated by the above instructions (and if a sample program is shown above, only type the <STUDENT CODE> portion.)

PLEASE HELP!

- - - Updated - - -

never mind. i got it.
Code:
if (isBalloon==true && isRed==false) {
      cout << "Balloon" << endl;
}
else if (isBalloon==true && isRed==true) {
      cout << "Red balloon" << endl;
}
else {
      cout << "Not a balloon" << endl;
}
 
Technology news on Phys.org
Your solution works. Note also that you can write [m]if (isBalloon)[/m] instead of [m]if (isBalloon == true)[/m] and [m]if (!isBalloon)[/m] instead of [m]if (isBalloon == false)[/m].

I would write the code as follows.
Code:
if (isBalloon)
  if (isRed)
    cout << "Red balloon" << endl;
  else
    cout << "Balloon" << endl;
else
  cout << "Not a balloon" << endl;
 
your second statement should be else if and its always good practice to include {} :o you're right though. that would have been way simpler. now that i notice, my by book actually does the same way you do. i just took the long route lol
 
Last edited:
ineedhelpnow said:
your second statement should be else if
Could you write exactly which part of my code has to be replaced and by what?
 
second statement
Code:
if (isBalloon)
  if (isRed)
    cout << "Red balloon" << endl;
else if
    cout << "Balloon" << endl;
else
  cout << "Not a balloon" << endl;

also in another example i did:
Write an if-else statement that prints "Goodbye" if userString is "Quit", else prints "Hello".

Sample program:

Code:
#include <iostream>
#include <string>
using namespace std;

int main() {
   string userString;

   userString = "Quit"; 

   <STUDENT CODE>

   return 0;
}

the code i came up with was
Code:
if (userString=="Quit") {
    cout << "Goodbye" << endl;
}
else {
    cout << "Hello" << endl;
}
and it was correct but it wouldn't accept any other format. why can't i just do (userString) in this one.
 
ineedhelpnow said:
second statement
Code:
if (isBalloon)
  if (isRed)
    cout << "Red balloon" << endl;
else if
    cout << "Balloon" << endl;
else
  cout << "Not a balloon" << endl;
"Second statement" is not clear enough. The problem is that the definition of "statement" is recursive: a statement may contain sub-statements. Here the entire code above is a compound statement; then
Code:
  if (isRed)
    cout << "Red balloon" << endl;
  else
    cout << "Balloon" << endl;
is a statement, [m]cout << "Red balloon" << endl;[/m] and [m]cout << "Balloon" << endl;[/m] are statement. On the other hand,
Code:
  else
    cout << "Balloon" << endl;
is not a statement, but a part of a statement. If statements were written one after another, then it would be easy to locate the second one, but since they are nested, it is not clear how to count them.

Also,
Code:
else if
    cout << "Balloon" << endl;
is syntactically incorrect since an [m]if[/m] must be followed by a condition.

ineedhelpnow said:
also in another example i did:
Write an if-else statement that prints "Goodbye" if userString is "Quit", else prints "Hello".

...

the code i came up with was
Code:
if (userString=="Quit") {
    cout << "Goodbye" << endl;
}
else {
    cout << "Hello" << endl;
}
and it was correct but it wouldn't accept any other format. why can't i just do (userString) in this one.
How do you suggest the computer should execute the code [m]if (userString)[/m]? How would the computer know that userString is supposed to be compared to "Quit" and not, say, to "Exit"?
 
Evgeny.Makarov said:
Also,
Code:
else if
    cout << "Balloon" << endl;
is syntactically incorrect since an [m]if[/m] must be followed by a condition.
sorry i forgot to state the condition.

Evgeny.Makarov said:
How do you suggest the computer should execute the code [m]if (userString)[/m]? How would the computer know that userString is supposed to be compared to "Quit" and not, say, to "Exit"?
because in the line above it says userString="Quit"
 
ineedhelpnow said:
sorry i forgot to state the condition.
Which condition? Checking [m]!isRed[/m] is not necessary because this is the [m]else[/m] branch of the [m]if (isRed)[/m].

ineedhelpnow said:
because in the line above it says userString="Quit"
You did write [m]if (userString=="Quit")[/m], which is correct, but I was thinking you were asking why you could not write [m]if (userString)[/m]. Were you asking something else?

Edit: Sorry, I misunderstood your thought. The initial assignment is indeed [m]userString = "Quit"[/m], but the computer does what it is told in the [m]if[/m] statement. If you need to compare userString with "Quit", you need to say so.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
12
Views
3K
  • · Replies 4 ·
Replies
4
Views
10K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 2 ·
Replies
2
Views
7K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K