Chapter 5: Control Structure

Slides:



Advertisements
Similar presentations
ALGORITHMS AND FLOWCHARTS
Advertisements

Microsoft® Small Basic
PROBLEM SOLVING TECHNIQUES
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 Making Decisions in a Program.
ALGORITHMS AND FLOWCHARTS
PROGRAMMING, ALGORITHMS AND FLOWCHARTS
Computer Science Selection Structures.
Programming Logic and Design, Second Edition, Comprehensive
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
ALGORITHMS AND FLOWCHARTS CSCI 105 – Computer Fluency.
Chapter 5: More on the Selection Structure
Flowcharting & Algorithms. Quick. Close your Eyes and Listen You are still sitting in the classroom. However, you have been called to the counselor’s.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 5 Making Decisions.
ALGORITHMS AND FLOWCHARTS. A typical programming task can be divided into two phases: Problem solving phase  produce an ordered sequence of steps that.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
UCT Department of Computer Science Computer Science 1015F Iteration
Decision making If.. else statement.
Chapter 4 Selections © Copyright 2012 by Pearson Education, Inc. All Rights Reserved.
ALGORITHMS AND FLOWCHARTS
More on the Selection Structure
GC101 Introduction to computers and programs
REPETITION CONTROL STRUCTURE
Making Choices with if Statements
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Algorithms and Flowcharts
Introduction to Programming
The Selection Structure
Chapter 5: Control Structure
Chapter 4: Control Structures
( Iteration / Repetition / Looping )
Lecture 3 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz
An Introduction to Programming with C++ Fifth Edition
Microsoft Visual Basic 2005 BASICS
Selection By Ramin && Taimoor
ALGORITHMS AND FLOWCHARTS
Control Structure Senior Lecturer
Control Structure Senior Lecturer
Making Decisions in a Program
Chapter 4: Algorithm Design
ALGORITHMS AND FLOWCHARTS
PROBLEM SOLVING CSC 111.
Control Structure Senior Lecturer
SELECTION STATEMENTS (2)
Objectives After studying this chapter, you should be able to:
Understanding the Three Basic Structures
1) C program development 2) Selection structure
Algorithm Discovery and Design
ALGORITHMS AND FLOWCHARTS
3 Control Statements:.
Introduction to Algorithms and Programming
Decision making If statement.
Chapter 4: Algorithm Design
Microsoft Visual Basic 2005: Reloaded Second Edition
SELECTIONS STATEMENTS
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Introduction to Programming
Beginning C Lecture 3 Lecturer: Dr. Zhao Qinpei
CS2011 Introduction to Programming I Selections (I)
Chapter 3: Selection Structures: Making Decisions
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
REPETITION Why Repetition?
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Control Structures.
Presentation transcript:

Chapter 5: Control Structure Part 1: Selection Control Structure

Lesson Outcome At the end of this chapter, students should be able to: Use control structure in problem solving Apply types of selection control structure on the given problems Understand how computer executes an algorithm using selection control structure Understand how to trace an algorithm Write an algorithm using selection control structure to solve a problem

Condition A condition or testing is a comparison between at least two values. It can also test variables that can have either TRUE or FALSE (YES or NO) as the answer. To make a condition or comparison between two values or variables, the following relational operator can be used:

Condition (cont.) Relational operator Meaning > Greater than < Less than == Equals to, is ≤ Less or equals ≥ Greater or equals ≠ Not equal There are a few special words that can be used in a condition like even, odd, negative, positive or divisible.

If Statement A selection control structure is used when we have a choice to carry out actions based on certain conditions. This control structure is implemented in an algorithm using the If statement and case statement. Various of selection control structure are: One way selection Two way selection Multiple selections

One Way Selection Is used when we have only one choice. If the condition is true then the task is performed. If the condition is false, then no processing will take place. The format is as follows: If condition statement(s) EndIf

One Way Selection: Example Example of pseudocode using one way selection: If (a greater than b) calculate p calculate q Display “p = ”, p Display “q = ”, q EndIF

One Way Selection: Example (cont.) Example of flowchart using one way selection: When the value of variable a is greater than the value of variable b then the condition is true. The computer will executes three statements. Otherwise the computer does nothing.

Tracing an Algorithm Using One Way Selection Given the following pseudocode: Test the above pseudocode with the following data: 20 17 41 63 Start Display “Enter 2 numbers: ” Read no1, no2 Initialize sum with 0 If (no1 is divisible by 2) add no1 to sum EndIF If (no2 is divisible by 2) add no2 to sum Display “The total is ”, sum End

Tracing an Algorithm Using One Way Selection (cont.) We can draw the events in computer memory as follows when the data is 20 and 17 : Expected screen: no1 no2 Sum 20 17

Problem Solving using One Way Selection Task: Ask the user to enter a number. Determine whether the number is an even number. If yes, display the number and a message to mention that the number is even number. Problem definition: Information Display number and the message that mention the number is even number when it is an even number. Otherwise do nothing. Data One number Process Check whether the number is even or not. If yes, display the number and the message that mention the number is even number. Otherwise do nothing.

Problem Solving using One Way Selection (cont.) Expected screen: Expected Screen 1 when the user enters even number and expected Screen 2 when the user enter NOT an even number. 19 Screen 1 Screen 2

Problem Solving using One Way Selection (cont.) Pseudocode 1: Flowchart 1 Pseudocode 2: Flowchart 2 Start Read number If (number is even) Display number, “ is an even number” EndIf End Start Read number If (number is divisible by 2) Display number, “ is an even number” EndIf End

Problem Solving using One Way Selection (cont.) Pseudocode 3: Flowchart 3 Start Read number If (number % 2 == 0) Display number, “ is an even number” EndIf End Exercise 5A

Problem Solving using One Way Selection (cont.) Flowchart for Pseudocode 1

Problem Solving using One Way Selection (cont.) Flowchart for Pseudocode 2

Problem Solving using One Way Selection (cont.) Flowchart for Pseudocode 3

Exercise 5A Task: Ask the user to enter two numbers. If the first number can be divided with the second number, display both numbers and the result. Problem definition: Expected screen: Detailed pseudocode: Detailed flowchart Information Data Process

Two Ways Selection In this type of selection, the computer has two choices to choose from. However, it can only execute one choice at one time, either to execute the first choice and ignore the second choice or process only the second choice and ignore the first choice. The format for two ways solution is: If (condition) Statement1(s) Else Statement2(s) EndIf

Two Ways Selection (cont.) The following flowchart shows the use of two ways selection. When the value of variable a is equal to value of variable b, then the condition is true. The computer only executes four statements. Otherwise, the computer only executes three statements.

Tracing an Algorithm using Two Ways Selection Given the following pseudocode: Trace the above pseudocode with the following data: 23, 12, 505 Draw a flowchart Start Display “Enter a number: ” Read no1 If (no1 % 2 == 0) Display “@@@@” Else Display “******” Display newline Display “&&&&&&” EndIF Display “The number entered = ”, no1 End

Tracing an Algorithm using Two Ways Selection (cont.) The location of variable no1 in the computer memory can be visualized as follows when the data is 23 : The expected screen is shown below: no1 23

Tracing an Algorithm using Two Ways Selection (cont.) FALSE TRUE Flowchart 1

Problem Solving Using Two Ways Selection Task: Display the biggest number between two numbers. Problem definition: Information The biggest Data Two numbers Process If the first number is greater than the second number, then the biggest is the first number. Otherwise, the bigger is the second number.

Problem Solving Using Two Ways Selection (cont.) Expected screen: A basic algorithm Start Read 2 numbers Find the biggest Display the biggest End

Problem Solving Using Two Ways Selection (cont.) A detailed algorithm: Start Display “Enter 2 numbers: ” Read no1, no2 If (no1 > no2) max  no1 Else max  no2 EndIF Display “The biggest = ”, max End

Problem Solving Using Two Ways Selection (cont.) The flowchart:

Exercise 5B Task: Calculate the difference between two numbers. Problem definition: Expected screen: Detailed pseudocode: Detailed flowchart Information Data Process

Multiple Ways Selection In solving a problem, sometimes we face a situation that has more than two choices. We can solve this problem using nested if statement, where there is if statement inside another if statement.

Multiple Ways Selection (cont.) The following formats refer to nested if statement: Format 1: If (condition1) :: If (condition2) EndIF Else Statement(s) Inner If statement is in between If … Else

Multiple Ways Selection (cont.) Format 2: If (condition1) Statement(s) Else If (condition2) :: EndIF Inner If statement is in between Else … EndIF

Multiple Ways Selection (cont.) Format 3: If (condition1) If (condition2) :: EndIF Else If (condition3) Inner If statement is in between If … Else and also in between Else … EndIF

Multiple Ways Selection (cont.) Examples of multiple ways selection are given below. Example 1: If (a > b) Display “aaa” Else If (a > 100) Display “bbb” EndIF Pseudocode Flowchart

Multiple Ways Selection (cont.) Example 2: If (a > b) If (a == c) Display “bbb” EndIF Pseudocode Flowchart

Multiple Ways Selection (cont.) Example 3: If (a > b) If (a == 10) Display “aaa” EndIF Else If (b < c) Display “bbb” Display “ccc” Pseudocode Flowchart

Tracing for Multiple Ways Selection Example: Given the following pseudocode, trace the pseudocode using the following test data: i) 100 54 ii) 5 10 iii) 7 7 Start Display “Enter values a and b : ” Read a, b If (a ≥ b) Display “2.1a” If (b > 10) Display “2.1.1a” Else Display “2.1.1b” EndIF Display “2.1b” End

Tracing for Multiple Ways Selection (cont.) Answer: i) Data are 100 54 The location in the computer memory as follows: An expected screen: a b 100 54

Problem Solving using Multiple Ways Selection Example: Ask the user to enter a number. Determine whether the number is positive, negative or zero. Problem definition: Required information Type of a number (positive, negative or zero) Data to be entered A number

Problem Solving using Multiple Ways Selection (cont.) Pseudocode: Start Display “Enter a number : ” Read number If (number == 0) type  “zero” Else //number is either negative or positive If (number > 0) type  “positive” type  “negative” EndIF Display “The number is ”, type End

Problem Solving using Multiple Ways Selection (cont.) Flowchart:

Exercise 5C Analyze the following problems and write pseudocode followed by flowchart. Display the following menu: Ask the user to enter a choice and perform the operation that corresponds with the choice. Choice Operation + Calculate addition of 3 numbers - Substract 1st number with the 2nd number * Multiply 3 numbers / Divide 1st number with the 2nd number

Logical Operator AND and OR We can combine more than one comparisons if it suitable. To combine two or more comparisons, we use a logical operator AND or OR in if statement.

Logical Operator AND and OR (cont.) The syntax to use logical operators is as follows: Using AND logical operator: Using OR logical operator: If (condition1 AND condition2 AND …) //block1 Else //block2 EndIF If (condition1 OR condition2 OR …) //block1 Else //block2 EndIF

Logical Operator AND and OR (cont.) The following table are called truth table. It is used to determine the value when the testing implements the logical operator AND or OR. Below is a truth table for logical operator AND: Condition1 Condition2 Condition1 AND Condition2 Executable block False block2 True block1

Logical Operator AND and OR (cont.) Below is a truth table for logical operator OR: Condition1 Condition2 Condition1 OR Condition2 Executable block False block2 True block1

Logical Operator AND and OR (cont.) Example 1: Ask the user to enter a letter. Determine whether the letter is a consonant or vowel. Problem definition: Required information Consonant or vowel Data to be entered A letter

Logical Operator AND and OR (cont.) Pseudocode: Start Read letter If (letter == ‘a’ OR letter == ‘A’ OR letter == ‘e’ OR letter == ‘E’ OR letter == ‘i’ OR letter == ‘I’ OR letter == ‘o’ OR letter == ‘O’ OR letter == ‘u’ OR letter == ‘U’ OR ) Display “vowel” Else Display “consonant” EndIF End

Logical Operator AND and OR (cont.) Example 2: You are given three numbers. Determine and display the biggest number. Problem definition: Required information The biggest Data to be entered 3 numbers

Logical Operator AND and OR (cont.) Pseudocode: Start Read no1, no2, no3 If (no1 > no2 AND no1 > no3) biggest = no1 Else If(no2 > no3) biggest = no2 biggest = no3 EndIF Display “The biggest number = ”, biggest End

Any Question