SLIDE05 - Exposure C Chapter V Variables and Constants The...

This preview shows page 1 - 12 out of 40 pages.

Exposure C++ Chapter V Variables and Constants
Image of page 1

Subscribe to view the full document.

The Limitation of Constants // PROG0501.CPP // This program demonstrates using four constants. #include <iostream.h> void main() { cout << 2500 << endl; cout << 123.4567 << endl; cout << 'M' << endl; cout << "Howdy Neighbors" << endl; }
Image of page 2
Defining and Assigning Variables Variable Definition Syntax datatype Variable Identifier // optional. but desired comment int Number; char Choice;
Image of page 3

Subscribe to view the full document.

// PROG0502.CPP // This program introduces integer, character and real number // variables. This program also demonstrates the use of an // assignment operator. #include <iostream.h> void main() { int Var1; // integer variable char Var2; // character variable float Var3; // real number (floating point) variable Var1 = 5000; // integer assignment Var2 = 'A'; // character assignment Var3 = 3.14159; // real number assignment cout << Var1 << endl; cout << Var2 << endl; cout << Var3 << endl; } PROG0502.CPP OUTPUT 5000 A 3.14159
Image of page 4
// PROG0503.CPP // This program demonstrates that C++ is case sensitive. #include <iostream.h> void main() { int Var1; // integer variable char Var2; // character variable float Var3; // real number (floating point) variable Var1 = 5000; // integer assignment Var2 = 'A'; // character assignment Var3 = 3.14159; // real number assignment cout << var1 << endl; cout << var2 << endl; cout << var3 << endl; } DOES NOT COMPILE AND GIVES ERROR MESSAGES LIKE: Compiling PROG0503.CPP Error PROG0503.CPP 18: Undefined symbol 'var1' Error PROG0503.CPP 18: Undefined symbol 'var2' Error PROG0503.CPP 18: Undefined symbol 'var3'
Image of page 5

Subscribe to view the full document.

Important Warning About Case Sensitivity C++ is case sensitive This means that GrossPay and grosspay are two completely different identifiers to a C++ compiler.
Image of page 6
The Assignment Operator The two previous program examples included the program statements below. At first glance it may appear like an equation because the equality = sign is used. The program statement Var1 = 5000; certainly looks like an equation. It is tempting to "Var1 equals 5000". Saying equals will not really confuse people who understand C++ programming, but it is more correct to say something like: Var1 gets 5000 Var1 becomes 5000 5000 is assigned to Var1 Var1 = 5000; // integer assignment Var2 = 'A'; // character assignment Var3 = 3.14159; // real number assignment
Image of page 7

Subscribe to view the full document.

// PROG0504.CPP // This program demonstrates that the assignment operator can be // used for evaluating mathematical expressions. The program also // shows program output that mixes constants with variables. #include <iostream.h> void main() { int N1 = 10, N2 = 20, N3 = 30; cout << N1 << " " << N2 << " " << N3 << endl; N1 = 100 + 200; N2 = 500 + 300; N3 = -100 + -200; cout << endl; cout << N1 << " " << N2 << " " << N3 << endl; N1 = N2 + 400; N2 = N1 + 600; N3 = N1 + N2; cout << endl; cout << N1 << " " << N2 << " " << N3 << endl; } PROG0504.CPP OUTPUT 10 20 30 300 800 -300 1200 1800 3000
Image of page 8
Assignment Operator Syntax Variable = Expression Rate = 8.765; Sum = X + 13; Total = N1 + N2 + N3;
Image of page 9

Subscribe to view the full document.

Wrong Assignment Operator Syntax Do not switch the left and right side of an assignment operator. The following examples will not work: 8.765 = Rate; X + 13 = Sum; N1 + N2 + N3 = Total;
Image of page 10
// PROG0505.CPP // This program demonstrates that the assignment // operator is not an equal sign. // An assignment statement is not an equation.
Image of page 11

Subscribe to view the full document.

Image of page 12
  • Fall '16
  • Ms. Walker
  • Imperative programming, endl

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern