Transcription of The if Statement and Practice Problems
{{id}} {{{paragraph}}}
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
Domain:
Source:
Link to this page:
Please notify us if you found a problem with this document:
{{id}} {{{paragraph}}}