Home Segments Index Top Previous Next

278: Mainline

Either the if-true statement or the if-false statement or both may be embedded if statements. Accordingly, another solution to the temperature-testing problem looks like this:

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

Note that the first if statement's if-false statement is, itself, an embedded if statement. This embedded if statement is executed only if the temperature is not less than 25°F. If the temperature is between 25°F and 50°F, nothing is displayed.