Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 47

Programming

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.

 To reach the destination, you must start at the


beginning and take each step, one after the other,
until you reach the destination.

 The programs you have written so far are like a


“path” of execution for the program to follow
PROGRAM AS A SEQUENCE OF
STEPS
PROGRAM AS A SEQUENCE OF STEPS
 The type of code in Figure 4-1 is called a sequence
structure because the statements are executed in
sequence, without branching off in another direction.
 i.e. there exists only path of instructions execution to follow

 Programs often need more than one path of execution,


however.

 Many algorithms require a program to execute some


statements only under certain circumstances.

 This can be accomplished with a decision structure.


DECISION STRUCTURE
 In a decision structure’s simplest form, a specific action
is taken only when a specific condition exists.

 If the condition does not exist, the action is not


performed.

 Flowchart in Figure 4-2 shows the logic of a decision


structure.
FLOWCHART FOR EVALUATING A
DECISION
 if the condition is true, the program
flow follows one path, which leads
to an action being performed.

 If the answer to the question is no


(or the condition is false), the
program flow follows another path,
which skips the action.
FLOWCHART FOR EVALUATING A
DECISION
SOME EXAMPLE DECISION STRUCTURE
 If the car is low on fuel, stop at a service station and get
fuel.
 If it’s raining outside, go inside.

 If you’re hungry, get something to eat.

 One way to code a decision structure in C++ is with the


if statement
THE IF STATEMENT
 Allows statements to be conditionally executed or
skipped over

 Models the way we mentally evaluate situations:


 "If it is raining, take an umbrella."
 "If it is cold outside, wear a coat."
THE IF STATEMENT
THE IF STATEMENT
 General Format:

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.

 The if statement isn’t complete without the conditionally


executed statement that comes after it.
if (expression)
statement;
IF STATEMENT NOTES
 Do not place ; after (expression)
 If you put ; after the if part, the
compiler will assume you are placing a
null statement there.
 The null statement is an empty
statement that does nothing.
 This will prematurely terminate the if
statement, which disconnects it from
the statement that follows it.
PROGRAMMING STYLE AND THE IF
STATEMENT
 Even though if statements usually span more than one
line, they are technically one long statement.
if (a >= 100)
cout << "The number is out of range.\n";

if (a >= 100) cout << "The number is out of


range.\n";

 Compiler considers the if part and the cout


statement as one unit, with a semicolon properly
placed at the end.
PROGRAMMING STYLE AND THE IF
STATEMENT
 Indention and spacing are for the human readers of a
program, not the compiler.
 Here are two important style rules you should adopt for
writing if statements:

1. Place statement; on a separate line


2. The statement; should be indented one “level” from
the if statement
COMPARING FLOATING-POINT
NUMBERS IN IF STATMENT
 To prevent round-off errors from causing error, you
should stick with
> (greater-than)
< ( less-than )
NOW BACK TO TRUTH
 Here is a summary of the rules you have seen so
far:
 When a relational expression is
 true it has the value 1.
 false it has the value 0.

 For the if statement


 Any expression that has the value 0 is considered
false
 Any expression that has any value other than 0 is
considered true
MULTIPLE ACTIONS IN DECISION
STRUCTURE
EXPANDING THE IF STATEMENT
 To execute more than one statement as part of an
if statement, enclose them in { }:

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";
}

 { } creates a block of code


DON’T FORGET THE BRACES!
THE IF/ELSE STATEMENT
 The if/else statement will execute one group of
statements if the expression is true, or another group of
statements if the expression is false.

 Provides two possible paths of execution


THE IF/ELSE STATEMENT
 General Format:

if (expression)
statement1; // or block
else
statement2; // or block
IF/ELSE-WHAT HAPPENS
To evaluate:
if (expression)
statement1;
else
statement2;

 Ifthe expression is true, then


statement1 is executed and statement2 is
skipped.

 Ifthe expression is false, then


statement1 is skipped and statement2 is
executed.
THE IF/ELSE STATEMENT AND
MODULUS OPERATOR IN PROGRAM 4-8
FLOWCHART FOR PROGRAM 4-8 LINES
14 THROUGH 18
TESTING THE DIVISOR IN PROGRAM 4-9

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

 Nested if statements can be used to test more than one


condition
NESTED IF STATEMENTS
 Another example, from Program 4-1
PROGRAMMING STYLE AND NESTED
DECISION STRUCTURES
 For readability and easier debugging, it’s important to
use proper alignment and indentation in a set of nested if
statements.

 This makes it easier to see which actions are performed


by each part of the decision structure.
PROGRAMMING STYLE AND NESTED
DECISION STRUCTURES
USE PROPER INDENTATION!

You might also like