Programming Basics Topic 8 Part 2 Selection Repetition Chapter 3 Part 4 Control Logic Structures All modern programming languages are based on 3 basic control structures Sequence Instructions are executed one after another in the order they appear in the program Until another control structure takes precedence Selection Based on some condition either one part of the program is executed or another part is executed The program chooses which part to execute based on the condition Repetition Part of the code is executed over and over repeated This can be for a set number of times or until a condition is met Chapter 3 Programming Basics 1 of 53 Selection Structures What if I only want some instructions to run some of the time Chapter 3 Programming Basics 2 of 53 1 Selection Operators Selection Choosing between two or more alternative actions Alter the sequential flow of the instructions in a program Based on a Boolean Expression An expression that evaluates to 1 of 2 possibilities Either True or False The computer evaluates a Boolean Expression and determines which instruction to execute based on the result Boolean expressions are formed using relational operators 3 of 53 Chapter 3 Programming Basics Relational Operators Symbol Name Less than Greater than NOTE this is not the same as is an assignment Equal Less than or equal Greater than or equal Not Equal We use Relational Operators to compare values in Selection Statements These will return a True or False value 4 of Chapter 3 Programming Basics 53 Relational Operators Examples How would the computer evaluate these expressions 4 7 3 1 3 1 11 8 41 1 42 41 1 42 12 12 Chapter 3 Programming Basics 5 of 53 2 Selection Statements If If statements take one of two forms If Then If Then Else These can be nested A simple if then statement is a one way statement A one way decision either executes some additional instructions if the decision is true or does nothing if it is false 6 of 53 Chapter 3 Programming Basics Flowcharting Selection Statements We need a new Symbol Decision 7 of 53 Chapter 3 Programming Basics Flowchart pseudocode for If Then Statement IF condition F T True instructions PSEUDOCODE IF condition THEN true instructions END IF NOTE the indent This denotes which instructions should run when the condition is TRUE 1 The Boolean expression is evaluated 2 If it evaluates to TRUE then the True Instructions are executed 3 If it evaluates to FALSE the statement is ignored and the program continues with the next executable statement Chapter 3 Programming Basics 8 of 53 3 If Then Example IF hours 40 F T OUTPUT No overtime OUTPUT Continue PSEUDOCODE IF hours 40 THEN OUTPUT No overtime END IF What would the output be for hours 40 hours 120 OUTPUT Continue 9 of 53 Chapter 3 Programming Basics Exercise 1 Write the flowchart for a code segment that divides two numbers In order to prevent an error we need to make sure that the bottom number is not equal to 0 If it isn t equal to 0 output the result of the division Draw the flowchart and write the pseudocode Chapter 3 Programming Basics 10 of 53 If Then Exercise PSEUDOCODE 11 of 53 Chapter 3 Programming Basics 4 If Then Else Statements Two way Decisions Either execute one set of instructions or another Based on a Boolean expression If the condition is true then Execute one set of instructions Else Execute another set of instruction 12 of 53 Chapter 3 Programming Basics Flowchart for If Then Else Stmt F IF Condition False F Instructions T True Instructions 1 The Boolean expression is evaluated 2 If it evaluates to TRUE then True Instructions are executed 3 If it evaluates to FALSE then False Instructions are executed Chapter 3 Programming Basics 13 of 53 Pseudocode IF THEN ELSE statement Pseudocode for an IF THEN ELSE statement NOTE The indents These denote which instructions should run based on the condition IF condition THEN true instructions ELSE false instructions END IF 14 of 53 5 If Then Else Example F T IF hours 40 OUTPUT Overtime due PSEUDOCODE IF hours 40 THEN OUTPUT No overtime ELSE OUTPUT Overtime due END IF OUTPUT No overtime If Then Else statements can also be nested 15 of 53 If Then Else Exercise Let s expand upon our division problem If the bottom number is equal to 0 we will display the following error message Error can t divide by 0 Otherwise output the result of the division Draw the flowchart and write the pseudocode Chapter 3 Programming Basics 16 of 53 If Then Else Exercise PSEUDOCODE 17 of 53 6 Selection Exercise Problem Statement Farmer Pete is trying to determine which animals to store in the larger pen He needs to determine if he has more sheep or more pigs His program should state which animal is most populous Design the algorithm using a HIPO chart followed by pseudocode and a flowchart What are our Inputs What are our Outputs Draw the HIPO chart Chapter 3 Programming Basics 18 of 53 HIPO Chart Remember we should refine to the level of 1 instruction The HIPO chart should show the structure of the code note the way decisions are handled 19 of 53 Write the PSEUDOCODE 20 of 53 7 Draw the FLOWCHART What variables do we need Input Output If Then Else statements can also be nested Processing Chapter 3 Programming Basics 21 of 53 Nesting If Then Else Statements Chapter 3 Programming Basics 22 of 53 Repetition 8 Control Logic Structures Sequence Instructions are executed one after another in the order they appear in the program Until another control structure takes precedence Selection Based on some condition either one part of the program is executed or another part is executed The program chooses which part to execute based on the condition Repetition Part of the code is executed over and over repeated This can be for a set number of times or until a condition is met Chapter 3 Programming Basics 24 of 53 Repetition Structures What if I want some instructions to run over and over again Chapter 3 Programming Basics 25 of 53 Repetition Structures Repetition When a set of instructions need to be executed more than 1 time Run a select set instructions repeatedly Conditions again are based on a Boolean Expression The computer evaluates a Boolean Expression and executes the code until that condition is FALSE It can execute a set number of times or based on some event that occurs in the loop until some condition is false 26 of 53 Chapter 3 Programming Basics 9 Flowcharting loops We use the same Symbol we used with IF Statements Because loops are based on a Decision Decision 27 of
View Full Document
Unlocking...