cs 117 spring 2002 review for exam 2 march 6, 2002 open book, 1 page of notes
Post on 21-Dec-2015
215 views
TRANSCRIPT
CS 117 Spring 2002
Review for Exam 2
March 6, 2002
open book, 1 page of notes
Material Covered
• Hanly– Chapter 3
– Chapter 4
– Chapter 5
• Friedman-Koffman– Chapter 3
– Chapter 4
– Chapter 5
– Chapter 6
Boolean Expressions
• needed for both selection and repetition• don't confuse == with =
a==b has value of either true or falsea=b has same value as b
• de Morgan's laws!(p && q) == !p || !q!(p || q) == !p && !q
Boolean Operators
• comparison operators < less than<= less than or equal>= greater than or equal> greater than== equal!= not equal
Boolean operators
• unary! logical
NOT
• binary operators&& logical and|| logical or
• short circuit evaluation
Selection Review
• Hanly Chapter 3– p 84 #1-4
– p. 92 #1-2
– p. 94 # 1-3
– p 105 # 1-5
• Friedman-Koffman Chapter 4– p 186 # 1-2
– p 210 #3
– 215 #1-2
– p 220 #8-12
Selection
• select between several alternative blocks of code
• two types– if else– switch
Selection
• selective execution of parts of the program
• ifif (condition)
statement;• condition is a boolean expression• statement is single statement or block of
statements enclosed by {}
if..else
if (condition)thenDoThis;
elsedoThat;
thisAlwaysDone;
• body of if and else one statement unless { } used
Multiple if statements
• Sometimes there are more than two cases. if (condition)
thenDoThis;else if (condition2)
doThat;else if (condition3)
…else
doInAllOtherCases;thisAlwaysDone;
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
switch example
• Suppose you want to write a simple calculator program. Your program will read in a character into the variable op and two numbers v1 and v2. You want to use a switch statement to decide what to do with v1 and v2. Provide cases for +, -, / and *, using the first case as a model.
code
switch (op) {case ' % ': ans = v1 % v2; break;
default: cout "error\n";}
case '+': ans = v1 + v2; break;
case '-': ans = v1 - v2; break;
case '*': ans = v1 * v2; break;
case '/': ans = v1 / v2; break;
Repetition Review
• Hanly Chapter 4– p 116 # 1-3
– P 125 # 1-5
– p 134 #1, 5
– p 140 #2
• Friedman-Koffman Chapter 5– p 232 # 2-5– p238 # 1-2– p 245 # 1, 2, 4-6– p 265 #1-2– p 268 #1-2– p 273 #2-3– p274 #1– p 284 #1-5, 7-10– p 285 # 6-7
Repetition
• executing same block of code multiple times
• three forms– while– do - while– for
Loop Review :while
• Most commonly used when repetition is not counter controlled
• condition test precedes each loop repetition• loop body may not be executed at all
while (condition) dothis;
Loop Review: do-while
• Convenient when at least one repetition of loop body must be ensured
• test condition after execution of body
dothat;
while (condition);
Loop Overview
• Counting loop - number of repetitions – known ahead of time – can be controlled by a counter
• also convenient for loops involving non counting loop control with simple initialization and updates
• condition test precedes the execution
for (initialization; condition; modification)doSomething;
Function Review
• Hanly Chapter 5
– p 165 # 1-4
– p 179 # 1-2
– p 185 #1-2
– p 188 # 1-2
• Friedman-Koffman Chapter 3 & 6– p 119 #1
– p 142 #1
– p 165 #10 (just write the functions)
– p 305 #3
– p 313 # 1
– p 337 #1, 5
Functions
• know the difference between a declaration and a definition
• know how and when to use void
• understand the difference between pass by value and pass by reference
• reference parameters
Function Declarations
• also called prototype or signature
• can declare a function without knowing how to write the function
Parts of a declaration
double someFunction( int param1, int param2);• The return type of the function is double. It can
be any type, either built-in or a class type. If nothing is to be returned, it should be void.
• The function name is someFunction. – The rules for naming functions are the same as for
naming variables.
• The parameter list is contained within the parentheses. There– can be any number of parameters, including 0.
Calling a function
• You call a function by giving its name followed by a list of arguments in parentheses. double x = someFunction( arg1, arg2);
• The list of arguments should match the function's parameter list in number of arguments and type.
• arg1 and arg2 may be variables, numbers or expressions. They are evaluated and the values stores in the corresponding function parameters.
Function Definition
• The function definition contains the code that is executed when the function is called.
double someFunction( int param1, int param2) {// code that is executedreturn aDouble; // required for function that returns a value}
• Note: param1 and param 2 are declared in the function signature; don't redeclare them or they will be hidden by the newly declared variables.
• The function parameters are places to store the values that are passed in.
• void functions return no value - don't use them with assignment
Value and Reference Parameters
• by default function arguments are passed by value – they can't be changed by the function
• reference parameters have a & between the type and the argument namevoid swap( int & a, int & b);
• Reference parameters can be changed by the function.