week 7 - b (multiple if and switch)
DESCRIPTION
notesTRANSCRIPT
-
C++ Control Structures CHAPTER 6
-
ifelseif STATEMENT
SYNTAXType 1:
if (conditional expression1) statement1 ; //action#1elseif (conditional expression2) statement2 ; //action#2elseif (conditional expression3) statement3 ; //action#3statementN;Type 2:
if (conditional expression1){ statement1.1 ; //action#1 statement1.2 ;}else if (conditional expression2){ statement2.1 ; //action#2 statement2.2 ;} elseif (conditional expression3){ statement3.1 ; //action#3 statement3.2 ;}statementN;Type 3:
if (conditional expression1){ statement1.1 ; //action#1 statement1.2 ;}else if (conditional expression2) statement2.1 ; //action#2elseif (conditional expression3){ statement3.1 ; //action#3 statement3.2 ;}statementN;
-
SAMPLE QUESTION
PROBLEMWrite a program that will input a number and print the following :Number Message1 small2 6 average8, 9 largeOther not in the list
-
ANSWER
PROGRAMOUTPUT# includemain ( ){ int number; cout number; if ( number = = 1 ) cout
-
SAMPLE QUESTION 1. Write a program that received a letter grade. Print the message based on the grade given below :Grade messageA AppleB BallC CokeOthers Error
-
ANSWER
PROGRAMOUTPUT# includemain ( ){ char letter; cout letter; if ( letter = = A ) cout
- 2. What is the output for the following program segment? if ( n > = 1 && n < = 9 ) cout
-
3. Write a program that request an age. Print the message based on the age given below :Grade message2 Playing games3 or 4 Learn ABC5 6 Learn numbersOthers Out of nursery SAMPLE QUESTION
-
ANSWER
PROGRAMOUTPUT# includemain ( ){ int age; cout age; if ( age = = 2 ) cout
-
4. Write a program that accept a letter grade from a student. Assign a value as a point base on the letter grade entered. Display the point and the message.
Letter Grade Point and MessageA Point = 4, ExcellentB Point = 3, GoodC Point = 2, FairD Point = 1, Poor F Point = 0, FailSAMPLE QUESTION
-
ANSWER
PROGRAMOUTPUT# includemain ( ){ char grade; cout grade; if ( grade = = A ) cout
-
Switch ( ) STATEMENTit will terminates the entire switch statement
SYNTAXSwitch (variable name){ case value1 : statement1; break; case value2 : statement2; break; . . case valueN : statementN; break; default : statementN; break;}
-
Where:
value1, value2, value3, valueN value of the condition
statement1, statement2, statementN any form of statements if the value of the expression / condition matches the value.
Default statement executed when NONE of the value matches the value of the expression.
Break it will terminate the entire switch statement.
-
SAMPLE QUESTION
PROBLEMWrite a program that will input a number and print the following :Number message1 small2 6 average8, 9 largeOther not in the list
-
PROGRAMOUTPUT# includemain ( ){ int number; cout number; switch(number) { case 1 : cout
-
1. Write a program that will input a number grade and print the following :Grade message1 Excellent2 4 Good5 & 6 Not BadOther Not in the list SAMPLE QUESTION
-
PROGRAMOUTPUT# includemain ( ){ int grade; cout grade; switch(grade) { case 1 : cout
-
2.Write a program that will received a letter grade. Print the message based on the grade given below :
Grade messageA or a AppleB or b BallC or c CokeOthers ErrorSAMPLE QUESTION
-
PROGRAMOUTPUT# includemain ( ){ char grade; cout grade; switch(grade) { case A : case a : cout
-
3. Write a program that accept a letter grade from a student. Assign a value as a point base on the letter grade entered. Display the point and the message.
Letter Grade Point and MessageA Point = 4, ExcellentB Point = 3, GoodC Point = 2, FairD Point = 1, Poor F Point = 0, FailSAMPLE QUESTION
-
PROGRAMOUTPUT# includemain ( ){ char grade; cout grade; switch(grade) { case A : cout
-
1. Write a program :
a. Input a mark. If mark is between 40 to 100 (inclusive) print PASS, otherwise print FAIL.
b. Input 2 prices. If the first price is greater than 20, print the first price. Otherwise print the second price.
REVISION
-
c. Request user to enter their response. If the response is Y display Continue otherwise display Quit.
d. If an integer number is between 10 and 50 inclusively, multiply the number by 2 and display message Multiply. Otherwise, divide the number by 5 and display the message Division.REVISION
-
2. Write a program :
a. The price ticket to the Wonderful Theme Park depends on these table : REVISION
AgeMemberNon-MemberBelow 12RM10.00RM15.00Between 12 to 50RM20.00RM40.00Over 50RM15.00RM30.00
-
b. Prompt user to enter a number. Display a message based on the number entered. REVISION
NumberMessage2 - 5Under age6, 9, 10Middle age12 or moreOver ageOthersError
**********************