Download presentation
Presentation is loading. Please wait.
Published byAlison Lindsey Modified over 9 years ago
1
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel
2
Outlines C++ programs in their simplest form Variables and Types Input/Output Assignment operation
3
C++ programs in their simplest form #include using namespace std; //The main function int main( ) {//Your C++ statements below define the main function. … return 0; //Return some integer to finish the main function. }
4
Declaration of variables: Type and Name: e.g. double miles; int ageOfChild1; char input_Character; Variables Computer memory for storing and processing information Names Start with a letter, _ allowed, no space Use meaningful valid names for your variables
5
Types of variables: For storing different kinds of information double for storing real numbers: 3.14, 2.718, … int for storing integers: 1, 2, 3, … char for storing characters: 'a ', 'A ', 'b', … string for storing strings: “Hello”, “good day”, … bool for storing logical values: true, false …
6
Output statements: cout : the console output the screen << : the output operator cout << "The distance is " << miles << endl; cout << "(3+4)*5 =" << (3+4)*5 << endl;
7
Input statements: cin : the console input the keyboard >> : the output operator cin >> miles ;
8
Output statements: Need to separate information of different types: Wrong: cout << "The distance is " miles << endl; cout << "(3+4)*5 =" (3+4)*5 << endl;
9
Input statements: What are wrong below? cin << miles; cin >> miles >> endl;
10
Assignment operation using ' = ' ' = ' actually works more like ' ' instead: Get the result from the expression on the right hand side of ' = ' Store the result into the variable on the left hand side of ' = ' double area, radius=10; area = 3.14 * radius * radius;
11
Assignment operation using ' = ' Something wrong below? double area, radius; area = 3.14 * radius * radius; 3.14 * radius * radius = area;
12
Assignment operation using ' = ' What is wrong below? double area, radius; area = 3.14 * radius * radius; Answer: On Visual Studio: It will raise a runtime exception since radius has no valid contents at this point.
13
Assignment operation using ' = ' What is wrong below? double area, radius; 3.14 * radius * radius = area; Answer: You have no way to store the info in area on the right hand side into 3.14 * radius * radius on the left hand side.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.