0% found this document useful (0 votes)
23 views

Chapter 7 C++ Program Control Structures

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Chapter 7 C++ Program Control Structures

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CC02 MODULE FUNDAMENTALS OF PROGRAMMING

CHAPTER 7: C++ PROGRAM CONTROL STRUCTURES

Objectives:

✓ Understand what control structure is.


✓ Differentiate types of Control Structures
✓ Know how to use and construct Control Structures

Control Strucuture

A program is usually not limited to a linear sequence of instructions. During its process it
may bifurcate, repeat code or take decisions. For that purpose, C++ provides control structures
that serve to specify what has to be done by our program, when and under which circumstances.

With the introduction of control structures we are going to have to introduce a new concept:
the compound-statement or block. A block is a group of statements which are separated by
semicolons (;) like all C++ statements, but grouped together in a block enclosed in braces: { }:

{ statement1; statement2; statement3; }

Most of the control structures that we will see in this section require a generic statement
as part of its syntax. A statement can be either a simple statement (a simple instruction ending
with a semicolon) or a compound statement (several instructions grouped in a block), like the one
just described. In the case that we want the statement to be a simple statement, we do not need
to enclose it in braces ({}). But in the case that we want the statement to be a compound statement
it must be enclosed between braces ({}), forming a block.

Types of control structures


1. Sequence Structure
2. Selection /Conditional Structure
3. Repetition/Iteration Structure

SEQUENCE STRUCTURE
The instructions are executed sequentially starting from the first instruction up to the last
instruction in the program.

Entry S1 S2 S3 S4

Page 1
CC02 MODULE FUNDAMENTALS OF PROGRAMMING

CONDITIONAL / SELECTION STRUCTURE

It perform different computations or actions depending on whether a programmer-


specified boolean condition evaluates to true or false.

a. If Statement

The if keyword is used to execute a statement or block only if a condition is fulfilled. Its form is:

Syntax:
if (condition) statement

Where condition is the expression that is being evaluated. If this condition is true, statement is
executed. If it is false, statement is ignored (not executed) and the program continues right after
this conditional structure.

The
1 if (x following
== 100) code fragment prints x is
100
2 coutonly<<if"xthe value stored in the x variable is
is 100";
indeed 100:
1 if (x == 100) If we want more than a single statement to be executed in
2 { case that the condition is true we can specify a block using
3 cout << "x is "; braces { }:
4 cout << x;
5 }

b. If Else Statement

It executes two statements in a program. If the condition is satisfied the first statement will be
executed otherwise, it will go to another statement.
We can additionally specify what we want to happen if the condition is not fulfilled by using the
keyword else. Its form used in conjunction with if is:
Syntax: if (condition)
statement 1;
else
statement 2;
Prints on the screen x is 100 if
For example: indeed x has a value of 100, but if it has
not -and only if not- it prints out x is
1 if (x == 100) not 100.
2 cout << "x is 100";
3 else
4 cout << "x is not 100";

Page 2
CC02 MODULE FUNDAMENTALS OF PROGRAMMING

c. Else If Statement

Usually it is used to execute if you have three or more statement to be executed in the program.
The if + else structured can be concatenated with the intention of verifying a range of values. The
following example shows its use telling if the value currently stored in x is positive, negative or
none of them (i.e zero):

1 if (x > 0)
2 cout << "x is positive";
3 else if (x < 0)
4 cout << "x is negative";
5 else
6 cout << "x is 0";

note
Remember that in case that we want more than a single statement to be executed, we
must group them in a block by enclosing them in braces { }.

d. Switch Case Statement

Its objective is to check several possible constant values for an expression. Its form is
the

e. It works in the following way:


Switch (expression)
f. 1. Switch evaluates expression and checks if its
{ g. equivalent to constant1, if it is, it executes group
Case constant 1:
h. of statements1 until it finds the break statement.
Group of statements
i. When it finds this break statement the program
1;
j. jumps to the end of the switch selective structure.
Break;
k. 2. If expression is not equal to constant 1 it will be
Case constantl. 2: check against constant2 if its equal to this, it will
m.
Group of statement 2; execute group of statements2 until break keyword
Break n. is found, then will jump to the end of the switch
- o. selective structure.
- 3. code
p. Both of the following Finally,
fr if the value of expression did not match
- q. any of the previously specified constants, the
Default: r. program will execute the statements included after
the default.
s.
Default groupt. of
statements
}
Both of the following code fragments have the same behavior

Page 3
CC02 MODULE FUNDAMENTALS OF PROGRAMMING

SWITCH CASE SAMPLE If – else equivalent

switch(x) {
case If (x == 1)
cout<<”x is 1”; {
break; Cout<<”x is 1”;
}
case Else if (x == 2)
cout<<”x is 2”; {
break Cout<<”x is 2”;
}
default: Else
cout<<”value of x {
is unknown”; Cout<<”value of x
} is unknown”;
}

The switch case statement is a bit peculiar within C++ language because it uses labels
instead of blocks. This forces us to put break statements after the group of statements
that we want to be executed for a specific condition. Otherwise the remaining statements
– including those corresponding to other labels - will also be executed until the end of the
switch selective block or a break statement is reached.

For more knowledge Fundamentals of Programming, please check the


link provided;

➢ https://www.youtube.com/watch?v=0YS8mLNrqhw
➢ https://www.youtube.com/watch?v=usU9ULY7t8E
➢ https://www.youtube.com/watch?v=L_sXxIkd7VU

REFERENCES

❖ https://www.w3schools.com/cpp/cpp_conditions.asp

Page 4

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy