Professional Documents
Culture Documents
WEEK 6 (Autosaved)
WEEK 6 (Autosaved)
Fundamentals
1
PROGRAM AS A SEQUENCE OF STEPS
You might think of the statements in a
procedural program as individual steps taken as
you are walking down a road.
if (expression)
statement;
THE IF STATEMENT-WHAT HAPPENS
To evaluate:
if (expression)
statement;
If the expression is true, then statement is
executed.
If the expression is false, then statement is
skipped.
IF STATEMENT IN PROGRAM 4-2
Continued…
IF STATEMENT IN PROGRAM 4-2
FLOWCHART FOR PROGRAM 4-2 LINES
21 AND 22
IF STATEMENT EXAMPLES
IF STATEMENT NOTES
Semicolons do not mark the end of a line, but the end of
a complete C++ statement.
if ( expression)
{
statement;
statement;
// Place as many statements here as
necessary.
EXPANDING THE IF STATEMENT
To execute more than one statement as part of an
if statement, enclose them in { }:
if (score > 90)
{
grade = 'A';
cout << "Good Job!\n";
}
if (expression)
statement1; // or block
else
statement2; // or block
IF/ELSE-WHAT HAPPENS
To evaluate:
if (expression)
statement1;
else
statement2;
Continued…
EXERCISES
NOW BACK TO TRUTH
For example, the following is a legal if statement in C+
+:
if (value)
cout << "It is True!";
if (x + y)
cout << "It is True!";
if (pow(a, b))
cout << "It is True!";
NESTED CONDITION CHECKING
Sometimes an if statement must be nested inside another if
statement. See following case study
NESTED CONDITION CHECKING
NESTED IF STATEMENTS
From Program 4-10
NESTED IF STATEMENTS
An if statement that is nested inside another if
statement