+ All Categories
Home > Documents > Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Week 2 Questions from Last...

Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Week 2 Questions from Last...

Date post: 01-Jan-2016
Category:
Upload: magdalene-oneal
View: 215 times
Download: 0 times
Share this document with a friend
35
Monday, Jan 13, 2003 Kate Gregory with material from Deitel and Deitel Week 2 • Questions from Last Week • Control Structures • Functions • Lab 1
Transcript

Monday, Jan 13, 2003 Kate Gregorywith material from Deitel and Deitel

Week 2

• Questions from Last Week

• Control Structures

• Functions

• Lab 1

Monday, Jan 13, 2003 Kate Gregorywith material from Deitel and Deitel

Course ScheduleDate Week Topic Chapter Hand Out Due Back Test

6-Jan-03 1Administrivia / Overview / Intro to C++ / Control Structures 1

13-Jan-03 2Functions / Arrays, Pointers, Strings 2,3,4,5 Lab 1 / Lab 2

20-Jan-03 3 Classes, Data Abstraction 6 Lab 1 5%27-Jan-03 4 More on Classes 7 Lab 3 Lab 2 5%3-Feb-03 5 No Lecture

10-Feb-03 6 Operator Overloading 8 Lab 4 Lab 3 5%17-Feb-03 Reading Break24-Feb-03 7 Inheritance 9 Lab 4 5% Midterm 25%

3-Mar-03 8Virtual Functions and Polymorphism 10 Lab 5

10-Mar-03 9 Stream IO 11 Lab 6 Lab 5 5%17-Mar-03 10 Templates 12,13 Lab 7 Lab 6 5%24-Mar-03 11 Exceptions31-Mar-03 12 File IO 14 Lab 7 5%

??? Exam Final 40%

Monday, Jan 13, 2003 Kate Gregorywith material from Deitel and Deitel

Control Structures

• Normally execution falls from one statement to another:

i++;

j++;

x = y;

Monday, Jan 13, 2003 Kate Gregorywith material from Deitel and Deitel

Conditions

• if

• else

• switch

• Immediate if / ternary operator

Monday, Jan 13, 2003 Kate Gregorywith material from Deitel and Deitel

Repetitions

• while

• for

• do / while

Monday, Jan 13, 2003 Kate Gregorywith material from Deitel and Deitel

if

if ( x > 0) y = 2;z = 3;

if ( x > 0){ y = 2; z = 3;}

Monday, Jan 13, 2003 Kate Gregorywith 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;

Monday, Jan 13, 2003 Kate Gregorywith 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 !=

Monday, Jan 13, 2003 Kate Gregorywith 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";

Monday, Jan 13, 2003 Kate Gregorywith 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 loopsExpression Resulttrue && false falsetrue || false true!false true

Monday, Jan 13, 2003 Kate Gregorywith 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

Monday, Jan 13, 2003 Kate Gregorywith material from Deitel and Deitel

Else

if ( x > 0)

y = 2;

z = 3;

if ( x > 0)

y = 2;

else

z = 3;

Monday, Jan 13, 2003 Kate Gregorywith material from Deitel and Deitel

Switch

if (x == 1)

cout << “one”;

else

if ( x == 2)

cout << “two”;

else

if ( x == 3)

cout << “three”;

Monday, Jan 13, 2003 Kate Gregorywith 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”;}

Monday, Jan 13, 2003 Kate Gregorywith material from Deitel and Deitel

Switch errors

• Forgetting the {}

• Forgetting the break statements

• Forgetting a default case

Monday, Jan 13, 2003 Kate Gregorywith 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;

Monday, Jan 13, 2003 Kate Gregorywith material from Deitel and Deitel

while

x = 0;

while (x < 10)

{

cout << x << ‘\n’ ;

x ++;

}

Monday, Jan 13, 2003 Kate Gregorywith material from Deitel and Deitel

for

for (x = 0; x < 10; x ++)

{

cout << x << ‘\n’ ;

}

• Eliminates forgetting to increment

• Remember to use ; not ,

Monday, Jan 13, 2003 Kate Gregorywith 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

Monday, Jan 13, 2003 Kate Gregorywith material from Deitel and Deitel

Do

x = 11;

do

{

cout << x << ‘\n’ ;

x ++;

} while (x < 10)

Monday, Jan 13, 2003 Kate Gregorywith material from Deitel and Deitel

Break

• Used for exiting a loop immediatelyx = 0;

while (x < 10)

{

if ( x > 4)

break;

cout << x << ‘\n’ ;

x ++;

}

Monday, Jan 13, 2003 Kate Gregorywith 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 ++;}

Monday, Jan 13, 2003 Kate Gregorywith 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

Monday, Jan 13, 2003 Kate Gregorywith 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

Monday, Jan 13, 2003 Kate Gregorywith 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

Monday, Jan 13, 2003 Kate Gregorywith 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

Monday, Jan 13, 2003 Kate Gregorywith 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

Monday, Jan 13, 2003 Kate Gregorywith 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

Monday, Jan 13, 2003 Kate Gregorywith 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);

Monday, Jan 13, 2003 Kate Gregorywith material from Deitel and Deitel

Call By Value

void ChangeIt (int a)

{

a++ ;

}• Some code calls it

int i=3;

ChangeIt(i);

Monday, Jan 13, 2003 Kate Gregorywith material from Deitel and Deitel

Call By Reference

void ChangeIt (int& a)

{

a++ ;

}• Some code calls it

int i=3;

ChangeIt(i);

Monday, Jan 13, 2003 Kate Gregorywith 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

Monday, Jan 13, 2003 Kate Gregorywith 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;}

Monday, Jan 13, 2003 Kate Gregorywith 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

Monday, Jan 13, 2003 Kate Gregorywith material from Deitel and Deitel

For Next class

• Complete Lab 1– Late penalty is TERRIBLE

• Read chapters 4 and 5


Recommended