SFTW120-LectureNotes-05

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

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

View Full Document Right Arrow Icon
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 s Selection structures are used for selecting one of several possible actions in a program s In C, there are two selection structures: b if : for selecting one of at most two options b 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
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 SFTW120 - Programming Science 3 Lecture Notes 5 if s When a selection between two cases is needed s Format of if statement: if ( condition ) statement T ; else statement F ; s If condition evaluates to true, execute statement T s If condition evaluates to false, execute statement F s 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
Background image of page 2
3 SFTW120 - Programming Science 5 Lecture Notes 5 Conditions s The if statement chooses between two cases based on a condition s Conditions are expressions that evaluate to either true or false s Examples of conditions: x <= 0 leap(year) code == 'a' s 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 s 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
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 SFTW120 - Programming Science 7 Lecture Notes 5 Comparison operators s A comparison operator compares two operands and returns either 1 ( true ) or 0 ( false ) s 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 s Sometimes more than one condition need to be combined into one conditional expression b Example: testing if a number ( num ) is inside a valid range b The valid range has an upper and a lower limit ( up , low ) b Wrong (separate conditions): if (num <= up) /* do something */; if (num >= low) /* do something */; b Correct (combined conditions): if (num <= up && num >= low) /* do something */;
Background image of page 4
5 SFTW120 - Programming Science 9 Lecture Notes 5 Logical operators s Complex conditional expressions are formed with logical operators : b && (and): true if both operands are true, otherwise false b || (or): true if at least one operand is true, otherwise false b ! (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 SFTW120 - Programming Science 10 Lecture Notes 5 Examples of logical operators s Given variables: a=0, b=1, c=1 0 !((!a && b) || (b && !c)) 1 !(a && b) 1 (a && b) || c 0 !b 1 !a 1 a || b 1 b && c 0 a && b Value Expression
Background image of page 5

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

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

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 Right Arrow Icon
Ask a homework question - tutors are online