Introduction to Programming (in C++) Data and statements Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.

Slides:



Advertisements
Similar presentations
1 Demo Reading Assignments Important terms & concepts Fundamental Data Types Identifier Naming Arithmetic Operations Sample Programs CSE Lecture.
Advertisements

Introduction to Programming (in C++) Subprograms: procedures and functions Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science,
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1 Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Programming Switch command. COMP102 Prog. Fundamentals: Switch command / Slide 2 Multiple Selection: The switch Statement value1 action 1 value2 action.
1 CS 105 Lecture 4 Selection Statements Wed, Jan 26, 2011, 6:05 pm.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
Basic Elements of C++ Chapter 2.
C++ Programming Language Day 1. What this course covers Day 1 – Structure of C++ program – Basic data types – Standard input, output streams – Selection.
Introduction to Programming (in C++) Introduction Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Introduction to C++ Programming
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Introduction to Programming (in C++) Data types and visibility Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. Computer Science, UPC.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
Introduction to Programming (in C++) Conclusions Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
High-Level Programming Languages: C++
Data types and their representation Jordi Cortadella Department of Computer Science.
Prime numbers Jordi Cortadella Department of Computer Science.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
Chapter 3 Expressions and Interactivity Department of Computer Science Missouri State Univeristy.
Introduction to Programming (in C++) Algorithms on sequences. Reasoning about loops: Invariants. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept.
Sequences Jordi Cortadella Department of Computer Science.
CS31: Introduction to Computer Science I Discussion 1A 4/9/2010 Sungwon Yang
Introduction to Programming (in C++) Loops Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
CHAPTER 4: CONTROL STRUCTURES - SEQUENCING 10/14/2014 PROBLEM SOLVING & ALGORITHM (DCT 1123)
C++ Programming: Basic Elements of C++.
First steps Jordi Cortadella Department of Computer Science.
Chapter 05 (Part III) Control Statements: Part II.
Data Types and Operations On Data Objective To understand what data types are The need to study data types To differentiate between primitive types and.
1 09/15/04CS150 Introduction to Computer Science 1 Life is Full of Alternatives Part 2.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Lecture #6 OPERATORS AND ITS TYPES By Shahid Naseem (Lecturer)
For loops, nested loops and scopes Jordi Cortadella Department of Computer Science.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Chapter 2: Introduction to C++. Language Elements Keywords Programmer-defined symbols (identifiers) Operators Punctuation Syntax Lines and Statements.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Chapter 3 – Variables and Arithmetic Operations. First Program – volume of a box /************************************************************/ /* Program.
CS201 Introduction to Sabancı University 1 Chapter 2 Writing and Understanding C++ l Writing programs in any language requires understanding.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
Parameter passing Jordi Cortadella Department of Computer Science.
Functions Jordi Cortadella Department of Computer Science.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
A Sample Program #include using namespace std; int main(void) { cout
Bill Tucker Austin Community College COSC 1315
Jordi Cortadella, Ricard Gavaldà, Fernando Orejas
Chapter Topics The Basics of a C++ Program Data Types
What Actions Do We Have Part 1
LESSON 2 Basic of C++.
Bill Tucker Austin Community College COSC 1315
Basic Elements of C++.
For loops, nested loops and scopes
Test Review Computer Science History
Basic Elements of C++ Chapter 2.
Chapter 2 Elementary Programming
Expression Review what is the result and type of these expressions?
Programming Funamental slides
Programming Funamental slides
Summary Two basic concepts: variables and assignments Basic types:
CS150 Introduction to Computer Science 1
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Life is Full of Alternatives
Life is Full of Alternatives
COMS 261 Computer Science I
Presentation 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 2© Dept. CS, UPCIntroduction to Programming

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. 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, UPC3 3 3 true int x bool z

Types A data type specifies: – 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, UPC4

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, UPC5

STATEMENTS Introduction to Programming© Dept. CS, UPC6

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, UPC7

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, UPC8

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, UPC9

Sequence of statements A sequence of statements (not necessarily assignments) is executed sequentially: statement_1; statement_2;... statement_n; Introduction to Programming© Dept. CS, UPC10

Example: swapping the value of two variables Solution 1 int x, y; // Precondition: x=X, y=Y x = y; y = x; // Postcondition: x=Y, y=X Why is this solution incorrect? Solution 2 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, UPC11 ?

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, UPC12

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, UPC13

Examples of I/O in C++ #include 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, UPC14

Examples of I/O in C++ #include using namespace std;... int x, y; double z;... cin >> x >> y >> z; cout << xy << ", " << z+1 << endl;... > in_out , 3.75 Introduction to Programming© Dept. CS, UPC15 Some aesthetic formatting is usually required

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, UPC16

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, UPC17

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, UPC18

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, UPC19

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, UPC20

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; } Introduction to Programming© Dept. CS, UPC21 Blocks of statements are enclosed inside { … }

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) Introduction to Programming© Dept. CS, UPC22 {a,b,c} {a,c}{b,c} {a}{b}{c} Decision tree abababab acacacacbcbcbcbc

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) 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, UPC23

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, UPC24

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; // m=max(a,b) if (c > m) m = c; Introduction to Programming© Dept. CS, UPC25