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
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
7 replies · 11K views
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;
}
 
Physics 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.