{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

SFTW120-LectureNotes-05

# SFTW120-LectureNotes-05 - SFTW120 Programming Science...

This preview shows pages 1–6. Sign up to view the full content.

1 SFTW120 Programming Science Lecture Notes 5: C: Selection Structures (Textbook Chapter 4) © Robert P. Biuk-Aghai SFTW120 - Programming Science 2 Lecture Notes 5 C selection structures square6 Selection structures are used for selecting one of several possible actions in a program square6 In C, there are two selection structures: box2 if : for selecting one of at most two options box2 switch : for selecting one of many options if cond. Yes case No case Example: if number = 0 then print error message else divide by number

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 SFTW120 - Programming Science 3 Lecture Notes 5 if square6 When a selection between two cases is needed square6 Format of if statement: if ( condition ) statement T ; else statement F ; square6 If condition evaluates to true, execute statement T square6 If condition evaluates to false, execute statement F square6 In either case, only one statement is executed, never both! SFTW120 - Programming Science 4 Lecture Notes 5 Example of if if (leap(year)) days_in_year = 366; else days_in_year = 365; Sets the value of variable days_in_year to 366 if the function leap returns true for the given year , otherwise sets it to 365 The else-form is optional, e.g. if without else : if (leap (year)) days_in_year = 366; Note: Assume that function leap() has already been defined
3 SFTW120 - Programming Science 5 Lecture Notes 5 Conditions square6 The if statement chooses between two cases based on a condition square6 Conditions are expressions that evaluate to either true or false square6 Examples of conditions: x <= 0 leap(year) code == 'a' square6 In C, the number 0 means false , the number 1 (or any other non-zero number) means true SFTW120 - Programming Science 6 Lecture Notes 5 Comparison operators square6 Many conditions include a comparison operator : not equal to != equal to == greater than or equal to >= less than or equal to <= greater than > less than < Meaning Operator

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
4 SFTW120 - Programming Science 7 Lecture Notes 5 Comparison operators square6 A comparison operator compares two operands and returns either 1 ( true ) or 0 ( false ) square6 Examples (x=1, y=2, num=3, parent=‘M’): num != 42 parent == 'M' x >= y x <= 0 num > x x < y Example 1 (true) != 1 (true) == 0 (false) >= 0 (false) <= 1 (true) > 1 (true) < Value Operator SFTW120 - Programming Science 8 Lecture Notes 5 Complex conditional expressions square6 Sometimes more than one condition need to be combined into one conditional expression box2 Example: testing if a number ( num ) is inside a valid range box2 The valid range has an upper and a lower limit ( up , low ) box2 Wrong (separate conditions): if (num <= up) /* do something */; if (num >= low) /* do something */; box2 Correct (combined conditions): if (num <= up && num >= low) /* do something */;
5 SFTW120 - Programming Science 9 Lecture Notes 5 Logical operators square6 Complex conditional expressions are formed with logical operators : box2 && (and): true if both operands are true, otherwise false box2 || (or): true if at least one operand is true, otherwise false box2 ! (not): true if operand is false, false if operand is true false false false false false false true op1 && op2 true true false true false true true true true op1 || op2 op2 op1 true false false true !op op

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 23

SFTW120-LectureNotes-05 - SFTW120 Programming Science...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online