Example: tourism industry

The if Statement and Practice Problems

TheifStatement and Practice ProblemsThe SimpleifStatementUseTo specify the conditions under which a Statement or group ofstatements should be (boolean-expression) Statement ;whereifis a reserved word,boolean-expressionis an expression that evaluates totrueorfalse, andstatementis a C++ Statement or a group of statements enclosed in curlybraces (a compound Statement ).ActionIf the boolean expression istrue, the specified Statement is executed; otherwise it is not. Ineither case, execution continues with the next Statement inthe grade is a charif (grade == A )cout << "EXCELLENT WORK!" << endl;-----------------------------------------------------------------------------// yards is an intif (yards != 0)cout << "There were " << yards << " yards." << endl;-----------------------------------------------------------------------------// temperature, normalTemp and degreesOfFever are doubles;// hasFever is a boolif (temperature > normalTemp) {degreesOfFever = temperature - normalTemp;hasFever = true;}1 Theif-elseStatementUseTo choose exactly one out of two statements (possibly compound statements) to be executed;specifies the conditions under which the first Statement is tobe executed and provides analternative Statement to execute if these conditions are not (boolean-expression) Statement -1;elsestat ement-2;whereifa

statement-1; else statement-2; where if and else are reserved words, boolean-expression is an expression that evalu-ates to true or false, and statement-1 and statement-2 are C++ statements (possibly compound statements, i.e. a group of statements enclosed by curly braces). Action

Tags:

  Practices, Testament, Problem, The if statement and practice problems

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Transcription of The if Statement and Practice Problems

1 TheifStatement and Practice ProblemsThe SimpleifStatementUseTo specify the conditions under which a Statement or group ofstatements should be (boolean-expression) Statement ;whereifis a reserved word,boolean-expressionis an expression that evaluates totrueorfalse, andstatementis a C++ Statement or a group of statements enclosed in curlybraces (a compound Statement ).ActionIf the boolean expression istrue, the specified Statement is executed; otherwise it is not. Ineither case, execution continues with the next Statement inthe grade is a charif (grade == A )cout << "EXCELLENT WORK!" << endl;-----------------------------------------------------------------------------// yards is an intif (yards != 0)cout << "There were " << yards << " yards." << endl;-----------------------------------------------------------------------------// temperature, normalTemp and degreesOfFever are doubles;// hasFever is a boolif (temperature > normalTemp) {degreesOfFever = temperature - normalTemp;hasFever = true;}1 Theif-elseStatementUseTo choose exactly one out of two statements (possibly compound statements) to be executed;specifies the conditions under which the first Statement is tobe executed and provides analternative Statement to execute if these conditions are not (boolean-expression) Statement -1;elsestat ement-2;whereifandelseare reserved words,boolean-expressionis an expression that evalu-ates totrueorfalse, andstatement-1andstatement-2are C++ statements (possiblycompound statements, a group of statements enclosed bycurly braces).

2 ActionIf the boolean expression istrue, Statement -1is executed andstatement-2is skipped;otherwisestatement-1is skipped andstatement-2is executed. In either case, executioncontinues with the next Statement in the numItems is an int; averageCost and totalCost are doublesif (numItems >= 0)averageCost = totalCost / numItems;elsecout << "No items were purchased." << endl;----------------------------------- ----------------------------------const double POLLUTION_CUTOFF = ;// pollutionIndexis a doubleif (pollutionIndex < POLLUTION_CUTOFF)cout << "Safe Condition" << endl;elsecout << "Hazardous Condition" << endl;2 The Extended-ifStatementUseTo choose one Statement (possibly compound) to be executed from among a group of state-ments (possibly compound); specifies the conditions under which each Statement may beexecuted and may contain a default Statement (in anelseclause at the end) to be executedifnoneof these conditions are met.

3 Note that in the absence of a finalelseclause, it maybe the case thatnoneof the statements are (boolean-expression-1) Statement -1;else if (boolean-expression-2) Statement -2;..else if (boolean-expression-n) Statement -n;elsest atement-default;whereifandelseare reserved words,boolean-expression-1,boolean-expre ssion-2,..,boolean-expression-nare expressions that evaluate totrueorfalse, andstatement-1, Statement -2,.., Statement - n, andstatement-defaultare C++ statements, possiblycompound statements. (A compound Statement is a group of statements enclosed by curlybraces.)ActionThe boolean expressions are evaluated in the order of their appearance to determine thefirst expression that istrue. The associated Statement is executed, and execution continueswith the first Statement following the entireif-else-ifconstruct. If none of the booleanexpressions istrue, the Statement associated with theelseclause is executed, and executionthen continues with the Statement following the none of the boolean expressionsistrueand theelseclause is omitted, execution falls through to (continueswith) thenext Statement in the program after the (next page)3 Examples// score is a double; grade is a charif (score == 100) {grade = A ;cout << "Superb" << endl;}else if (score >= 90) {grade = A ;cout << "Excellent" << endl;}else if (score >= 80) {grade = B ;cout << "Very Good" << endl;}else if (score >= 70) {grade = C ;cout << "Good" << endl;}else if (score >= 60)grade = D ;elsegrade = F.

4 ---------------------------------------- -----------------------// Ch is a charif ((Ch >= a ) && (Ch <= z )) {// code to process lower-case leter}else if ((Ch >= A ) && (Ch <= Z )) {// code to process upper-case leter}else if ((Ch >= 0 ) && (Ch <= 9 )) {// code to process digit character}else {// code to process non-letter/non-digit characters}4 Practice Problems What is wrong with the followingifstatement (there are at least 3 errors). Theindentation indicates the desired numNeighbors >= 3 || numNeighbors = 4++numNeighbors;cout << "You are dead!" << endl;else--numNeighbors; Describe the output produced by this poorly indented program segment:int number = 4;double alpha = ;if (number > 0)if (alpha > 0)cout << "Here I am!" << endl;elsecout << "No, I m here!" << endl;cout << "No, actually, I m here!" << endl; Consider the followingifstatement, wheredoesSignificantWork,makesBreakthrou gh,andnobelPrizeCandidateare all boolean variables:if (doesSignificantWork) {if (makesBreakthrough)nobelPrizeCandidate = true;elsenobelPrizeCandidate = false;}else if (!)

5 DoesSignificantWork)nobelPrizeCandidate = false;First, write a simplerifstatement that is equivalent to this one. Then write a singleassignment Statement that does the same thing. Writeifstatements to do the following: If character variabletaxCodeis T , increase price by adding thetaxRateper-centage ofpriceto it. If integer variableopCodehas the value 1, read in double values forXandYandcalculate and print their If integer variablecurrentNumberis odd, change its value so that it is now 3timescurrentNumberplus 1, otherwise change its value so that it is now half ofcurrentNumber(rounded down whencurrentNumberis odd). Assigntrueto the boolean variableleapYearif the integer variableyearis aleap year. (A leap year is a multiple of 4, and if it is a multiple of 100, it mustalso be a multiple of 400.) Assign a value to double variablecostdepending on the value of integer variabledistanceas follows:DistanceCost-------------------- -------------------------0 through than 100 but not more than than 500 but less than 1, ,000 or


Related search queries