Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 117 Spring 2002 Review for Exam 2 March 6, 2002 open book, 1 page of notes.

Similar presentations

Presentation on theme: "CS 117 Spring 2002 Review for Exam 2 March 6, 2002 open book, 1 page of notes."— Presentation transcript:

1 CS 117 Spring 2002 Review for Exam 2 March 6, 2002 open book, 1 page of notes

2 Material Covered Hanly –Chapter 3 –Chapter 4 –Chapter 5 Friedman-Koffman –Chapter 3 –Chapter 4 –Chapter 5 –Chapter 6

3 Boolean Expressions needed for both selection and repetition don't confuse == with = a==b has value of either true or false a=b has same value as b de Morgan's laws !(p && q) == !p || !q !(p || q) == !p && !q

4 Boolean Operators comparison operators <less than <=less than or equal >=greater than or equal > greater than ==equal !=not equal

5 Boolean operators unary !logical NOT binary operators &&logical and ||logical or short circuit evaluation

6 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

7 Selection select between several alternative blocks of code two types –if else –switch

8 Selection selective execution of parts of the program if if (condition) statement; condition is a boolean expression statement is single statement or block of statements enclosed by {}

9 if..else if (condition) thenDoThis; else doThat; thisAlwaysDone; body of if and else one statement unless { } used

10 Multiple if statements Sometimes there are more than two cases. if (condition) thenDoThis; else if (condition2) doThat; else if (condition3) … else doInAllOtherCases; thisAlwaysDone;

11 format of switch switch (variable) { case value1: action1: break; case value2: action2; break; default:// if no other case holds default action; }

12 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

13 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.

14 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;

15 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

16 Repetition executing same block of code multiple times three forms –while –do - while –for

17 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;

18 Loop Review: do-while Convenient when at least one repetition of loop body must be ensured test condition after execution of body do that; while (condition);

19 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;

20 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

21 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

22 Function Declarations also called prototype or signature can declare a function without knowing how to write the function

23 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.

24 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.

25 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 executed return 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.

26 The function parameters are places to store the values that are passed in. void functions return no value - don't use them with assignment

27 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 name void swap( int & a, int & b); Reference parameters can be changed by the function.

Download ppt "CS 117 Spring 2002 Review for Exam 2 March 6, 2002 open book, 1 page of notes."

Similar presentations

Ads by Google