Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 CS 105 Lecture 2 Variables Wed, Jan 19, 2011, 5:07 pm."— Presentation transcript:

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

2 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

3 3 Write pseudocode (Design) Translate pseudocode into C++ source code (Coding) Edit code Compile Link Writing a C++ Program

4 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?

5 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

6 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

7 7 Every C++ Program Must Have: int main() { } C++ Required Elements

8 8 // Sam Smith // CS 105 // Section 07 #include using namespace std; int main() { cout << “Hello World!!” << endl; return (0); } Your First Program

9 9 Actually uses multiple programs to translate source code program into machine code (executable by hardware). Preprocessor Actual compiler Linker Compiler

10 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

11 11 #include: Replace w/text of specified file. #include usually occurs at top of program. Pound sign (#) must be in first column. Ex: #include for typical input and output operations in C++ Error if file to include can’t be found. #include

12 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

13 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

14 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

15 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

16 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

17 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

18 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 “ >” Linking

19 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

20 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”

21 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

22 22 (From Navy website for Adm. Grace Murray Hopper)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

23 23 The “First” Bug

24 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

25 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

26 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

27 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

28 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?

29 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++

30 30 Summary Comments Preprocessor statements (e.g., #include) Namespace declaration Main Function Elements of a Program

31 31 Main Function: int main() { // statements; return 0; } Main Function

32 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

33 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

34 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

35 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

36 36 int num; num = 10; num = 15; num 10 num Main Memory 15 num Variables and Main Memory

37 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

38 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

39 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/Assignme nt

40 40 int main() { int num1 = 10; variable declaration with initialization cout << num1; variable output } Variable Initialization

41 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

42 42 int: Integer, typically -32768 to 32767 (de- pends on machine/compiler). No commas!! float: Real number, range typically 10 -38 to 10 38. 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

43 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

44 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)

45 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


Download ppt "1 CS 105 Lecture 2 Variables Wed, Jan 19, 2011, 5:07 pm."

Similar presentations


Ads by Google