Exercise 1 – Datentypen & Variablen Informatik I / D-ITET Informatik I / D-ITET Exercise 1 – Datentypen & Variablen 1
Agenda Informatik I / D-ITET Variables Declaration & Assignment Allowed names for variables Type Conversion Operators & Arithmetic operations cin & cout Functions Scope 2 2
Data-types Informatik I / D-ITET C++ has 5 built-in basic Datatypes Function Memory int Saves integer numbers 2 Byte float Saves real numbers 4 Byte double Saves real numbers with higher precision than the float data-type 8 Byte bool Saves logical expressions (True/False or respectively 0/1) 1 Byte char Saves characters Memory size may vary, because they depend on the implementation and Hardware. 3 3
Data-types Informatik I / D-ITET The memory is binary: Larger Numbers need many bits: Overflow: int i = MAX_INT; //highest possible value i++; // overflow! Lowest negative value! 5 -> 101 4 -> 100 12 -> 1100 32,767 -> 0111111111111111 s 01111111 11111111 16 bits = 2 bytes 4 4
Declaration & Assignment Informatik I / D-ITET Informatik I / D-ITET Declaration & Assignment Declaration of a variable The syntax for declaring a variable is as follows: <type> <name>; <type> <name> = <Number/variable>; For example: int My_first_Variable; Declares a integer Variable with the Names „My_first_Variable“. 5 5
Declaration & Assignment Informatik I / D-ITET Declaration & Assignment More Examples: double My_first_Variable; My_first_Variable = 5.994; This declares a variable and assignes a value to it one line later. This can also be done like this: double My_first_Variable = 5.994; Thus declaring and assigning the Variable in one step 6
Specifiers Informatik I / D-ITET Specify a modifier to the variable type Optional <specifier> <type> <name>; Example: unsigned int ui; Specifier Function long Variable gets double the memory short Variable gets half the memory unsigned Variable will NOT be able to have a sign (only positives can be stored), thus is able to save numbers with more accuracy/ more range const Variable value cannot be changed 7 7
Allowed names for variables Informatik I / D-ITET Informatik I / D-ITET Allowed names for variables Not all names are allowed for variables! Variable names may only contain alphabetic characters, numeric digits and the underscore character (_) The first character cannot be a numeric digit You cannot use C++ reserved words as names (such as main, int or for) Names should not begin with underscore Characters Uppercase characters are considered to be different from lowercase characters 8 8
Allowed names for variables Informatik I / D-ITET Informatik I / D-ITET Allowed names for variables Examples: int firstname; int Firstname; int leet1337; int 1337leet; int what_a_great_name; int var!; int _thisvar; \\Valid \\Valid, variable differs from firstname \\Not Valid, begins with number \\Not Valid, contains special character \\Valid, but shouldn’t be used, starts with _ 9 9
Arithmetic operations Informatik I / D-ITET Informatik I / D-ITET Arithmetic operations There are 5 types of different Arithmetic Operators: plus (+), minus (-), times(*), divided(/) and Modulo(%) The first 4 are intuitive Modulo returns the remainder of a division For example: 5 % 3; //Result = 2 because 3 goes into the 5 one time with a rest of 2 21 % 5; //Result = 1 10 10
Arithmetic operations Informatik I / D-ITET Informatik I / D-ITET Arithmetic operations Arithmetic Operations are evaluated as follows: Brackets before operators Division, Multiplication and Modulo before Minus and Plus If two arguments have the same precedence level, expression is evaluated left to right 11 11
Arithmetic operations Informatik I / D-ITET Arithmetic operations Examples 5 * 4 + 1; //21 5 * (4 + 1); //25 2 * 3 + 3 * 4 / 2; //12 9 % 4 + 1; //2 9 % (4 + 1); //4 5 * 3 % 4 * 2; //6 5 * (3 % 4) * 2; //30 12
Operators Informatik I / D-ITET But other operators also exist: Assignment: int x,y; x = y; x *= y; // x = x * y; x /= y; x += y; x -= y; x %= y; y = x++; // y = x; x = x + 1; y = ++x; // x = x + 1; y = x; y = x--; // y = x; x = x – 1; y = --x; // x = x -1; y = x; 13
Operators Informatik I / D-ITET But other operators also exist (will be discussed next week): Comparisons int x,y; bool a,b; a == b, x == y, a != b, x != y x > y, x < y, x >= y, x <= y Logical operators !a, a && b, a || b a | b, a & b, a ^ b 14
Type Conversion Informatik I / D-ITET Consider the following code: float ft_var = 5.453; int it_var = ft_var; The Variable „it_var“ cannot save a real number. So what happens when this code is executed? The Answer is, the value which will be assigned to „it_var“ will be converted. This process is called Conversion. 15
Type Conversion Informatik I / D-ITET The assigned value will be converted to type of the variable to which it is assigned to At our Example: float ft_var = 5.453; int it_var = ft_var; This means that the value 5.453 will be converted to an integer. C++ does that by truncating the fractional part of the value Thus it_var has the value 5 after the assignment 16
Type Conversion Informatik I / D-ITET Assignments: Whenever a numerical value is assigned to a variable of another type, the value will be converted to the type of the receiving variable. 17 17
Type Conversion Informatik I / D-ITET Conversion in Expression: A second time of the conversion of variables, is the conversion in Arithmetic expressions in which several types of variables are included like the following: 5.453f * 3 * 9.86 double float int 18
Type Conversion Informatik I / D-ITET A rule of thumb in these cases is that C++ converts the expression into the more general type of the two variables. Thus at our example 5.453f(float) * 3(int) * 9.86(double) = 16.359(float) * 9.86(double) = 161.29974(double) 19
Type Conversion Informatik I / D-ITET A special note: divison of int variables The resulting type of such a division is still an integer variable Rational parts of the division will be truncated Thus: 5 / 2 == 2 //The .5 is truncated 1 / 2 == 0 //Same reason here 20
Type Conversion Informatik I / D-ITET Even more examples 13.0f * 4 = 52.0 (float) 7.0 * (3 / 7) = 0.0 (double) 20 / (10 / 6) = 20 (int) 20 / (10.0 / 6) = 12 (double) 21 21
cin & cout Informatik I / D-ITET So lets get back to some programming Cin and cout are your way to talk to the user With these „objects“ you can print messages on the console For example the program „Hello World“ But you can also print variables on it You can even read in variables 22
cin & cout Informatik I / D-ITET Enable use of cin and cout by: #include <iostream> using namespace std; Use cin and cout with the shift operator ( << or >> ): cout << “using cout“; // printing int i; cin >> i; // reading 23
cout Informatik I / D-ITET The order is important: cout << “using cout“; // valid cout >> “using cout“; // invalid “using cout“ >> cout; // invalid Concatenation is valid: cout << “pri“ << “nting“; // valid Use of variables and numbers is also valid: cout << “var: “ << i << “ num: “ << 3; Note the differenence: cout << “i“; cout << i; 24
cout Informatik I / D-ITET New line: cout << “line1\nline2“; // 2 lines cout << “line1“; cout << “line2“; // just 1 line! cout << “line1“ << “\n“; cout << “line2“; // ok cout << “line1“ << endl << “line2“; 25
cin Informatik I / D-ITET Usage: int i; cin >> i; Can also concatenate: int a,b; cin >> a >> b; Converts to relavent types: int a; cin >> a; // waits for user input // if user inputs a string then a=0 26
cin Informatik I / D-ITET Parses by spaces and new line, but waits for new line: int a,b; cin >> a; // waits for user input // if user input is “65 73“ and Enter // then a = 65; cin >> b; // b = 73 without waiting cin >> a; // waits for user input again 27 27
Structure of a function Informatik I / D-ITET Structure of a function return type function name argument type argument int main ( int argc, char* argv[] ) { cout << “Hello, World!” << endl; return 0; } argument list function body return value function name: name of the function function body: statements to be executed argument: variable whose value is passed into function body from the outside argument type: type of the argument return value: value that is passed to the outside after function call return type: type of the return value (void if there is no return value) 28
Functions Informatik I / D-ITET Advantages Readability int main() { cout << “Please enter two integers: "; int a,b; cin >> a >> b; int res = (a + b) * (a + b); cout << “The result is: " << res << endl; return 0; } 29
Functions Informatik I / D-ITET Advantages Readability int square_of_sum(int i1, int i2) { int r = i1 + i2; return r*r; } int main() cout << “Please enter two integers: "; int a,b; cin >> a >> b; int res = square_of_sum(a,b); cout << “The result is: " << res << endl; return 0; 30
Functions Informatik I / D-ITET Advantages Code re-use int square_of_sum(int i1, int i2) { int r = i1 + i2; return r*r; } int main() cout << “Please enter four integers: "; int a,b,c,d; cin >> a >> b >> c >> d; int res1 = square_of_sum(a,b); int res2 = square_of_sum(c,d); cout << “the results are: " << res1 << “,” << res2 << endl; return 0; 31
Functions Informatik I / D-ITET Arguments and return value Both are optional Invocation of a function must have () Decleration must be before invocation int square_of_sum(int i1, int i2) res1 = square_of_sum( a, b); void print_message() { cout << “This is also a function“ << endl; } Function name Variable name Should be declared first print_message(); 32 32
Scope Informatik I / D-ITET Errors? Scope = validity/visibility of a variable int nonsense(int input) { return input * x; } int main() int x = 3; if (x == 3) int y = 15; int z = nonsense(y); cout << z << endl; return 0; Errors? 33
Scope Informatik I / D-ITET int nonsense(int input) { return input * x; } int main() int x = 3; if (x == 3) int y = 15; int z = nonsense(y); cout << z << endl; return 0; x is not visible here; only visible inside «main» function y is not valid outside { } we cannot use y as an argument for function «nonsense». Scope of x 34