#5 Conditional Control Structures

# #5 Conditional Control Structures - Mapua Institute of...

This preview shows pages 1–3. Sign up to view the full content.

Mapua Institute of Technology School of IT CS103 – Fundamentals of Problem Solving and Programming 1 Lecture #4 Control Structures What are control structures? Basically, control structures specify the sequence of execution of a group of statements. Some statements may need to be executed in sequential fashion, some may have to be repeated several times while others may or may not be executed depending on some condition. What are the three different types of control structures? 1. sequential 2. conditional 3. loop (repetition) What is a sequential control structure? A sequential control structure is organized such that statements are executed in sequence, i.e., one after the other in the order of their appearance in the source code. Example: a = 1; /* first statement */ b = 2; /* second statement */ c = a + b; /* third statement */ In the code above, the statements will be executed always in the following order: first statement, followed by the second and finally by the third statement. What is a conditional control structure? First, some analogy: Scenario number 1: You are driving a car. The road forks into two, which of the two possible paths do you take? Your choice can be influenced by factors such as distance or the amount of traffic. Scenario number 2: “If it rains tomorrow, I will not go out; I’ll just read a book. Otherwise, I will go to the mall and watch a movie.” Question: What do you think is common to these two scenarios? A conditional control structure is organized in such a way that there is always a condition that has to be evaluated first. The condition will either evaluate to a true or false. In C language, remember that a condition will evaluate to a 1 (for true) and 0 (for false). The result of the condition will then dictate the course of action to be taken. The conditional control structure allows the program to make choices depending on a condition. What are the available conditional control structures in C++? There are basically two types of conditional control structures in C++, namely: if statement (including if-else and nested if) switch case statement Prepared: akdbalan Page 1 of 8

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Mapua Institute of Technology School of IT CS103 – Fundamentals of Problem Solving and Programming 1 How do you use an if statement? The syntax for the if statement is as follows: if (<expression>) <statement> The value of <expression> is first evaluated, if it is non-zero, then the condition is specified as true. If it is evaluated as zero, then the condition is specified as false. If the condition is true, then the <statement> is executed. NOTE: the expression must always be enclosed within a pair of parentheses; forgetting the parentheses will result into a syntax error if there is more than one statement that need to be executed when the condition is non-zero, then these statements must be grouped in a pair of curly brackets. Examples:
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 8

#5 Conditional Control Structures - Mapua Institute of...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online