4.6: The if/else if Statement

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Write an if/else statement that adds 1 to the variable minors if the variable age is less than 18, adds 1 to the variable adults if age is 18 through 64, and adds 1 to the variable seniors if age is 65 or older.

if (age < 18) minors++; else if (age >=18 && age <=64) adults++; else seniors++;

Write a statement that compares the values of score1 and score2 and takes the following actions. When score1 exceeds score2, the message "player1 wins" is printed to standard out. When score2 exceeds score1, the message "player2 wins" is printed to standard out. In each case, the variables player1Wins,, player1Losses, player2Wins, and player2Losses,, are incremented when appropriate. Finally, in the event of a tie, the message "tie" is printed and the variable tieCount is incremented .

if (score1 > score2) { cout << "player1 wins" << endl; ++player1Wins; ++player2Losses; } else if (score2 > score1) { cout << "player2 wins" << endl; ++player2Wins; ++player1Losses; } else { cout << "tie" << endl; ++tieCount; }

Write a statement that increments (adds 1 to) one and only one of these five variables : reverseDrivers parkedDrivers slowDrivers safeDrivers speeders. The variable speed determines which of the five is incremented as follows: The statement increments reverseDrivers if speed is less than 0, increments parkedDrivers if speed is less than 1, increments slowDrivers if speed is less than 40, increments safeDrivers if speed is less than or equal to 65, and otherwise increments speeders.

if(speed < 0){ reverseDrivers++; }else if(speed < 1){ parkedDrivers++; }else if(speed < 40){ slowDrivers++; }else if(speed <= 65){ safeDrivers++; }else{ speeders++; }


Ensembles d'études connexes

California Driver's Ed Permit Test

View Set

biotechnology chapter 3- quiz 3 and 4

View Set

Chapter 11: Valuation and Characteristics of Bonds

View Set

Ch 14 The Peripheral Nervous System***

View Set