Download presentation
Presentation is loading. Please wait.
1
CS 117 Spring 2002 Decision Making Hanly Chapter 3 Friedman-Koffman Chapter 4
2
Flow control Three types of program flow sequential (what we’ve done so far) selection (Chapter 3) –if - else –switch repetition (Chapter 4) –while –do - while –for
3
Boolean variables bool type - variables that can be either true or false For regular variables, a zero value is considered to be false, anything else is true
4
Boolean operators unary !logical NOT binary operators &&logical and ||logical or p!p TF FT
5
Truth Table pqp && qp || q TTTT TFFT FTFT FFFF
6
Short-circuit evaluation -if the result is uniquely determined by the value of the first operand, the second won’t be evaluated. –p && qhas to be false if p is false –p || qis always true if p is true
7
Boolean Operators comparison operators <less than <=less than or equal >=greater than or equal > greater than ==equal !=not equal
8
bitwise operators (we won’t use these) ^complement &and |or
9
Precedence revisited function calls ( )use to force the desired order !unary - (negation) */ % +-(binary) => ==!= && || =(also+=-=*=/=etc)
10
Selection Statements These provide a way to select different paths through the code –if - else –switch
11
Examples of Boolean Expressions p !p p || q !(p && q) && p||q x < y 0 <=x && x <= 100 a + b == c
12
Cautions a==b is different from a=b using == with double variables is not recommended - test the magnitude of the difference 0 <= x <= 100 is not what you'd expect from math
13
When do you need selection some operations need only be done under certain conditions –you can't withdraw more money than your bank account holds - it doesn’t make sense to have a negative balance sometimes things are done differently in different ranges of a variable –you are taxed differently in different ranges of income –piecewise functions are calculated differently in different regions a program with a menu has to do different things depending on what is selected
14
execute code sometimes
15
if statement if (condition) thenDoThis; thisAlwaysDone; condition is a boolean expression First statement after if is done if condition is true to execute multiple statements, surround them with { }
16
different code at different times
17
if..else if (condition) thenDoThis; else doThat; thisAlwaysDone; body of if and else one statement unless { } used
18
Nested if statements if (condition) { if (condition2) thenDoThis; else doThat; } else doTheOther; thisAlwaysDone;
19
Nested if caution in the absence of { }, an else always goes with the nearest if. if (cond1) if (cond2) thenDoThis; else doTheOther; // done if cond1 true && cond2 false thisAlwaysDone;
20
Multiple if statements Sometimes there are more than two cases. if (condition) thenDoThis; else if (condition2) doThat; else if (condition3) … else doInAllOtherCases; thisAlwaysDone;
21
switch statement another selection statement allows you to select between several discrete values of a variable Use this only with enumerable types (int, char)
22
format of switch switch (variable) { case value1: action1: break; case value2: action2; break; default:// if no other case holds default action; }
23
switch statement The first case for which variable has the value given in the case is executed break forces exit from the switch statement; without it, execution falls through to next case
24
if vs switch ranges of values any type –boolean expressions –double –char –int discrete values enumerable types –int –char
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.