Home Segments Index Top Previous Next

276: Mainline

Suppose, for example, that you want to write a program that displays a message that depends on the temperature of a refrigerator car. Specifically, if the temperature is greater than 50°F, you want your program to display It is too warm!, and if the temperature is less than 25°F, you want your program to display It is too cold!.

Further suppose that you are able to assign the temperature, measured by some sort of temperature sensor, to a variable named temperature.

One solution is to write a program that uses an if statement in which the embedded statements are output statements:

#include  
main ( ) { 
  int temperature; 
  cin >> temperature; 
  if (temperature < 25) cout << "It is too cold!" << endl; 
  if (temperature > 50) cout << "It is too warm!" << endl; 
} 
--- Data ---
55 
--- Result --- 
It is too warm!