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

  • C/C++
  • Thread starter ineedhelpnow
  • Start date
  • Tags
    C++ Data
In summary: In other words, when you writeif (userString=="Quit") cout << "Goodbye" << endl;else cout << "Hello" << endl;the computer does not know you want to compare userString with "Quit" just because you assigned userString to "Quit" earlier. It does not remember the assignments.
  • #1
ineedhelpnow
651
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
  • #2
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;
 
  • #3
your second statement should be else if and its always good practice to include {} :eek: 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:
  • #4
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?
 
  • #5
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.
 
  • #6
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"?
 
  • #7
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"
 
  • #8
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.
 

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

1. What is a bool data type in C++?

A bool data type in C++ is a variable type that can hold either a true or false value. It is used to represent logical values and is typically used in conditional statements and loops.

2. How is a bool data type declared in C++?

A bool data type is declared using the keyword "bool" followed by a variable name and an optional initialization value. For example: bool myBool = true;

3. What is the size of a bool data type in C++?

In most C++ compilers, the size of a bool data type is 1 byte. However, it is recommended to use the sizeof() operator to determine the exact size on a specific platform.

4. How are bool values evaluated in C++?

In C++, a bool value of true is equivalent to the integer value of 1, and a value of false is equivalent to 0. This means that when evaluating a bool expression, any non-zero value will be considered true and a value of 0 will be considered false.

5. Can a bool data type hold any value other than true or false?

No, a bool data type in C++ can only hold the values of true or false. Any other value assigned to a bool variable will be converted to either true or false based on its corresponding integer value.

Similar threads

Replies
2
Views
5K
Replies
5
Views
2K
Replies
4
Views
10K
Replies
12
Views
2K
Replies
1
Views
1K
Replies
2
Views
6K
Replies
4
Views
7K
Back
Top