Problem Solving & Program Design in C Sixth Edition By Jeri R. Hanly & Elliot B. Koffman 1-1.
CS 117 Spring 2002 Decision Making Hanly Chapter 3 Friedman-Koffman Chapter 4.
-
date post
21-Dec-2015 -
Category
Documents
-
view
220 -
download
1
Transcript of CS 117 Spring 2002 Decision Making Hanly Chapter 3 Friedman-Koffman Chapter 4.
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
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
Short-circuit evaluation
• -if the result is uniquely determined by the value of the first operand, the second won’t be evaluated.
– p && q has to be false if p is false
– p || q is always true if p is true
Boolean Operators
• comparison operators < less than<= less than or equal>= greater than or equal> greater than== equal!= not equal
Precedence revisited
function calls( )use to force the desired order! unary - (negation) * / %+ - (binary)< <= >= >== !=&&||= (also += -= *= /= etc)
Selection Statements
• These provide a way to select different paths through the code– if - else– switch
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
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
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 { }
different code at different timesexecutebody of if
continue after if
check if condition
FalseTrueexecutebody of else
if..else
if (condition)thenDoThis;
elsedoThat;
thisAlwaysDone;
• body of if and else one statement unless { } used
Nested if statements
if (condition) {if (condition2)
thenDoThis;else
doThat;}
elsedoTheOther;
thisAlwaysDone;
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;
Multiple if statements
• Sometimes there are more than two cases. if (condition)
thenDoThis;else if (condition2)
doThat;else if (condition3)
…else
doInAllOtherCases;thisAlwaysDone;
switch statement
• another selection statement
• allows you to select between several discrete values of a variable
• Use this only with enumerable types (int, char)
format of switch
switch (variable) {case value1:
action1:break;
case value2:action2;break;
default: // if no other case holdsdefault action;
}
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
if vs switch
• ranges of values
• any type– boolean expressions
– double
– char
– int
• discrete values
• enumerable types– int
– char