1 cs 105 lecture 2 variables wed, jan 19, 2011, 5:07 pm

45
1 CS 105 Lecture 2 Variables Wed, Jan 19, 2011, 5:07 pm

Post on 19-Dec-2015

217 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: 1 CS 105 Lecture 2 Variables Wed, Jan 19, 2011, 5:07 pm

1

CS 105 Lecture 2

VariablesWed, Jan 19, 2011, 5:07 pm

Page 2: 1 CS 105 Lecture 2 Variables Wed, Jan 19, 2011, 5:07 pm

2

• Go to www.cs.iit.edu/~cs105/wed_eve, click on Syllabus, review Lecture 01 notes, course schedule.

• Make sure you go to Lab 0.

• (Make sure you go to Lab 0 even if didn’t miss last week :-)

If You Missed Last Week

Page 3: 1 CS 105 Lecture 2 Variables Wed, Jan 19, 2011, 5:07 pm

3

• Write pseudocode (Design)

• Translate pseudocode into C++ source code (Coding)

• Edit code

• Compile

• Link

Writing a C++ Program

Page 4: 1 CS 105 Lecture 2 Variables Wed, Jan 19, 2011, 5:07 pm

4

• English-like written solution to a programming problem.

• Somewhere between informal description and program written in C++.

• How do we get pseudocode?• How do we solve problems?• Design issues apply to almost any

vocation.

What Is Pseudocode?

Page 5: 1 CS 105 Lecture 2 Variables Wed, Jan 19, 2011, 5:07 pm

5

• Steps in creating pseudocode:

• Understand the problem.

• Decide how to solve the problem.

• Write the solution using a logical sequence of statements.

• Typically, we break down large problems into smaller subproblems and solve those.

Creating Pseudocode

Page 6: 1 CS 105 Lecture 2 Variables Wed, Jan 19, 2011, 5:07 pm

6

• Source code is the actual program code that will run once compiled and linked.

• Pseudocode should be easily translatable into source code.

Pseudo vs Source Code

Page 7: 1 CS 105 Lecture 2 Variables Wed, Jan 19, 2011, 5:07 pm

7

• Every C++ Program Must Have:

• int

• main()

• {

• }

C++ Required Elements

Page 8: 1 CS 105 Lecture 2 Variables Wed, Jan 19, 2011, 5:07 pm

8

// Sam Smith// CS 105// Section 07#include <iostream>using namespace std;intmain(){ cout << “Hello World!!” << endl; return (0);}

Your First Program

Page 9: 1 CS 105 Lecture 2 Variables Wed, Jan 19, 2011, 5:07 pm

9

• Actually uses multiple programs to translate source code program into machine code (executable by hardware).

• Preprocessor

• Actual compiler

• Linker

Compiler

Page 10: 1 CS 105 Lecture 2 Variables Wed, Jan 19, 2011, 5:07 pm

10

• Preprocessing is the actions taken before a source file is handed off to the compiler

• Outcome of preprocessing must still be a correct source code file

• #include is an example

Preprocessing

Page 11: 1 CS 105 Lecture 2 Variables Wed, Jan 19, 2011, 5:07 pm

11

• #include: Replace w/text of specified file.

• #include usually occurs at top of program.

• Pound sign (#) must be in first column.

• Ex: #include <iostream> for typical input and output operations in C++

• Error if file to include can’t be found.

#include

Page 12: 1 CS 105 Lecture 2 Variables Wed, Jan 19, 2011, 5:07 pm

12

• Converts source code into an object file or machine code.

• Each change to source file requires a recompilation before re-execution.

• Compiler also looks for malformed programs — syntax errors a.k.a. “compile time errors”.

Actual Compiler

Page 13: 1 CS 105 Lecture 2 Variables Wed, Jan 19, 2011, 5:07 pm

13

• Syntax error: A non-proper, not allowable, sequence of characters or words given a particular language.

• Typical syntax errors:

• Spelling mistakes

• Punctuation mistakes

• Wrong type of data for operation

Syntax Error

Page 14: 1 CS 105 Lecture 2 Variables Wed, Jan 19, 2011, 5:07 pm

14

• Undefined variable name

• C++ is case sensitive

• Example: “Hi” >> cout (not COUT).

• Unrecognized keyword

• Certain “key” words have special meaning in C++. Example: int.

Spelling Errors Examples

Page 15: 1 CS 105 Lecture 2 Variables Wed, Jan 19, 2011, 5:07 pm

15

• Missing, extra, or misplaced parentheses, braces, commas, semicolons...

• Parentheses unbalanced or of wrong shape.

• Comments malformed.

• Syntax errors are typically listed at bottom of screen when compilation is complete.

Punctuation Error Examples

Page 16: 1 CS 105 Lecture 2 Variables Wed, Jan 19, 2011, 5:07 pm

16

• Two kinds of syntax error messages:

• Warning: Compiler finds possible error, lets it go by without failing.

• Error: Compiler finds an error that causes compile to fail.

Syntax Error Messages

Page 17: 1 CS 105 Lecture 2 Variables Wed, Jan 19, 2011, 5:07 pm

17

• First error in program may “cause” other errors to show up.

• General advice: Fix the first error (and any obvious errors), then recompile.

Syntax Error Messages

Page 18: 1 CS 105 Lecture 2 Variables Wed, Jan 19, 2011, 5:07 pm

18

• Linking connects (i.e. links) your object code with external libraries (external = not written as part of this program).

• Object library contains already-written and compiled code to use with other programs.

• Example: Code for iostream includes definition of “<<“ and “>>”

Linking

Page 19: 1 CS 105 Lecture 2 Variables Wed, Jan 19, 2011, 5:07 pm

19

• If linker cannot find the libraries, error messages are generated.

• Successful linking creates an executable file.

• An executable file can be run.

• (.exe file in Windows)

Linking

Page 20: 1 CS 105 Lecture 2 Variables Wed, Jan 19, 2011, 5:07 pm

20

• Also called semantic or run-time errors.

• Program compiles, but doesn’t produce the expected results.

• Results wrong, missing, extra.

• Program halting wrongly.

• Errors may be repeatable or intermittent.

Logic Errors – “Bugs”

Page 21: 1 CS 105 Lecture 2 Variables Wed, Jan 19, 2011, 5:07 pm

21

• Halts early.

• Never halts (infinite loop).

• Halts due to bad operation.

• Possible example: Dividing by zero.

• Compiler does type checking to avoid wrong kinds of data at runtime.

Halting Problems

Page 22: 1 CS 105 Lecture 2 Variables Wed, Jan 19, 2011, 5:07 pm

22

• (From Navy website for Adm. Grace Murray Hopper)

• 1947: Moth found shorting a relay in early computer.

• “Bugs” and “debugging” already used as terms, but this is first example of a bug being caused by an actual bug.

The “First” Bug

Page 23: 1 CS 105 Lecture 2 Variables Wed, Jan 19, 2011, 5:07 pm

23

The “First” Bug

Page 24: 1 CS 105 Lecture 2 Variables Wed, Jan 19, 2011, 5:07 pm

24

• Test-run program with different inputs.

• Test plan: series of tests (e.g., inputs) that have predetermined expected outputs.

• Test-run program under all potential conditions.

• May need simulator, other tools to look at data & operations during program run.

Detecting Bugs

Page 25: 1 CS 105 Lecture 2 Variables Wed, Jan 19, 2011, 5:07 pm

25

• Debugging: Finding and removing bugs.

• When errors detected, analysis is needed.

• Work backward from symptom: What can cause this symptom?

• Example: We printed x and got the wrong value. Where did x get set/changed? Why was x set to a bad value?

Debugging

Page 26: 1 CS 105 Lecture 2 Variables Wed, Jan 19, 2011, 5:07 pm

26

• Comments are used to help the person reading the program understand it.

• Good comments are invaluable.

• Typically two types of comments.

• Summary of program or major part of it.

• Descriptions of data/variables.

Comments

Page 27: 1 CS 105 Lecture 2 Variables Wed, Jan 19, 2011, 5:07 pm

27

• Summary comment typically describes intent of larger piece of program.

• E.g. overall summary at beginning of program.

• With data/variable definition, comments describe properties, uses of data/variable.

• E.g. x should be ≥ 0 and ≤ y.

Types of Comments

Page 28: 1 CS 105 Lecture 2 Variables Wed, Jan 19, 2011, 5:07 pm

28

• Don’t generally need to comment “what” is happening unless it’s especially complicated.

• Comments are good for saying things you can’t easily infer from the program.

• Why are we doing something?

• What are the properties of and relationships between variables?

What Goes in Comments?

Page 29: 1 CS 105 Lecture 2 Variables Wed, Jan 19, 2011, 5:07 pm

29

• Two ways to write comments in C++

• // Comment to end of line

• /* Comment until star slash */

• First occurrence of star slash (not “matching” like parentheses).

• Can be many lines away.

Comments in C++

Page 30: 1 CS 105 Lecture 2 Variables Wed, Jan 19, 2011, 5:07 pm

30

• Summary Comments

• Preprocessor statements (e.g., #include)

• Namespace declaration

• Main Function

Elements of a Program

Page 31: 1 CS 105 Lecture 2 Variables Wed, Jan 19, 2011, 5:07 pm

31

• Main Function:

int main(){ // statements; return 0;}

Main Function

Page 32: 1 CS 105 Lecture 2 Variables Wed, Jan 19, 2011, 5:07 pm

32

• White Space:

• Not recognized by compiler

• Used by humans to show program structure.

• Indent (e.g. 3 spaces) for each new function, selection, or loop

Program Format

Page 33: 1 CS 105 Lecture 2 Variables Wed, Jan 19, 2011, 5:07 pm

33

• Variables are names (identifiers) used to store values that may change.

• Every variable has a value and a type.

• Type: the kind of value (integer, floating point, character, etc.) being stored.

• Value gets assigned/reassigned as program runs

Variables

Page 34: 1 CS 105 Lecture 2 Variables Wed, Jan 19, 2011, 5:07 pm

34

• In C++, we “declare” variables.

• Tells compiler to set aside storage space.

• Tells compiler the variable’s type (what type of value it will hold)

• May or may not specify an initial value for the variable.

• Must declare variable before using it.

Declarations of Variables

Page 35: 1 CS 105 Lecture 2 Variables Wed, Jan 19, 2011, 5:07 pm

35

int main() { int num1; variable declaration num1 = 10; variable assignment cout << num1; variable output}

•Declaration int num1 includes type and name of variable (no initial value).

Example

Page 36: 1 CS 105 Lecture 2 Variables Wed, Jan 19, 2011, 5:07 pm

36

• int num;

• num = 10;

• num = 15;

<garbage>

num

10

num

Main Memory

15

num

Variables and Main Memory

Page 37: 1 CS 105 Lecture 2 Variables Wed, Jan 19, 2011, 5:07 pm

37

• Storage location of data in a computer.

• Used when a program is running.

• RAM = “Random Access Memory”

• “Wiped clean” when computer rebooted.

• Byte: basic unit of storage (8 bits; can store one letter of English alphabet)

Main Memory

Page 38: 1 CS 105 Lecture 2 Variables Wed, Jan 19, 2011, 5:07 pm

38

• Kilobyte (KB): 1000 (or 1024) Bytes

• Bug photo was 100 KB

• Megabyte (MB): 1,000,000 Bytes

• CD holds 700 MB

• Gigabyte (GB): 1,000,000,000 Bytes

• DVD holds 4.7 GB

Main Memory

Page 39: 1 CS 105 Lecture 2 Variables Wed, Jan 19, 2011, 5:07 pm

39

• Initialization: Giving a value to a variable when its space is allocated (specified in declaration).

• Assignment: Giving a value to a variable after it’s been allocated.

• Use assignment statement to do this.

Initialization/Assignment

Page 40: 1 CS 105 Lecture 2 Variables Wed, Jan 19, 2011, 5:07 pm

40

int main() { int num1 = 10; variable declaration

with initialization

cout << num1; variable output}

Variable Initialization

Page 41: 1 CS 105 Lecture 2 Variables Wed, Jan 19, 2011, 5:07 pm

41

• C++ supports some built-in/”primitive” data types. (Correspond to types of data typically supported by hardware.)

• Various kinds of numbers (integral and floating-point).

• Characters.

Built-In Datatypes

Page 42: 1 CS 105 Lecture 2 Variables Wed, Jan 19, 2011, 5:07 pm

42

• int: Integer, typically -32768 to 32767 (de-pends on machine/compiler). No commas!!

• float: Real number, range typically 10-38 to 1038. E.g. 6.02e23 for 6.02 × 10²³.

• double: Larger exponents, more significant digits than float (typically 10e-308 to 10e308).

Built-In Numeric Types

Page 43: 1 CS 105 Lecture 2 Variables Wed, Jan 19, 2011, 5:07 pm

43

• Can use letters, digits 0–9 and underscores.

• Can’t start with a digit.

• Case-sensitive (Example: NumWidgets is not the same as numwidgets).

• Can’t contain spaces or other characters.

• Practical maximum of 32 characters.

• Cannot use C++ keywords.

Identifiers

Page 44: 1 CS 105 Lecture 2 Variables Wed, Jan 19, 2011, 5:07 pm

44

• Use a meaningful, descriptive name so that variable’s use is easily understood. (E.g. counter, second, minute, length, width.)

• Separate internal words with underscores or with capitalization. (Example: averageRainfall or average_rainfall, not averagerainfall.)

Naming Variables (Cont)

Page 45: 1 CS 105 Lecture 2 Variables Wed, Jan 19, 2011, 5:07 pm

45

• Scope: Area of a program within which a variable can be referenced.

• Typical variable has local scope, within the { … } in which it was defined.

• Later on we’ll see function parameters; they have local scope too.

Scope of a Variable