Computer Programming (M5-Main) PDF
Computer Programming (M5-Main) PDF
PROGRAMMING 1
CHAPTER 5
PROGRAM CONTROL STRUCTURE:
CONDITIONAL CONTROL
STRUCTURE
Upon completion of this module, you will be able to:
• Enumerate and explain the different types of control
structures
• Identify the application and limitations of the different
conditional statement.
• Implement conditional control structures in solving
problems created in C++
Flow of control refers to the order in which a
program’s statements are executed
statement; statement1;
statement2; TRUE
STATEMENT
statementn;
} REST OF THE
Note: To make the multiple statements become compound PROGRAM
statement, enclosed the statements with { }.
Write a program that reads an integer and identifies if it
is a positive number.
REST OF THE
PROGRAM
Write a program that reads an integer and identifies if it
is a non negative integer or not.
Output: (if no is greater than
or equal to 0)
Output:
The structure of an if-ladder statement:
if ( expression1 )
statement1;
else if ( expression2 )
statement2;
else
statement3;
Note: Whether the indentation exists or not, the compiler will, by default, associate an else with
the closest previous unpaired if, unless braces are used to alter this default pairing
START
FALSE
EXPRESSION
The if statements are
STATEMENT 1
1
executed from the top down.
As soon as one of the
TRUE conditions controlling the if
FALSE is true, the statement
EXPRESSION
STATEMENT 2
associated with that if is
2
executed, and the rest of the
ladder is bypassed. If none
TRUE of the conditions is true,
STATEMENT 3 then the final else statement
will be executed.
REST OF THE
PROGRAM
Write a program that accepts an integer and classifies
the integer as positive, negative or zero.
Output: (if no is greater than 0)
REST OF THE
PROGRAM
Output:
TRUE
TRUE FALSE
ANOTHER IF-ELSE EXPRESSION
STATEMENT 1 2 STATEMENT 2
STATEMENT
REST OF THE
PROGRAM
ANOTHER IF-ELSE
Write a program that STATEMENT
finds the largest number
among the three
numbers using nested if.
Output:
ANOTHER IF-ELSE
STATEMENT
The structure of a switch statement:
A switch statement
allows a variable to be switch(expression) {
case constant1: statement(s);
tested for equality break; If the break statement
against a list of values. case constant2: statement(s);
was omitted, the
following case would
Each value is called a break; be executed
TRUE
EXPRESSION
=
CONSTANT1
STATEMENT(S) BREAK
FALSE
TRUE
EXPRESSION
=
CONSTANT2
STATEMENT(S) BREAK
FALSE
TRUE
EXPRESSION
=
CONSTANTn
STATEMENT(S) BREAK
FALSE
DEFAULT STATEMENT(S)
REST OF THE
PROGRAM
• Do not use floats in switch statements.
• Expression evaluated by switch should match a case.
This means that the matching case must also be an
integer or a constant expression which evaluates to an
integer.
• Letters (not words) can be used in case statements as
constants.
• Use break statement to stop further execution otherwise
it continues execution with whatever code that follows.
Write a program
that displays the Output:
corresponding
Filipino word of
the inputted
number from 1-3
otherwise
display “Invalid
input”.
WHAT IF BREAK IS REMOVED ON THE
FIRST CASE? WHAT IS THE OUTPUT?
UNDERSTANDING SWITCH
CONTROL STRUCTURE
WHAT IF DEFAULT IS REMOVED IN THE
PROGRAM? WHAT IS THE OUTPUT?
UNDERSTANDING SWITCH
CONTROL STRUCTURE
Output:
UNDERSTANDING SWITCH
CONTROL STRUCTURE
• Cadenhead, R et. Al. (2016). C++ in 24 Hours, Sams Teach Yourself (6th
Edition). Sams Publishing
• McGrath, M. (2017). C++ programming in easy steps (5th ed.).
Warwickshire, United Kingdom: Easy Steps Limited
• Tale, T. (2016). C++: The Ultimate Beginners Guide to C++ Programing.
CreateSpace Independent Publishing Platform
• Cadenhead, R et. Al. (2016). C++ in 24 Hours, Sams Teach Yourself (6th
Edition).Sams Publishing
• McGrath, M. (2017). C++ Programming in Easy Steps (5th ed.).
Warwickshire, United Kingdom: Easy Steps Limited
• Deitel (2017), How to Program C++, 10th Edition