DOC PREVIEW
Saddleback CS 1B - Selection

This preview shows page 1-2-3 out of 10 pages.

Save
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Control Structures 4 types Topic 1 CS1A Review P3 Sequential The program flow moves from one statement to the next in the order it exists in the code top to bottom Selection Decision Selection structures make decisions and execute commands depending upon the decision If statements or Switch statements Selection Chapter 5 in the shrinkwrap 2 of 57 Topic 1 P3 Selection CH 5 Control Structures 4 types 2 Repetition What if I only want some instructions to run some of the time Used when something needs to be repeated Could be a certain number of times until a certain value has been reached or a condition has been met Selection Structures Subprograms functions A small piece of code that performs a specific task Today we will focus on Selection Repetition Don t worry We ll cover functions later Topic 1 P3 Selection CH 5 3 of 57 Selection Structures Selection Choosing between two or more alternative actions Run certain instructions based on some condition Conditions are based on Boolean Expressions An expression that evaluates to 1 of 2 possibilities Either True or False The computer evaluates a Boolean Expression and determines which instructions to execute based on the result Boolean expressions are formed using relational operators 5 of 57 4 of 57 Topic 1 P3 Selection CH 5 Relational Operators Equal Less than Greater than NOTE this is not the same as is an assignment 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 1 or False 0 value 6 Topic 1 P3 Selection CH 5 of 57 Topic 1 P3 Selection CH 5 1 Examples of Boolean Expressions What will be the result of these Boolean functions 5 5 a c 4 3 10 10 20 6 6 5 9 A Z a Z If you compare characters using relational operators It compares the ASCII values in this case a has a greater ASCII value than Z Topic 1 P3 Selection CH 5 7 of 57 3 types of Selection Statements Char SP ASCII Chart for Printing Characters 0 1 2 3 4 5 6 7 CH 5 Topic 1 P3 Selection One way Decisions Dec Char Dec Char Dec 8 9 A B C D E F G H I J K L M N O 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 P Q R S T U V W X Y Z a b c d e f g 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 h i j k l m n o p q r s t u v w x y z DEL 8 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 of 57 One way Decisions If Then Statements Multi way Decisions Two way Decisions If Then Else Statements Conditional Statements Two way Decisions If the condition is true then execute some instructions else the condition is false execute another set of instructions Char 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 3 types of Selection Statements If the condition is true then execute some instructions If the condition is false don t do anything special Decimal Value Multi way Decisions Nested If Then or Nested If Then Else Statements Nested If Then or Nested If Then Else Statements Many options Nested Conditional If Then Else If Switch statements 9 of 57 If Statements 10 of 57 Flowchart for If Then Statement If statements take different forms For now we will focus on the 2 basic forms IF Boolean Expression If Then If Then Else Both of these statements can be nested A simple if then statement is a one way stmt One way decisions F T Statement If a condition is true execute some special instructions Syntax if boolean expression statements Topic 1 P3 Selection CH 5 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 11 of 57 Topic 1 P3 Selection CH 5 12 of 57 2 Example If Then Example Nested If Statements Note this must be the T condition IF hours 40 T Selection structure within another selection structure Used when more than one decision must be made before an appropriate action can be carried out For example an If Then statement that contains another IfThen statement within the statement section F Syntax if boolean expression statements OUTPUT No Overtime OUTPUT Continue if hours 40 cout No overtime endl cout Continue endl Nested Selection Structure Primary Decision always made by the outer selection structure What would the output be for hours 40 Secondary Decision Always made in the inner or nested selection structure hours 120 13 of 57 14 of 57 Topic 1 P3 Selection CH 5 Topic 1 P3 Selection CH 5 Nested If Statements Example Nested If Then Outer if Inner if IF Boolean Expression F T IF hours 40 T F IF Boolean Expression F T IF hourlyEmp Y Statement F Topic 1 P3 Selection CH 5 Primary if hours 40 Secondary if hourlyEmp Y Why not Y cout Overtime Due T OUTPUT Overtime Due Note Each if statement has its own block of16 57 15 of 57 Topic 1 P3 Selection CH 5 If Then Else Statements Flowchart for If Then Else Stmt Two way Decisions Either execute one set of instructions or another Based on a Boolean expression If the condition is true then F Execute one set of instructions Else False F statements Execute another set of instructions Syntax if boolean expression statement s else statement s Topic 1 P3 Selection CH 5 IF Boolean Expression T True Statements 1 The Boolean expression is evaluated 2 If it evaluates to TRUE then statement1 is executed 3 If it evaluates to FALSE then statement2 is executed 17 of 57 Topic 1 P3 Selection CH 5 18 of 57 3 Flowchart for If Then Else Stmt F IF hours 40 Nested If Then Else Example INPUT hours T F OUTPUT F Overtime Due if hours 40 cout No Overtime endl else cout Overtime Due endl OUTPUT No Overtime IF hours 40 Primary Decision T F INPUT hourlyEmp OUTPUT No Overtime Secondary Decision F IF T hourlyEmp Y OUTPUT Sorry you must be hourly to Earn overtime If Then Else statements can also be nested T OUTPUT Overtime Due 19 of 57 of20 57 Topic 1 P3 Selection CH 5 cout Enter hours worked cin hours Outer If Then Else if hours 40 Comparing Floating Point Values cout Are you an hourly employee Will this work cin get hourlyEmp if hourlyEmp Y Inner If Then Else cout Overtime Due else cout Sorry you …


View Full Document

Saddleback CS 1B - Selection

Download Selection
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Selection and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Selection and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?