Download presentation
Presentation is loading. Please wait.
Published byMarshall Thomas Modified over 8 years ago
1
1 A more complex example Write a program that sums a sequence of integers and displays the result. Assume that the first integer read specifies the number of values remaining to be entered. A typical input sequence might be 5 100 6 189 3 2 where the 5 indicates that the subsequent five values are to be summed. What do we need to know? How to read numbers from the keyboard How to store their values An algorithm to compute their sum How to display a message on the screen
2
2 Reading input from the keyboard We've seen the object cout that represents the standard output (the screen). It is used in conjunction with operator<< which transfers the specified data to standard output: cout << "Hello world!"; cout << "Hello " << "world!"; // same as before Similarly, the object cin represents standard input (the keyboard). It is used in conjunction with operator>> to transfer data from standard input to memory. How will we know where exactly in memory this data has been stored, so we can access it? Solution: Somehow assign a name to the memory location where this data resides.
3
3 Storing values in memory View memory as a numbered sequence of bytes, where the number is the address of each byte. In order to easily access the data stored in a memory location, we assign a name to them. A variable is a named memory location where a value is stored. Variables can be used to store various types of data (integers, floating point numbers, user-defined types...) Not all types require the same size. Specify ("declare") in advance the type of data that will be stored in a variable to facilitate memory management. In C++, variables may be declared at any point in a program, as long as it's before they are used.
4
4 Storing values in memory The syntax for declaring a variable is: data_type variable_name ; or, for several variables of the same type: data_type variable_name1, variable_name2 ; The name of a variable is a sequence of letters, digits and underscores that does not begin with a digit. C++ is case sensitive
5
5 Readability Pick meaningful variable names that describe the entity they refer to. Things to avoid: the letter l (el) which can be confused with the digit 1 single-letter variables (there's one exception) variable names starting with one or two underscores (many internal C++ names follow this convention) extremely long names names similar to C++ reserved or predefined words names similar to one another
6
6 Basic types int for signed integers (4 bytes*) range: -2 31..+2 31 unsigned int for unsigned integers (4 bytes*) range: 0..+2 32 float and double for single- and double-precision floating point numbers (4 and 8 bytes respectively) range: ± ~10 -44.85 to ~10 38.53 for floats range: ± ~10 -323.3 to ~10 308.3 for doubles char for characters (1 byte) range: -128..+127 but ASCII uses only 0..127 bool for boolean values (1 byte) range: 0-1 * for our architecture
7
7 Literals #include using std::cout; int main () { cout << 10 << "USD = " << 8.2 << "EUR\n"; return 0; } integer literal double literal string literals
8
8 Literals Major disadvantages: a literal cannot be reused (you have to type it in every time) it is easy to make a typo if you want to change it, you have to make sure you change all of its occurrences inside the program. Good idea: Use #define (C++ preprocessor directive) to define a literal #define NUM_DOLLARS 10 #include using namespace std; int main () { cout << NUM_DOLLARS; return 0; } Look ma, no semicolon!
9
9 #defined constants Location: with other directives (e.g. #include) Syntax: #define UNIQUE_NAME some_value Before compiling, pre-processor does 'find & replace' on your file: every occurrence of UNIQUE_NAME is replaced by some_value. Convention: UNIQUE_NAME is always in UPPERCASE. Major advantages: One single, central location for fixed values prevents scattered, hard-to-find errors in literals makes it easy to modify the value Works for ANY repeated text, not just numbers string literals, even executable text statements…
10
10 named constants Declared like variables BUT preceded with the keyword const initialized at declaration their value CANNOT change afterwards. #include using namespace std; int main () { const int num_dollars = 10; cout << num_dollars; // num_dollars = 20; ILLEGAL! return 0; }
11
11 Operators Assignment operator: = Arithmetic operators: +, , *, /, % Relational operators:, =, ==, != Logical operators: &&, ||, !
12
12 Operators ( ) ! Unary – * / % + – = > = = != && || = higher precedence lower precedence Associativity: left-to-right (except = and unary operators)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.