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.

Answer :

Answer:

The answer to this question can be given as:

// code to if-else ladder statement can be given as:

if(isBalloon==true && isRed!=true) //if block

{

cout<<"Balloon"; //message

}

else if ((isBalloon && isRed)==true) //else if block

{

cout<<"Red balloon"; //message

}

else //else block

{

cout<<"Not a balloon"; //message

}

Explanation:

In the above code, we use the if-else ladder statement. In this statement, we use two variables and AND logical operator. AND operator executes when both condition is true. The following if-else ladder statement can be described as:

  • In the if block, we check the two conditions that the variable isBalloon value is "true" and the variable isRed value is "false". Then it prints "Balloon" on console.
  • In the else-if block, we check if both variables (isBalloon and isRed) value is "true". Then it prints "Red balloon" on console.
  • finally, if both above condition is false then It print "Not a balloon" on console.

Other Questions