34.6k views
5 votes
Consider the following code segment. It should display a message only if the cost is between 50 and 75 dollars. The message should also be displayed if the cost is exactly 50 dollars or exactly 75 dollars. if _________________ : print("The cost is in the desired range") What condition should be placed in the blank to achieve the desired behavior

User M Raymaker
by
4.7k points

1 Answer

2 votes

Answer:

In C++ it can be written as:

if(cost>=50&&cost<=75)

{ cout<<"The cost is in the desired range";}

In Python:

if(cost <= 75 and cost>= 50):

print("The cost is in the desired range")

Step-by-step explanation:

cost>50&&cost<75 covers the requirement that cost should be between 50 and 75 dollars

But the whole statement cost>=50&&cost<=75 covers the requirement that cost should be between 50 and 75 dollars and message should also be displayed if the cost is exactly 50 dollars or exactly 75 dollars. For example if the value of cost=50 then this message The cost is in the desired range is displayed.

In the above program && in C++, and in Python is used which is a logical operator which means that both the conditions cost <= 75 and cost>= 50 should be true for the if condition to evaluate to true. For example if cost=35 then this message is not printed because both the conditions are false i.e. 35 is neither less than or equal to 75 nor greater than or equal to 50.

User Gpapaz
by
4.7k points