Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 1400 15 Jan 2007 Chapter 2 sections 1, 2, 4 – 6, 8, 11 - 15.

Similar presentations


Presentation on theme: "CS 1400 15 Jan 2007 Chapter 2 sections 1, 2, 4 – 6, 8, 11 - 15."— Presentation transcript:

1 CS 1400 15 Jan 2007 Chapter 2 sections 1, 2, 4 – 6, 8, 11 - 15

2 Your first C++ program… Boilerplate // Cannon, demo program #include using namespace std; int main() {// program goes here… return 0; }

3 Output… Pseudocode Print “Enter a number: “ C++ cout << “Enter a number: “; Pseudocode Print square C++ cout << square;

4 Input… Pseudocode Get value from user C++ cin >> value;

5 Assignments… Pseudocode Square = n * n C++ square = n * n; Pseudocode Pay = hours * 40 + (hours-40) * Overtime_pay C++ pay = hours * 40 + (hours-40) * overtime_pay;

6 Declarations… All variables must be established or declared prior to use – usually at the top of the program Integers are declared using int. For example; int value; int width, height, depth; A variable name must begin with a letter, followed by letters, digits, or underscores.

7 Pseudocode Example: “Write a program to calculate the volume of a box, using dimensions provided by the user.” Print “Enter height, width, and depth of box:” Get height from user Get width from user Get depth from user volume = height * width * depth Print “Volume is: “ Print volume

8 C++ boilerplate… // Cannon, demo program #include using namespace std; int main() { return 0; }

9 Adding C++ instructions… // Cannon, demo program #include using namespace std; int main() {int height, width, depth, volume; cout << “Enter height, width, and depth: “; cin >> height >> width >> depth; volume = height * width * depth; cout << “Volume is: “ << volume; return 0; }

10 C++ is case sensitive! Use lower case for simplicity Upper case or underscore may be used to separate multi-word variable names int phone_number;// easiest to read int phoneNumber;// easier to read int phonenumber;// not as easy to read

11 Extended syntax… Input and output statements can be cascaded; cin >> height >> width >> depth; cout << “the value of width is: “ << width; Declarations can be a list; int height, width, depth, volume; Output messages can include control characters such as \n (new line) and \a (beep) cout << “This appears on line 1 \n This is on line 2”; Output messages may output endl to force a new line cout << “this is line1 “ << endl << “This is line 2”;

12 Integer variables Declaring variables as type int – –Only allows them to hold an integer (+ or -) –Range -2,147,483,648 to 2,147,483,647 Variations – –unsigned int –long –short… etcetera

13 Floating point variables… Declaring variables to be type float – –Allows them to hold an approximate floating- point value (precision to ~6 digits) –Range +/- 3.4 x 10 -38 to +/- 3.4 x 10 38 –Constants may be expressed in floating-point notation or E-notation –E-notation is like scientific notation 3.14 x 10 5 is the same as 3.14E5

14 Literals vs. numbers A literal or string is enclosed in quotes cout << “hello” << “my name is Fred”; Any letter or character may be part of a literal Literals are for humans to read – you cannot do math with a literal or store a literal in a numeric variable! int x; x = “5”;// ouch!!

15 Keywords Certain words are reserved in C++ and cannot be used for variable names Fig 2.4

16 Mathmatical Operators Addition and subtraction: +, - Division and multiplication:/, * Modulus (remainder):% Precedence: *, /, % are higher than +, -

17 Truncation An int variable can only hold integers int whole; float decimal; decimal = 3.14159; whole = 25; Integer truncation will occur on; –assignment:whole = decimal; –division:decimal = 1 / 3; An integer divided by an integer is always an integer!!

18 Scope A variable may only be used after it is declared: {// age may not be used here … int age; …// age may be used here }


Download ppt "CS 1400 15 Jan 2007 Chapter 2 sections 1, 2, 4 – 6, 8, 11 - 15."

Similar presentations


Ads by Google