Notes 04 Control Structure - Selection Structure PDF

Title Notes 04 Control Structure - Selection Structure
Course visual programming
Institution Universiti Teknologi MARA
Pages 8
File Size 203.6 KB
File Type PDF
Total Downloads 113
Total Views 149

Summary

Programming...


Description

Chapter 4: Control Structures – Selection Structure Learning Objectives In this chapter you will learn:  To understand the sequencing, selection, and repetition control structures.  To learn the if/else command for selection structure  To learn the switch command for selection structure 1.0 Control Structure A control structure is a block of programming code that analyzes variables and choose a direction in which to go based on given parameters. It orders/commands the flow of the program. All programs can be written using three types of control structures:   

Sequence – statements in a program are executed one after another in the order in which they are written. This is called sequential execution. Selection – used to choose among alternative courses of action. Repetition – allows the programmer to specify that an action is to be repeated while some conditions remain true.

2.0 Sequence Structure The following is an example of statements written in sequence: num1 = 20; num2 = 7; sum = num1 + num2; Each of the preceding statements is executed in the sequence in which they appear. The following diagram flowcharts the flow of control in the preceding code segment: num1 = 20

num2 = 7

sum = num1 + num2

Each statement in a sequence structure is executed one after another in a sequential, linear flow.

Chapter 4: Control Structures – Selection Structure

Page 1 of 8

3.0 Selection Structures Selection structure lets programs follow different paths of execution. In a selection structure, a question is asked, and depending on the answer, the program takes one of the courses of action, and move on to the next event. There are two commands that can be used for selection structures:  if / else – where at each time, there are only two courses of action  switch – where at each time, multiple courses of action can be taken 3.1 if / else statement As mentioned earlier, in a selection structure, a question is asked and the program takes one of the courses of action. In if/else selection statement, the question is asked in a Boolean expression which leads to two formative results: true or false (yes or no). When the result is true, the first course of action is taken; when the result is false, the second course of action is taken. C++ also has the simple if statement. The simple if statement executes an action only if the condition is true. 3.1.1 The simple if statement The syntax for the simple if statement is as follows: if (booleanExpression) { statement(s); }

The execution flowchart is as the following:

boolean expression

false

true statement(s)

next statement

If booleanExpression evaluates as true, the statements inside the block are executed.

Chapter 4: Control Structures – Selection Structure

Page 2 of 8

Example 4.1: if (studentGrade>=60) { cout= 55) is tested. If the second condition is true, the grade becomes ‘B’. If the condition is false, the third condition and the rest of the conditions (if necessary) continue to be tested until a condition is met or all the conditions have proven to be false. If all the conditions are false, the grade becomes ‘D’. The preceding nested if statements can be rewritten using the ternary operator method as in the following: grade=(score >= 70? 'A': score >= 55? 'B': score >= 40? 'C': 'D')

Chapter 4: Control Structures – Selection Structure

Page 5 of 8

3.1.5 Conditional Expression The type of expression used for if statement need not be restricted to only relational or logical operators. It only requires the controlling expression evaluates to either true or false. For that case, a zero value or non-zero value can be used to since a value zero is automatically converted into false and all non-zero values are converted to true. Example 4.5: #include void main() { int a,b; couta>>b; if(b) cout...


Similar Free PDFs