Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.

Similar presentations


Presentation on theme: "1 C++ Programming Basics Chapter 2 Lecture CSIS 10A."— Presentation transcript:

1 1 C++ Programming Basics Chapter 2 Lecture CSIS 10A

2 2 Agenda Review  An Intermediate Program Making a Program Interactive Assignment Statements Standard Program Structure File output Debugging

3 3 Review –what does this show? int x; x = 8; cout << "x=" <<x<<endl; cout << x << endl << "=x";

4 4 Review –Identify each piece #include using namespace std; int main() { // this program stores data in a variable int m; cout << “Enter the value of m:”; cin >> m; cout << “m = “ << m << endl; system(“pause”); return 0; }

5 5 Variables and Assignments Variables are like small blackboards We can write a number on them We can change the number We can erase the number C++ variables are names for memory locations We can write a value in them We can change the value stored there We cannot erase the memory location Some value is always there

6 6 Review –what does this show? int x; x=6; x=8; cout << "x = " << x;

7 7 Identifiers Variable names are called identifiers Choosing variable names Use meaningful names that represent data to be stored First character must be a letter the underscore character _ Remaining characters must be letters Numbers underscore character

8 8 Which identifiers are legal? feetAverage score 1993taxAverage_score SumAverage.score Valid? FINAL final

9 9 Keywords Keywords (also called reserved words) Are used by the C++ language Must be used as they are defined in the programming language Cannot be used as identifiers Examples (75 altogether): main, while, if, new, for, do, class, struct, cin, cout

10 10 Declaring Variables Before use, variables must be declared Tells the compiler the type of data to store Examples: int one_weight, total_weight; int represents whole numbers Could store 1, 4, -345, etc. one_weight and total_weight are both of type int Many other types, but for now, just use int!!!

11 11 Declaring Variables (Part 2) int main() {  int score1, score2, sum; cin>> … etc … sum=score1+score2; … etc … return 0; } Put all your variable declarations at the top of main

12 12 Declaring Variables (Part 3) Declaration syntax: Type_name Variable_1, Variable_2,... ; Declaration Examples: int m_score, total_score; float moon_distance; int age, num_students; char grade; ONLY USE TYPE int FOR NOW!!!

13 13 Agenda Review An Intermediate Program  Making a Program Interactive Arithmetic Standard Program Structure Debugging

14 14 An intermediate C++ program: // height.cpp // Convert height in feet to inches. #include using namespace std; int main() { int feet, inches; feet = 6; inches = feet * 12; cout << "Height is " << inches << " in."; return 0; }

15 15 Execution – what you see Height is 72 in.

16 16 Execution – what really happens int feet, inches; feet int ?inches ?

17 17 Execution – what really happens feet=6; feet int ?inches 6

18 18 Execution – what really happens inches = feet * 12; feet 6*12 int 72inches 6.0

19 19 Execution – what really happens cout << "Height is " << inches << " in."; feet int 72inches 6.0 Height is 72 in.

20 20 Agenda Review An Intermediate Program Making a Program Interactive  Arithmetic Standard Program Structure Debugging

21 21 Interactive Program The previous program always runs exactly the same way (Height is 72 in) We’d like to be able to “put in” the data we want to process So we add two more lines. One asking for data: cout<<“What is height in feet?”<<endl; The other takes input: cin >> feet;

22 22 An intermediate C++ program: // height.cpp // Convert height in feet to inches. #include using namespace std; int main() { int feet, inches; cout << “What is height in feet?” << endl; //******** cin >> feet; //******** inches = feet * 12; cout << "Height is " << inches << " in."; return 0; }

23 23 Basic Interactive Program int main() { 1)Declare Variables 2)Ask (prompt) for data 3)Input data 4)Calculate result 5)Display result with message return 0; } Go back a slide and number the lines Then open Lab1Basics.cpp and do prob1

24 24 A clever use of comments Inside Lab1Basics.cpp are 10 programming problems…how to keep separate? Remember // ignore to end of line Another type of comment works in “blocks” : /* start a block comment ignored end a block comment with */

25 25 Combine the two… /**** Problem 1 ******** // Here are your instructions (space for your code) // *****End Problem 1 *****/ /**** Problem 2 ******** (etc..) Make // to work on this problem change back to / when done TRY IT NOW! Do problems 1-3 in Lab1Basics.cpp

26 26 Agenda Review An Intermediate Program Making a Program Interactive Arithmetic  Standard Program Structure Debugging

27 27 Assignment Statements An assignment statement changes the value of a variable total_weight = one_weight + number_of_bars; total_weight is set to the sum one_weight + number_of_bars Assignment statements end with a semi-colon The single variable to be changed is always on the left of the assignment operator ‘=‘ On the right of the assignment operator can be Constants -- age = 21; Variables -- my_cost = your_cost; Expressions -- area = length * width;

28 28 Assignment Statements and Algebra The ‘=‘ operator in C++ is not an equal sign The following statement cannot be true in algebra number_of_bars = number_of_bars + 3; In C++ it means the new value of number_of_bars is the previous value of number_of_bars plus 3

29 29 Initializing Variables Declaring a variable does not give it a value Giving a variable its first value is initializing the variable Variables are initialized in assignment statements int miles, area; // declare the variables miles = 26; // initialize the variable area=0; // initialize Declaration and initialization can be combined int miles = 26, area = 0.0; Variables that are going to be input using cin>> or calculated later do not need to be initialized

30 30 Arithmetic in C++ Basic operations + - * / A =  r 2 area=radius*radius*3.14; F = (v -a) force=(v-a)/(v+a); (v+a) More on arithmetic later. C++ has some interesting properties to be explored next chapter.

31 31 Can you guess what is stored in x? int a, b=3, c, x; a=b*2; c=a+3; x=c-2; Program flow is sequential (top to bottom) abcx ?3?? 63?? 639? 6397

32 32 Your turn…what is stored in x? int a, b, c, x=3; a=2; x=3+a; x=x+a; Program flow is sequential, order matters!! abcx

33 33 Agenda Review An Intermediate Program Arithmetic Standard Program Structure  Debugging

34 34 Standard Program Structure To solve most problems, your main() program will generally look like this (conceptually) 1.Declare variables for input, result and intermediate data 2.Ask for data 3.Input data (cin) 4.Calculate result 5.Output result (cout)

35 35 Another interactive C++ program: // Convert input number of nickels and dimes // to cents. #include using namespace std; int main() { int nickels, dimes, cents; cout << "Enter number of nickels and dimes: "; cin >> nickels >> dimes; cents = 5 * nickels + 10 * dimes; cout << nickels << " nickels and " << dimes << " dimes " << "= " << cents << " cents " << endl; return 0; } Notice cascading input

36 36 Your Turn Run the previous code, found in Lab1Basics problem4, and verify it works correctly Then modify the program to input dimes and quarters and calculate the equivalent cents

37 37 Agenda Review An Intermediate Program Arithmetic Standard Program Structure Debugging 

38 38 Syntax Errors // errors.cpp #include using namespace std; int main() { int n; cout << "Hello" << endl; N = 2; cout << "n eqalls " << n cout << "So long << endl; system("pause"); return 0; } This program has 3 errors Run Problem 5 and see what they are

39 39 Tracking them down Syntax— double click on the first error message it takes you to that line of code Look at or above that line for possible errors Fix it and try again Errors often create other errors Just fix the first one, then recompile

40 40 That’s a wrap ! What we learned today: How to write an intermediate C++ program Standard program structure Assignments and Basic Arithmetic How to debug your program

41 41 Go back home proud ! You’re a C++ programmer !


Download ppt "1 C++ Programming Basics Chapter 2 Lecture CSIS 10A."

Similar presentations


Ads by Google