Programming Choice Structures: Switch Statement and if-else Statements, Slides of Computer Science

An outline for a computer science lecture on programming choice structures, with a focus on switch statements and if-else statements. Topics covered include review of choice programming, type bool variables, demorgan's laws, data validation, nested if statements, and the switch statement. Exercises for students to practice applying the concepts.

Typology: Slides

2013/2014

Uploaded on 02/01/2014

savitri_122
savitri_122 🇮🇳

4.6

(14)

184 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Switch statement/choice coding March 2, 2006
1
The
The switch
switch Statement and
Statement and
Programming Choice Structures
Programming Choice Structures
Larry Caretto
Computer Science 106
Computing in Engineering
and Science
March 2, 2006
2
Outline
Review choice programming
Statements (if, if-else, if-else-if)
Conditions; relational and logical operators
Type bool variables
Nested if statements
End-of-file tests
Programming exercises
3
Review Choice (if statements)
Three structures if, if-else, and if-else-if
Based on statement if (<condition>)
Condition used relational operators (<,
>, <=, >=, ==, !=) and logical operators
not(!) and(&&) or(||)
Condition evaluates to true or false
In if-else and if-else-if only one block of
code is executed
4
Review Type bool Variables
Type bool variables have two possible
values: true and false
Can be used to hold result of
expressions that give these values
leapYear = year % 4 == 0 && ( year %
100 != 0 || year % 400 == 0 )
Test bool variables in if statements and
use with logical operators
if ( leapYear && month == 2 ) days = 29;
5
Review DeMorgan’s Laws
Have two bool expressions, a and b,
that can have values of true or false
Combinations of conditions for a and b
satisfy both of the following
!(a && b) = !a || !b
!(a || b) = !a && !b
Proved these using truth table
Application to data validation follows
6
Review Data Validation
Apply DeMorgan’s Law to Validation
badData = x < Min || x > Max;
goodData = x >= Min && x <= Max;
goodData = !badData;
goodData = !(x < Min || x > Max);
!(a|| b) = !a&& !b
goodData = !(x < Min) && !(x>Max)
goodData = x >= Min && x <= Max
pf3
pf4
pf5

Partial preview of the text

Download Programming Choice Structures: Switch Statement and if-else Statements and more Slides Computer Science in PDF only on Docsity!

TheThe switchswitch Statement andStatement and

Programming Choice StructuresProgramming Choice Structures

Larry Caretto Computer Science 106

Computing in Engineering

and Science

March 2, 2006

2

Outline

  • Review choice programming
    • Statements (if, if-else, if-else-if)
    • Conditions; relational and logical operators
    • Type bool variables
    • Nested if statements
  • End-of-file tests
  • Programming exercises

3

Review Choice (if statements)

  • Three structures if, if-else, and if-else-if
  • Based on statement if ( )
  • Condition used relational operators (<,

    , <=, >=, ==, !=) and logical operators not(!) and(&&) or(||)

  • Condition evaluates to true or false
  • In if-else and if-else-if only one block of code is executed

4

Review Type bool Variables

  • Type bool variables have two possible values: true and false
  • Can be used to hold result of expressions that give these values
  • leapYear = year % 4 == 0 && ( year % 100 != 0 || year % 400 == 0 )
  • Test bool variables in if statements and use with logical operators if ( leapYear && month == 2 ) days = 29;

5

Review DeMorgan’s Laws

  • Have two bool expressions, a and b, that can have values of true or false
  • Combinations of conditions for a and b satisfy both of the following
  • !(a && b) = !a || !b
  • !(a || b) = !a && !b
  • Proved these using truth table
  • Application to data validation follows

6

Review Data Validation

  • Apply DeMorgan’s Law to Validation badData = x < Min || x > Max; goodData = x >= Min && x <= Max; goodData = !badData; goodData = !(x < Min || x > Max); !(a || b ) = !a && !b goodData = !(x < Min) && !(x>Max) goodData = x >= Min && x <= Max

7

Exercise

  • An example of an iteration problem, shown below, computes x = A

( )

() ( 1 ) 2 2 n

n n x

x A x + = +

  • Iterations continue until converged, defined as |x (n+1)^ – x (n)^ | < ε 1 + ε 2 |x(n+1)^ |
  • Coding this problem uses variables xNew and xOld for x(n+1)^ and x (n)^ and e and e2 for error tolerances ε 1 and ε 2 8

Exercise Continued

( 1 ) 1 2

( 1 ) ( 1 ) ()

( 1 ) () 2 2

  • (^) = + n + − n + < + n + n

n n untilx x x x

x A x ε ε

  • Define a bool variable, converged, that is true when |x(n+1)^ – x (n)^ | < ε 1 + ε 2 |x(n+1)^ | using fabs(x) for |x|
  • bool converged = fabs(xNew - xOld) < e1 + e2 * fabs(xNew);
  • What condition is true the solution is converged or iterations > maximum
  • converged || iterations > maximum

9

Nested If Statements

  • Can have one if block inside another
  • Example: Find days in month
    • If the number of the month is 4, 6, 9, or 11 the answer is 30
    • If the number of the month is 2
      • If it is a leap year, the answer is 29
      • Otherwise the answer is 28
    • For all other month numbers (1, 3, 5, 7, 8, 10, and 12) the answer is 31 10

Days in Month

if ( month == 4 || month == 6 || month == 9 || month == 11 ) { days = 30; } else if ( month == 2 ) { if (leapYear) // bool var { days = 29; } // continue on next chart

11

Days in Month Continued

else { days = 28; } } // ends else if (month==2) else { days = 31; }

12

The switch Statement

  • An alternative to the if-else-if
    • Tests for equality only
  • Dangerous difference – once a case is selected code for that case and all subsequent cases is executed
  • Start with switch ( ), where can be of type char or int
  • Followed by “cases” with particular values of the expression
  • Following example from Visual C++ Help

19

Alternative Structures

  • Recall previous example of finding days in a month depending on month of year and condition of a leap year or not
  • We used a nested if statement to code this
  • An alternative is to use a combined condition
  • Look at previous code first then consider alternative

20

Days in Month

if ( month == 4 || month == 6 || month == 9 || month == 11 ) { days = 30; } else if ( month == 2 ) { if (leapYear) // bool var { days = 29; } // continue on next chart

21

Days in Month Continued

else { days = 28; } } // ends else if (month==2) else { days = 31; }

22

Alternative Days in Month

if ( month == 4 || month == 6 || month == 9 || month == 11 ) days = 30; else if ( month == 2 && leapYear ) days = 29; else if ( month == 2 ) days = 28; else days = 31;

At this else-if we know that if month equals two it is not a leap year. Why?

23

The Dean’s List

  • Under a proposed change a student makes the dean’s list if the student is a undergraduate and - The student completes 12 units in a with a grade point average of 3.5 or - The student completes at least 6 units with a grade point average of 3.
  • Select variables and write code that sets the bool variable deansList to true if the student makes the list (false if not) (^24)

The Dean’s List Code

  • Use the following variables
    • status – a string variable equal to “grad” for graduate students and “undgrd” for undergraduaute students
    • units – a double variable equal to the units taken
    • gpa – a double variable equal to the grade point average
    • deansList – a bool variable that is true or false is a student is or is not on the list

25

Dean’s List Problem Summary

  • Set bool variable deansList to true if either condition below holds (false if not) - The student completes 12 units in a with a grade point average of 3.5 or - The student completes at least 6 units with a grade point average of 3.
  • Variables to be used
    • type double units for units
    • type double gpa for grade point average
    • type string for status = “grad” or “ugrd” (^26)

The Dean’s List Code II

if (status == “undgrd” ) { if (units >= 12 && gpa >= 3.5 ) deansList = true; else if ( units >= 6 && gpa >= 3.7 ) deansList = true; else deansList = false; } else deansList = false;

27

Deans List Code III

deansList = status == “undgrd” && ( ( units >= 12 && gpa >= 3.5 ) || ( units >= 6 && gpa >= 3.7 ) );