Description

Introduction to Programming (in C++) Data and statements Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC Outline Variables, data types and expressions Statements: Assignment

Information

Category:
## Self-Help

Publish on:

Views: 17 | Pages: 25

Extension: PDF | Download: 0

Share

Transcript

Introduction to Programming (in C++) Data and statements Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC Outline Variables, data types and expressions Statements: Assignment Input/output Conditional statement Introduction to Programming Dept. CS, UPC 2 Variables and literals Variable: symbolic name to represent data values. A variable is usually associated with a memory location. Intuition: think of a variable as a box containing values of a certain type. int x bool z 3 true In C++ (and many other languages), variables must be declared before they are used. Literal: a constant of a certain type. Examples: -4, , 4.1e-8, true, Greenland Introduction to Programming Dept. CS, UPC 3 A data type specifies: Types The set of values that data of that type can have The type of operations that can be performed with the data. Every programming language has a set of basic data types. Basic data types in C++: int, double, bool, char, string, Introduction to Programming Dept. CS, UPC 4 Expressions Expression: a combination of literals, variables, operators and functions that is evaluated and returns a value Examples a + 3 (i - 1) sqrt(x) log(4 n) (i - 3) = x (a!= b) and (s = abc ) Introduction to Programming Dept. CS, UPC 5 STATEMENTS Introduction to Programming Dept. CS, UPC 6 Statements Any programming language has a set of basic statements to manipulate data (read, write and transform). A program consists of a combination of data and statements to perform some task. A program can become a new statement (function) that can be used in other programs. Introduction to Programming Dept. CS, UPC 7 Assignment Assignment is the fundamental statement of imperative languages: variable = expression Semantics: The value of the expression is evaluated The result is stored in the variable The previous value of the variable is lost Introduction to Programming Dept. CS, UPC 8 Assignment Examples int x, i, j;... // x=3, i=8, j=-2 x = 3 i + j; // x=22, i=8, j=-2 x = x i; // x=14, i=8, j=-2 j = 0; // x=14, i=8, j=0 Variable initialization Variables can be initialized with an expression in their declaration: double pi = ; double two_pi = 2 pi; string my_name = Jordi ; Recommendation: declare the variables when needed (not before). Initialize the variable in the same declaration whenever possible. Introduction to Programming Dept. CS, UPC 9 Sequence of statements A sequence of statements (not necessarily assignments) is executed sequentially: statement_1; statement_2;... statement_n; Introduction to Programming Dept. CS, UPC 10 Example: swapping the value of two variables Solution 1 Solution 2 int x, y; // Precondition: x=x, y=y x = y; y = x;? // Postcondition: x=y, y=x Why is this solution incorrect? int x, y; // Precondition: x=x, y=y int z = x; x = y; y = z; // Postcondition: x=y, y=x A temporary variable is required Introduction to Programming Dept. CS, UPC 11 Swapping two integers with only two variables // Pre: x=a, y=b x = x y; // x=a-b, y=b y = x + y; // x=a-b, y=a x = y - x // Post: x=b, y=a Introduction to Programming Dept. CS, UPC 12 Basic I/O in C++ cin and cout represent the program s default input and output devices respectively (usually, the keyboard and the display). Simple operations: // Read and store in a variable cin variable ; // Write the value of an expression cout expression ; Introduction to Programming Dept. CS, UPC 13 Examples of I/O in C++ #include iostream using namespace std;... int x, y; double z;... cin x y z; cout x y z + 1 endl;... in_out Introduction to Programming Dept. CS, UPC 14 Examples of I/O in C++ #include iostream using namespace std;... int x, y; double z;... cin x y z; cout x y , z+1 endl;... Some aesthetic formatting is usually required in_out , 3.75 Introduction to Programming Dept. CS, UPC 15 Quotient and remainder // Input: reads two integer numbers (a, b) // Output: writes the quotient and remainder // of a/b int main() { int a, b; cin a b; cout Quotient: a/b , Remainder: a%b endl; } Introduction to Programming Dept. CS, UPC 16 Revisiting time decomposition // Input: reads an integer N = 0 that represents // a certain time in seconds // Output: writes the decomposition of N in // hours (h), minutes (m) and seconds (s) // such that 0 = m 60 and 0 = s 60. int main() { int N; cin N; int s = N%60; N = N/60; cout N/60 N%60 s endl; } Introduction to Programming Dept. CS, UPC 17 Conditional statement if ( condition ) statement1 ; else statement2 ; condition is a Boolean expression Semantics: if the condition evaluates true, statement1 is executed, otherwise statement2 is executed. Introduction to Programming Dept. CS, UPC 18 Conditional statement: example int a, b, m;... // Calculation of the maximum of two numbers // Pre: a=a, b=b if (a = b) m = a; else m = b; // Post: a=a, b=b, m=max(a,b) Introduction to Programming Dept. CS, UPC 19 The else part is optional // Input: reads an integer number // Output: writes the absolute value // of the number int main() { } int a; cin a; if (a 0) a = -a; cout a endl; Introduction to Programming Dept. CS, UPC 20 Min and max of two numbers int a, b, minimum, maximum; // Pre: a=a, b=b // Post: a=a, b=b, // minimum=min(a,b), maximum=max(a,b) if (a = b) { minimum = b; maximum = a; } else { minimum = a; maximum = b; } Blocks of statements are enclosed inside { } Introduction to Programming Dept. CS, UPC 21 Max of three numbers (I) int a, b, c, m; // Pre: a=a, b=b, c=c // Post: a=a, b=b, c=c, m=max(a,b,c) {a,b,c} a b a b {a,c} {b,c} a c a c b c b c {a} {c} {b} {c} Decision tree Introduction to Programming Dept. CS, UPC 22 int a, b, c, m; Max of three numbers (I) // Pre: a=a, b=b, c=c // Post: a=a, b=b, c=c, m=max(a,b,c) if (a = b) { if (a = c) m = a; else m = c; } else { if (b = c) m = b; else m = c; } Introduction to Programming Dept. CS, UPC 23 Max of three numbers (II) int a, b, c, m; // Pre: a=a, b=b, c=c // Post: a=a, b=b, c=c, m=max(a,b,c) if (a = b and a = c) m = a; else if (b = c) m = b; else m = c; Introduction to Programming Dept. CS, UPC 24 Max of three numbers (III) int a, b, c, m; // Pre: a=a, b=b, c=c // Post: a=a, b=b, c=c, m=max(a,b,c) if (a = b) m = a; else m = b; if (c m) m = c; // m=max(a,b) Introduction to Programming Dept. CS, UPC 25

Related Search

Similar documents

We Need Your Support

Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

Thanks to everyone for your continued support.

No, Thanks