ALGORITHMS CONDITIONAL BRANCH CONTROL STRUCTURE

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

Nested if-else Statements.  Should be indented to make the logic clear.  Nested statement executed only when the branch it is in is executed. For example,
CSI 1306 PROGRAMMING IN VISUAL BASIC PART 2. Part 2  1. Strings  2. Translating Conditional Branch Instructions  3. Translation Set 2  4. Debugging.
ALGORITHMS - PART 2 CONDITIONAL BRANCH CONTROL STRUCTURE
3 Decision Making: Equality and Relational Operators A condition is an expression that can be either true or false. Conditions can be formed using the.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 9 Decisions, Decisions, Decisions.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Introduction to Computers and Programming Lecture 5 New York University.
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
School of Computing Science CMT1000 Ed Currie © Middlesex University 1 CMT1000: Introduction to Programming Ed Currie Lecture 5B: Branch Statements - Making.
Basic Building Blocks of Programming. Variables and Assignment Think of a variable as an empty container Assignment symbol (=) means putting a value into.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
The Program Design Phases
Branching and Conditions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Chapter 2 - Algorithms and Design
Computer Science Selection Structures.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
1 Conditional statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
Problem Solving with Decisions
What does a computer program look like: a general overview.
Flow of Control Part 1: Selection
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Pseudocode Simple Program Design Third Edition A Step-by-Step Approach 2.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
Chapter 4: Control Structures SELECTION STATEMENTS.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
COMP Flow of Control: Branching 1 Yi Hong May 19, 2015.
Review, Pseudocode, Flow Charting, and Storyboarding.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Decisions, Decisions, Decisions Conditional Statements In Java.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC INTRO TO COMPUTING - PROGRAMMING If Statement.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
ALGORITHMS CONDITIONAL BRANCH CONTROL STRUCTURE 1.
Chapter 4: Control Structures I (Selection)
CS1010 Programming Methodology
Chapter 4: Control Structures I
CNG 140 C Programming (Lecture set 3)
‘C’ Programming Khalid Jamal.
More on the Selection Structure
Algorithms and programming
Chapter 4: Control Structures I
Bill Tucker Austin Community College COSC 1315
CS 326 Programming Languages, Concepts and Implementation
Operator Precedence Operators Precedence Parentheses () unary
REPETITION/LOOP CONTROL STRUCTURE
The Selection Structure
Introduction To Flowcharting
Numbering System TODAY AND TOMORROW 11th Edition
10.2 Implementation and Execution of Recursive Code
SELECTION STATEMENTS (1)
Control Structures.
Microsoft Visual Basic 2005 BASICS
Chapter 4: Control Structures I
Lecture 2: Logical Problems with Choices
Conditionals & Boolean Expressions
Computers & Programming Languages
1) C program development 2) Selection structure
Introduction to Algorithms and Programming
Faculty of Computer Science & Information System
Selection Control Structure
Selection Statements.
Algorithm and Ambiguity
Flowcharts and Pseudo Code
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Selamat Datang di “Programming Essentials in Python”
Presentation transcript:

ALGORITHMS CONDITIONAL BRANCH CONTROL STRUCTURE

Conditional Branch So far, in the method part of our algorithms, the instructions have been executed sequentially However, sometimes we need to vary the order of execution of the instructions. The order will be determined by the value of a condition We will test to determine whether the condition is true or false If the condition is true, we will execute certain instructions If the condition is false, we will execute other instructions

Definition of a Block But first, let us define a block as group of related instructions A block can contain one or as many instructions as we want BLOCK 1 -------------------- Get X Get Y Let Z = X + Y Give Z BLOCK 2 ---------------------- Let X = A + B * C /D

Block The key feature of a block is that it has only one entrance (one way to come in) By executing the first instruction in the block and only one exit (one way out) By executing the last instruction in the block You cannot execute any other instruction in the block without starting with the first and ending with the last instructions

Simple Sequence of Blocks Input Block -------------------- Get X Get Y Process Block ------------------ Let X = X + Y Let Y = 2 * Y Let Z = X + Y Output Block ----------------- Give Z

Test Blocks By adding a test at the beginning of a block, we let the results of the test determine which block of instructions will be executed TEST True Block ---------------------- Do these Instructions if Test is True False Block ----------------------- Do these Instructions if the Test is False

What is a Test? The test used in a conditional branching control structure contains a variable or expression that evaluates to either True or False

Logical Operators in a Test Expression

Examples of Tests

Logical Tests In practice, tests contain variables and expressions, not numbers Suppose that X, Y and Z are 3, 5, 8 respectively

Test Block Indentation is used to show different blocks in an algorithm To write a test block, use an IF statement, and indent the instructions to be executed If (Test) Do this statement Do this statement as well Do this statement after the If statement

Test Block (syntax and interpretation) if (Test) Block1 Interpretation If Test is evaluated to true Block1 is executed, else Block2 is executed. Note that the indentation is important. It determines the beginning and the end of each block. if (Test) Block1 else Block2

Algorithm 2.1(a) Write an algorithm to compute the absolute value of a number. Name: ABSOLUTE Givens: Number Results: Value Intermediates: None Definition: Value := ABSOLUTE (Number) ----------------------- Method Get Number If (Number >= 0) Let Value = Number If (Number < 0) Let Value = (-1) * Number Give Value

C# code for algorithm2.1 static void Main(string[] args) { int number = int.Parse(Console.ReadLine()); int abs=0; if (number >= 0) abs = number; Console.WriteLine(abs); }

C# code for algorithm2.1(a)(cont..) if (number < 0) { abs = -1 * number; Console.WriteLine(abs); }

Else It is redundant to do the test twice as in IF (X > 0) Do this IF (X <= 0) Do that The test should be written as If (X > 0) Else (or Otherwise)

Algorithm 2.1 (b) Write an algorithm to compute the absolute value of a number using only one test Name: ABSOLUTE Givens: Number Results: Value Intermediates: None Definition: Value := ABSOLUTE (Number) ----------------------- Method Get Number If (Number >= 0) Let Value = Number Else Let Value = (-1) * Number Give Value

C# code for algorithm2.1(b) static void Main(string[] args) { int number = int.Parse(Console.ReadLine()); int abs=0; if (number >= 0) abs = number; Console.WriteLine(abs); }

C# code for algorithm2.1(b)(cont..) else { abs = -1 * number; Console.WriteLine(abs); }

Algorithm 2.2 Write an algorithm which finds the largest of three given numbers General Concept Keep track of “Biggest so Far” Look at the first two numbers Store the larger of the two numbers in “Biggest so Far” Compare “Biggest so Far” with the third number If the third number is larger, then store it in “Biggest so Far”

Algorithm 2.2 Write an algorithm which finds the largest of three given numbers Name: BIG3 Givens: N1, N2, N3 Results:Largest Intermediates: None Definition: Largest := BIG3(N1,N2,N3) Method ------------------ Get N1 Get N2 Get N3 If (N1 > N2) Let Largest = N1 Else Let Largest = N2 If (N3 > Largest) Let Largest = N3 Give Largest

C# code for algorithm2.2 static void Main(string[] args) { int number1 = int.Parse(Console.ReadLine()); int number2 = int.Parse(Console.ReadLine()); int number3 = int.Parse(Console.ReadLine()); int largest; if (number1 > number2) largest = number1;

C# code for algorithm2.2(cont..) else largest = number2; if (number3 > largest) largest = number3; Console.WriteLine(largest); }

Algorithm 2.3 Write an algorithm which, when given an ordered list X1, X2, & X3, modifies the list so that the values are in ascending order General Concept Look at the first two numbers, X1 and X2. If X1 is larger than X2, swap them (remember the swap algorithm) Look at X2 and X3. If X2 is larger than X3, swap them This will put the largest number in the X3 position X2 may have changed, so we have to look at X1 again Look again at X1 and X2. If X1 is larger than X2, swap them Now the list is in non-decreasing order

Algorithm 2.3 Write an algorithm which, given an ordered list X1, X2 & X3, modifies it so that the values are in ascending order Method ---------------- Get X1, X2, X3 If (X1 > X2) Let Temp = X1 Let X1 = X2 Let X2 = Temp If (X2 > X3) Let Temp = X2 Let X2 = X3 Let X3 = Temp Give X1, X2, X3 Name: SORT3 Givens: X1,X2,X3 Results : X1,X2,X3 Intermediates: Temp Definition: SORT3(X1,X2,X3)

C# code for algorithm2.3 static void Main(string[] args) { int number1 = int.Parse(Console.ReadLine()); int number2 = int.Parse(Console.ReadLine()); int number3 = int.Parse(Console.ReadLine()); int temp; if(number1 > number2) temp = number1; number1=number2; number2 = temp; }

C# code for algorithm2.3 if(number2 > number3) { temp = number2; } if(number1 > number2) temp = number1; number1 = number2; number2 = temp;

C# code for algorithm2.3 Console.WriteLine(number1); }

Multiple Tests Sometimes we need to perform multiple related tests For example, in assigning grades, a student can receive A+, A, A-….E, F We can add an ELSE IF clause for multiple test results IF (Test 1) Execute block for Test 1 Else IF (Test 2) Execute block for Test 2 Else Execute block for Else

Algorithm 2.4 Write an algorithm which calculates the amount of money to charge for a ticket. The amount varies with the age of the individual. The charge for a person less than 16 is $7. The charge for a person over age 65 is $5 The charge is $10 for everyone else Name: FARE Givens: Age Results: Price Intermediates: None Definition: Price := FARE(Age) Method --------------------- Get Age If (Age < 16) Let Price = $7 Else If (Age > 65) Let Price = $5 Else Let Price = $10 Give Price

C# code for algorithm 2.4 static void Main(string[] args) { int age = int.Parse(Console.ReadLine()); int price; if (age < 16) price = 7; Console.WriteLine("the price of your ticket is " + price); }

C# code for algorithm 2.4(cont.) else if (age > 65) { price = 5; Console.WriteLine("the price of your ticket is " + price); } else price = 10;

Additional Material

Flow Charts

Flow Charts Logic is implemented with a Diamond Symbol There are two exits, which should be labeled Y/N or T/F The two paths need to join before the end of the flowchart

Algorithm 2.1(a) Name: ABSOLUTE Givens: Number Results: Value Intermediates: None Definition: Value := ABSOLUTE (Number)

Algorithm 2.1(b) Name: ABSOLUTE Givens: Number Results: Value Intermediates: None Definition: Value := ABSOLUTE (Number)

Algorithm 2.2 Name: BIG3 Givens: N1, N2, N3 Results:Largest Intermediates: None Definition: Largest := BIG3(N1,N2,N3)

Algorithm 2.3 Name: SORT3 Givens: X1,X2,X3 Results: None Intermediates: Temp Definition: SORT3(X1,X2,X3)

Algorithm 2.4 Name: FARE Givens: Age Results: Price Intermediates: None Definition: Price := FARE(Age)