Monday, June 18, 2012

Homework 5: Simple_if_else

1. Write an if/else statement that compares the variable  age with  65 , adds  1 to the variable  seniorCitizens if  age is greater than or equal to  65 , and adds  1 to the variable  nonSeniors otherwise.

if ( age >= 65 ) seniorCitizens ++ ; else nonSeniors ++ ;

2.  Write an if/else statement that compares the value of the variables  soldYesterday and  soldToday , and based upon that comparison assigns  salesTrend the value  -1 or  1 .

 -1 represents the case where  soldYesterday is greater than  soldToday ; 1 represents the case where  soldYesterday is not greater than  soldToday . 

if ( soldYesterday > soldToday ) salesTrend = - 1 ; else salesTrend = 1 ;

3. Write an if/else statement that assigns  true to the variable  fever if the variable  temperature is greater than  98.6 ; otherwise it assigns  false to  fever . 

if ( temperature > 98.6 ) fever = true ; else fever = false ;

No comments:

Post a Comment