Control Structure Senior Lecturer

Slides:



Advertisements
Similar presentations
More on Algorithms and Problem Solving
Advertisements

Programming in Visual Basic
ITEC113 Algorithms and Programming Techniques
CS107 Introduction to Computer Science Loops. Instructions Pseudocode Assign values to variables using basic arithmetic operations x = 3 y = x/10 z =
Structured Program Development in C
1 Chapter 2 Problem Solving Techniques INTRODUCTION 2.2 PROBLEM SOLVING 2.3 USING COMPUTERS IN PROBLEM SOLVING : THE SOFTWARE DEVELOPMENT METHOD.
ALGORITHMS AND FLOWCHARTS
Spring 2005, Gülcihan Özdemir Dağ Lecture 3, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 3 Outline 3.1 Introduction.
Chapter 2 - Algorithms and Design
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Selection Control Structure. Topics Review sequence control structure Structure theorem Selection control structure If statement Relational operators.
Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language.
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
Pseudocode Simple Program Design Third Edition A Step-by-Step Approach 2.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
ITEC113 Algorithms and Programming Techniques
CMSC 104: Peter Olsen, Fall 99Lecture 9:1 Algorithms III Representing Algorithms with pseudo-code.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Lab 6 (1) Range Review, Control Logic and Loops ► Control Logic and Loops ► Exercise.
Comments, Conditional Statements Continued, and Loops Engineering 1D04, Teaching Session 4.
Algorithms and Pseudocode
Algorithms JPC and JWD © 2002 McGraw-Hill, Inc. 2 Algorithms 2 An Algorithm is a finite set of precise instructions for performing a computation or for.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Flow Charts And Pseudo Codes Grade 12. An algorithm is a complete step-by- step procedure for solving a problem or accomplishing a task.
Algorithms and Pseudocode CS Principles Lesson Developed for CS4 Alabama Project Jim Morse.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Selection Control Structure
Chapter 4 MATLAB Programming
PROGRAM CONTROL STRUCTURE
Pseudocode Upsorn Praphamontripong CS 1110 Introduction to Programming
Chapter 5: Control Structure
CSC113: Computer Programming (Theory = 03, Lab = 01)
Chapter 4: Control Structures
ALGORITHM Basic CONCEPTS of Basic Concepts of Algorithm
Programming Fundamentals
Lecture 3 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz
SELECTION STATEMENTS (1)
Siti Nurbaya Ismail Senior Lecturer
ALGORITHMS AND FLOWCHARTS
Control Structure Senior Lecturer
Control Structure Senior Lecturer
Lecture 2: Logical Problems with Choices
Chapter 4: Algorithm Design
PROBLEM SOLVING CSC 111.
CS1100 Computational Engineering
SELECTION STATEMENTS (2)
Chapter 3: Introduction to Problem Solving and Control Statements
ALGORITHMS AND FLOWCHARTS
Chapter 4: Algorithm Design
Chapter 5: Control Structure
Understanding Problems and how to Solve them by using Computers
SELECTIONS STATEMENTS
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Click to add Text Computers & Instructions. Computers are given instructions in the form of computer programs that are created through the development.
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Flowcharts and Pseudo Code
Structured Program Development in C++
Chapter 3: Selection Structures: Making Decisions
Basic Concepts of Algorithm
Chapter 2 - Algorithms and Design
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
REPETITION Why Repetition?
Control Structures.
Flow Control I Branching and Looping.
Presentation transcript:

Control Structure Senior Lecturer Siti Nurbaya Ismail Senior Lecturer Faculty of Computer & Mathematical Sciences Universiti Teknologi MARA Kedah (e): sitinurbaya@kedah.uitm.edu.my (b): https://sitinur151.wordpress.com

Previously…

Examine the following problem Solution Kedai Kasut I offers discount to celebrate hari raya. 50% discount is offered for L size shoes. 30% discount for M size and 20% for S size. The formulas to calculate the new price is basically the same. But the discount rate is different for each size of shoe. Here, the shoes size determines the discount rate, the new price will have to be calculated differently for each shoes size. The formulas can be written as: New-price ← price – 20% x price OR New-price ← price – 30% x price New-price ← price – 50% x price The three different formulas demonstrate that there are three choices to select when calculating the new price and the three choices are determined by the shoes sizes whether it is L, M or S

Examine the following problem Solution You just received SPM result. You got a good result. You have many options to further your study. You have many choices depending on your interest and credentials. If you want to be an engineer, you should choose any engineering courses. If you want to be an expertise in computer science, take any computer science courses. If you want to be a chef, it is advisable to choose culinary discipline, and so on.

Selection Control Structure used when we have a choice to do activities based on certain conditions shows decision‐making capabilities of the computer is implemented in an algorithm using the If statement or case statement * If statement * shows how to implement it in a pseudocode and a flowchart variations of selection control structure are: * One way selection * Two ways selection * Multiple selections

If Statement: One Way Selection used when there is only one choice If the condition is true then the task is performed If the condition is false, then no processing will take place a situation where a computer has to do comparison at least between two values and makes a decision if the condition is true then certain action will be taken (statement 1 until statement n will be executed) if the condition is false, no action will be done IF condition statement(s) EndIF this style of writing a program is called indentation the purpose is to make the program easy to read and to ease debugging process.

If Statement: One Way Selection condition Representation Flowchart of One Way Selection

Condition A condition is a comparison at least between two values or variables. To make a condition (comparison between two values), the following relational operator can be used. Relational operator Meaning >  Greater than <  Less than == Equals to, is ≤ Less or equals ≥ Greater or equals != Not equal

Condition Examples of conditions : There are few special words can be used in a condition like even, odd, negative, positive or divisible. Examples: num1 < num2 age is greater or equals to 40 grade equals to ‘A’ grade == ‘A’ grade is ‘A’ status is “excellent” if ( number is odd ) if ( year is divisible by 4 ) If ( number is positive ) Note: When we use single character data as in above (iii) (iv) and (v), use single quotes (‘ ‘) and use double quotes( “ “ ) for a string (vi). Condition used in above (iii), (iv) and (iv) are the same.

Tracing Algorithm Using One Way Selection Example 1 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 % 2 == 0 ) add no2 to sum Display “ The total is “ , sum End Test the above pseudocode with the following data : i. 20 17 ii. 41 63 iii. 18 24

Example 1 Expected Output: The total is 20 The total is 0 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 % 2 == 0 ) add no2 to sum Display “ The total is “,sum End Example 1 Input Data: i. 20 17 ii. 41 63 iii. 18 24 no1 no2 no1 is divisible by 2 sum=no1+sum no2 % 2 == 0 sum=no2+sum sum 20 17 20/2 √ sum=20=0 17 % 2 ≠ 0 41 63 41/2 × 63 % 2 ≠ 0 18 24 18/2 √ sum=18+0 24 % 2 ==0 sum=18+24 42 Expected Output: The total is 20 The total is 0 The total is 42

Tracing Algorithm Using One Way Selection Example2 Start Display “Enter 3 numbers : “ Initialize sum1, sum2, sum3 with zero Read no1, no2, no3 If (no1 is divisible by 2 ) set sum1 with no1 EndIF If ( no2 % 2 == 0 ) set sum2 with no2 If ( no3 is even ) set sum3 with no3   sum = sum1 + sum2 + sum3 Display “ The total is “ , sum End Test the above pseudocode with the following data : i. 20 40 17 ii. 41 63 12 iii. 19 63 35

Example 2 Expected Output: The total is 60 Input Data: i. 20 40 17 ii. 41 63 12 iii. 19 63 35 no1 no2 no3 no1 is divisible by 2 set sum1 with num1 no2%2==0 set sum2 with num2 no3 is even set num3 with no3 sum1 sum2 sum3 sum = sum1+sum2+sum3 20 40 17 20/2 √ sum1=20 40%2==0 √ sum2=40 17 = even x =20+40+0 = 60

Two Ways Selection Have two choices to choose from However, only one choice can be executed at one time either to execute the first choice and ignore the second choice or process only the second choice and ignore the first one If ( condition ) Statement1(s) Else Statement2(s) EndIF

Multiple Ways Selection In solving a problem, sometimes there are situation that has more than two choices. This problem can be solve using multiple ways selection. There are two types of multiple ways selection Simple if statement Nested if statement nested if statement is the scenario where there is if statement inside another if statement

Multiple Ways Selection Simple if statement If (condition) Statement(s) ElseIf (condition) . Else EndIF If (condition) Statement(s) ElseIf (condition) . EndIF

Multiple Ways Selection Nested if statement

Multiple Ways Selection Nested if statement nested if statement is the scenario where there is if statement inside another if statement

Multiple Ways Selection Nested if statement nested if statement is the scenario where there is if statement inside another if statement

Multiple Ways Selection

Multiple Ways Selection

EXERCISES