0% found this document useful (0 votes)
69 views38 pages

Computer Programming (M5-Main) PDF

The document discusses conditional control structures in computer programming. It defines four types of flow control: sequential, selection, repetition, and invocation. Selection structures like if and if-else use relational and logical operators to select statements for execution based on conditions. Nested if statements allow if statements within other if statements to test multiple conditions. Programs examples demonstrate using if, if-else, and nested if statements to classify values, guess numbers, and find maximum values.

Uploaded by

Mohamad Lajarato
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views38 pages

Computer Programming (M5-Main) PDF

The document discusses conditional control structures in computer programming. It defines four types of flow control: sequential, selection, repetition, and invocation. Selection structures like if and if-else use relational and logical operators to select statements for execution based on conditions. Nested if statements allow if statements within other if statements to test multiple conditions. Programs examples demonstrate using if, if-else, and nested if statements to classify values, guess numbers, and find maximum values.

Uploaded by

Mohamad Lajarato
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

COMPUTER

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

Any algorithm can be built using combinations of


four standardized flow of control structures:

• Normal flow of control for all programs is


sequential
• Selection is used to select which statements are
performed next based on a condition

• Repetition is used to repeat a set of statements

• Invocation is used to invoke a sequence of


instructions using a single statement, as in
calling a function
• An expression created using a relational operator.
• Relational expressions are also known as conditions
• The expression is interpreted as either true (non-zero)
or false (0).
Relational Operator Meaning
< Less than
> Greater than
<= Less than or equal
>= Greater than or equal
== Equal to
!= Not equal to
Example
relational age < 30
operand operator operand height > 6.2
taxable <= 20000
grade >= 70 temp >= 98.6
grade == 100
number != 250
Character data can also be compared using relational
operators.
Expression Value Interpretation ASCII Code(Decimal)
‘A’ > ‘C’ 0 False
‘D’ <= ‘Z’ 1 True
‘E’ == ‘F’ 0 False
‘g’ >= ‘m’ 0 False
‘b’ != ‘c’ 1 True
‘a’ == ‘A’ 0 False
‘B’ < ‘a’ 1 True
‘b’ > ‘Z’ 1 True
• More complex conditions can be created using the
logical operations AND (&&), OR (||), and NOT (!)

First Second a && b a || b !a !b


Operand Operand
a b
True True True True False False
True False False True False True
False True False True True False
False False False False True True
logical Example
operator
operand operand ! temp
grade >= 70 && grade < 80
grade >= 70 && grade < 80 opt ==‘a’ || opt ==‘A’
!( length >= 9 )
relational relational
operator operator
The if statement allows you to control if a program
enters a section of code or not based on whether a given
condition is true or false. One of the important functions
of the if statement is that it allows the program to select
an action based upon the user's input.
• if
• if-else
• if-ladder
• nested if
START
The structure of an if statement:
a. single statement b. compound statement
FALSE
if ( expression ) if( expression ) { EXPRESSION

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.

Output: (if no is greater than 0)

Output: (if no is lesser than


or equal to 0)
START
The structure of an if-else statement:
FALSE
if ( expression )
STATEMENT
statement_TRUE; EXPRESSION
FALSE
else
statement_FALSE; TRUE
STATEMENT
TRUE

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: (if no is less than 0)


Write a program that accepts an integer and determine
whether the input is an odd or even integer.

A number is said to be even if and only if it is divisible by


2. Let us observe the remainder when we divide a
number by 2. Expression Remainder
What can be used
1/2 1
to test to check the
2/2 0
divisibility of a
3/2 1
number by 2?
4/2 0
Output: (if no is even)

Output: (if no is odd)


Write a program that guesses a magic number.

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)

Output: (if no is equal to 0)

Output: (if no is less than 0)


Write a program that displays the equivalent letter grade
of the percentage grade. Use the table below:

Percentage Grade Letter Grade


91-100 A
81-90 B
70-80 C
0-69 F
Other inputs You've entered wrong
data.
In order to arrive with the expression that test the range
of the percentage grade input, use a logical operator.

91-100 can be interpreted as the


grade is greater than or equal to 91 but less than or
equal to 100 and be translated as

if( grade>=91 && grade<=100)


Output:
START ANOTHER IF-ELSE
STATEMENT

FALSE (ELSE PART)


EXPRESSION
1 TRUE
EXPRESSION
2 STATEMENT 2
Nested if statements TRUE
means an if or if- FALSE (ELSE PART)
else statement inside
STATEMENT 3
another if statement. STATEMENT 1

REST OF THE
PROGRAM
Output:

Modify the program


that guesses a magic
number. If the guess
number is wrong, the
program should
determine if the guess
number is higher or
lower than the magic
number.
START

FALSE (ELSE PART)


EXPRESSION
1 STATEMENT 3

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

case, and the variable


being switched on is case constantn: statement(s);
break;
checked for each default : /* Optional */
switch case. statement(s);
}
START

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:

Write a program that


allows a user to select
a shape from the menu.
The program should
accept a lowercase or
an uppercase letter as
an input. If the input is
not in the choices
display “Invalid entry.”
WHAT IF WE REMOVED THE SINGLE
QUOTATION MARK IN THE CASE CONSTANT
STATEMENT?
UNDERSTANDING SWITCH
CONTROL STRUCTURE
WHAT IF WE ADDED A BREAK STATEMENT
AFTER EACH CASE OF A CAPITAL LETTER?

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

You might also like