Presentation is loading. Please wait.

Presentation is loading. Please wait.

Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Week 2 Questions from Last Week Control Structures Functions Lab 1.

Similar presentations


Presentation on theme: "Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Week 2 Questions from Last Week Control Structures Functions Lab 1."— Presentation transcript:

1 Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Week 2 Questions from Last Week Control Structures Functions Lab 1

2 Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Course Schedule

3 Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Control Structures Normally execution falls from one statement to another: i++; j++; x = y;

4 Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Conditions if else switch Immediate if / ternary operator

5 Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Repetitions while for do / while

6 Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel if if ( x > 0) y = 2; z = 3; if ( x > 0) { y = 2; z = 3; }

7 Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel If Gotchas Round brackets around the condition are never optional The condition can be true or false, or a number. All non-zero numbers are true. Only zero is false. Never put a ; after the round brackets if ( x > 0); y = 2;

8 Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Comparison Operators Greater Than > Greater Than or Equal To >= Less Than < Less Than or Equal To <= Equal To == Not Equal To !=

9 Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Two conditions if ( x > 0) if (y < 0) cout << "lower right"; Alternatively: if ( x > 0 && y < 0) cout << "lower right";

10 Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Logical Operators && ( logical AND ) –Returns true if both conditions are true || ( logical OR ) –Returns true if either of its conditions are true ! ( logical NOT, logical negation ) –Reverses the truth/falsity of its condition –Unary operator, has one operand Useful as conditions in loops ExpressionResult true && falsefalse true || falsetrue !falsetrue

11 Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Expressions in Conditions if (x+3 > y) if (sin(x) > 0) if (sin(x) > 0 && foo(y) < 0 ) –Short-circuiting

12 Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Else if ( x > 0) y = 2; z = 3; if ( x > 0) y = 2; else z = 3;

13 Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Switch if (x == 1) cout << “one”; else if ( x == 2) cout << “two”; else if ( x == 3) cout << “three”;

14 Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Switch switch (x) { case 1: cout << “one”; break; case 2: cout << “two”; break; case 3: cout << “three”; break; default: cout << “not 1, 2, or 3”; }

15 Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Switch errors Forgetting the {} Forgetting the break statements Forgetting a default case

16 Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Immediate if Also called ternary operator x = y>0? 3 : 6; Equivalent to if (y>0) x=3; else x=6;

17 Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel while x = 0; while (x < 10) { cout << x << ‘\n’ ; x ++; }

18 Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel for for (x = 0; x < 10; x ++) { cout << x << ‘\n’ ; } Eliminates forgetting to increment Remember to use ; not,

19 Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel While over for If you know how many times, use for If you don’t, use while –While not at the end of the file –While the user still wants to continue

20 Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Do x = 11; do { cout << x << ‘\n’ ; x ++; } while (x < 10)

21 Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Break Used for exiting a loop immediately x = 0; while (x < 10) { if ( x > 4) break; cout << x << ‘\n’ ; x ++; }

22 Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Continue Used for skipping the remainder of the body of a loop x = 0; while (x < 10) { if ( x > 4) continue; cout << x << ‘\n’ ; x ++; }

23 Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Continue in for for (x = 0; x < 10; x ++) { if ( x > 4) continue; cout << x << ‘\n’ ; } Continue in a while is only safe after you’ve incremented or moved to the next

24 Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Functions and Methods Really two words for the same thing. A function that is part of a class is called a member function. OO purists sometimes call it a method. A function that is not part of a class is called a global function. –The main() function is a global function

25 Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Global functions Gather up some code to make a single thing Perhaps because it’s used in many places Perhaps because you can understand your code better if it’s not a 10,000 line monolith Perhaps to simplify team programming Library code may be available as functions

26 Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Declare a function int foo (int x, int y); void bar(float f); void report(void); void deleteFiles(); Also called prototyping a function Placeholder names are optional, but always use them

27 Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Where to declare All code that calls the function must declare it first, so the compiler knows about it If the declaration is missing the compiler will complain Often a number of declarations are gathered into a header file

28 Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Implement a function void report(void) { cout << “This is a report”; return; } For void functions you can omit the return Only have the implementation in one file If it’s missing the linker will complain Also called defining a function

29 Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Call a function int i; i = foo ( 3, 4); OK to ignore return values foo (5,6); Compiler will check types i = foo(“Hello”, 4.5);

30 Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Call By Value void ChangeIt (int a) { a++ ; } Some code calls it int i=3; ChangeIt(i);

31 Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Call By Reference void ChangeIt (int& a) { a++ ; } Some code calls it int i=3; ChangeIt(i);

32 Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Reference Advantages When passing objects, the copy can be quite expensive You might want the function to change its parameters The C way, with pointers, makes code hard to read

33 Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Simple function example int DoubleIt(int a) { return 2 * a; } int main() { int x; cout << “Enter a number ”; cin >> x; cout << “Double that is “ << DoubleIt(x); return 0; }

34 Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Scope Variables come into scope when they are declared They go out of scope when they hit a brace bracket: { int i = 3; } i = 7; //compiler error Generally this is an issue when you declare something inside the block of an if/for/while and then try to use it afterwards

35 Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel For Next class Complete Lab 1 –Late penalty is TERRIBLE Read chapters 4 and 5


Download ppt "Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Week 2 Questions from Last Week Control Structures Functions Lab 1."

Similar presentations


Ads by Google