Introduction to C++ Lecture Set 2. Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 1

Size: px
Start display at page:

Download "Introduction to C++ Lecture Set 2. Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 1"

Transcription

1 Introduction to C++ Lecture Set 2 Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 1

2 More Arithmetic Operators More Arithmetic Operators In the first session the basic arithmetic operators were introduced. In addition to these, C++ contains a number of other operators which are, in effect, shorthand for common combinations of the basic operators. Copy Assignment Operators Because statements such as: a = a + b; are very common there exists a shorthand. a += b; Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 2

3 More Arithmetic Operators Similar Copy Assignment Operators exist for subtraction, multiplication, division and modulus. -= *= /= %= For example, the code a = 2; a*= 2; will result in a containing the value 4. Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 3

4 More Arithmetic Operators Pre- and Post- increment/decrement Operators c++; is shorthand for c=c+1; However, this operator comes in two flavours: ++c; c++; Called the pre-increment and post-increment operators respectively. Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 4

5 More Arithmetic Operators Consider the code fragment: c = 1; a = ++c; This illustrates the pre-increment operator which means "Add 1 to c and assign result to a". This will result in both a and c containing the value 2. Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 5

6 More Arithmetic Operators This should be compared with: c = 1; a = c++; Which illustrates the post-increment operator which means "Assign c to a then add 1 to c". In this case c will contain the value 2 but a the value 1. There are also corresponding pre- and post-decrement operators -- Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 6

7 More about iostreams More about iostreams Previously we introduced the output stream cout which which we have used to print simple text and expressions to the console. e.g. int i=10;... cout << "i = " << i << endl; The iostreams package defined in <iostream> provides the functionality for simple input of data as well as output. In fact 3 streams are defined: cin cout cerr standard input stream standard output stream standard error (output) stream Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 7

8 More about iostreams For all of the built-in types (int, double, char etc.) operators << and >> are defined to allow output and input to these standard streams. e.g. in the program readint.cpp the lines: int i; cout << "Enter an integer" << endl; cin >> i; cout << "i= " << i << endl; write the character string "Enter an integer" to the standard output stream then reads the value of i from the standard input. Finally the string "i= ", followed by the value of i is written to the standard output. Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 8

9 More about iostreams Formatted Output These streams may be manipulated to allow fine control over the format of output. In general this is complicated and a full description is beyond the scope of this course. ( Those who are brave can read Stroustrup chapter 21 ). Here we discuss a few of the most useful features. The output format used for floating point numbers is changed through the special state manipulation functions: cout.setf( ios::scientific, ios::floatfield ); cout.setf( ios::fixed, ios::floatfield ); sets the format to scientific form e.g e+03 sets the format to fixed point format cout.setf( std::_ios_fmtflags(0), ios::floatfield ); resets the format to the default (general) format Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 9

10 More about iostreams The precision used when formatting floating point numbers (only) is controlled by the precision() (member) function. e.g. cout.precision(8); Sets the precision to 8 figures (or for the scientific or fixed formats 8 decimal places). The default precision is 6. This remains continuously in force until the precision() function is called again. Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 10

11 More about iostreams The width() function specifies the minimum number of characters to be used for the NEXT output operation ONLY. This is demonstrated by the example iwidth.cpp int i=12; cout<<i<<endl; cout.width(4); cout<<i<<endl; cout.width(8); cout<<i<<endl; will print something like: In the second output statement 12 is preceded by 2 spaces in a field of 4 characters giving the output. In the third output statement 12 is preceded by 6 spaces in a field of 8 characters giving the output. Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 11

12 The If Statement In order to write any but the most trivial program there needs be some mechanism to allow the conditional execution of a block of code. In C++ this facility is provided by the if statement. Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 12

13 The If Statement In its simplest form we require the conditional execution of only one statement: if( logical_expression ) a_single_statement ; e.g. if ( x > 5 ) cout << " x is greater than 5 " << endl; If (and only if) the logical_expression is evaluated to be logically TRUE then the following statement is evaluated. Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 13

14 If statement In the more general case, where there are several statements to conditionally execute, we surround the block of code with curlies {} if( logical_expression ) { } Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 14

15 Introduction to C++ The Relational Operators In order to form a logical_expression there is a special set of operators which allow the comparison of two variables (or expressions): There are 6 operators: == is equal to!= is NOT equal to > is greater than >= is greater or equal to < is less than <= is less than or equal to DO NOT confuse the equality operator, ==, with the assignment operator, =. Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 15

16 If statement An expression formed using a relational operator always evaluates to be logical TRUE or FALSE. Some examples of the use of relational operators in the if statement: if ( i == 10 ) cout << " i is 10! " << endl ; if ( (x*x + y*y) > 5 ) { cout << " x-squared + y-squared is greater than 5 " << endl; } Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 16

17 Logical operators The Logical Operators It is often useful to combine several conditions in a single logical expression. To do this there are 3 logical operators : && Logical AND Logical OR! Logical NOT Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 17

18 logical operators These operators obey the rules of Boolean Algebra. If A and B are logical expressions then the results of these operators are according to this truth table A B A B A&&B!A T T T T F T F T F F F T T F T F F F F T Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 18

19 If statement The combination of logical operators and the relational operators allows one to build up complex logical expressions, e.g. if ( ( x > 5 ) && ( x < 10 ) ) { cout << " x is between 5 and 10 " << endl; } Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 19

20 The Nature of truth in C++ The Nature of Truth in C++ In C++ an expression which evaluates to 0 is equivalent to logically FALSE whilst ANY other value is logically TRUE. Thus in the expression: if (1) cout << " This is silly " << endl; Will always execute the output statement. Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 20

21 if...else Often one wants to choose between execution of two or more blocks of code, depending on the results of logical expressions. To make a simple choice between two options one can use a if... else construct Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 21

22 if...else The general block form looks like: if(logical_expression ) {... } else {... } Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 22

23 if..else if More complex constructs can be built using nested if statements, or the logically equivalent if... else if construct: if(logical_expression ) {... } else if ( another_logical_expression ) {... } Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 23

24 if..else if..else Here is an example, similar to inttest.cpp, showing how to choose between the three cases: i=0, i<0 and i>0 : if ( i == 0) { cout << " i is 0 " << endl; } else if ( i < 0 ) { cout << " i is less than 0 " << endl; } else { cout << " i is greater than 0 " << endl; } Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 24

25 while loops Loops: The while Statement A very common programming task involves the evaluation of an expression several times with different variable values. Rather than tediously typing in many lines of repetitive code it is convenient to introduce the idea of a loop construct. The while( ) Statement is the simplest way of allowing repetition of some code...a loop in the program. Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 25

26 while loops Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 26

27 while loops It takes the general block form: while ( logical_expression ){... } Examples of some logical_expressions are: I < 10 i less than 10 J > 20 j greater than 20 k == j k equal to j Don't confuse the equality operator with assignment. Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 27

28 while loops While the logical_expression is True then the series of statements within the curlies are evaluated until the logical_expression is False The following code fragment calculates adds up the numbers between 1 and 10 int sum = 0; int k = 0; while ( k < 10 ) { k = k + 1; sum = sum + k; } Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 28

29 do...while loops do...while blocks The do...while statement is a variation of the while statement in which a block of statements is evaluated at least once before a logical test is made: Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 29

30 do...while loops The example above may be rewritten using do...while: int sum = 0, k = 0; do { k = k + 1; sum = sum + k; } while( k < 10 ); A complete version of this program is available Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 30

31 For loops More General Loops: The for Statement C++ has a very general and flexible way of forming loops using the for statement. It may be thought of a generalized while loop. Usually S1 and S3 are used to initialize and update a counter variable. Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 31

32 For loops The following code fragment demonstrates its use: (S1) (E1) (S3) for ( int i = 0; i < 20; i++) { }... This code defines a loop, the loop variable i, which takes in turn the values 0,1,2,...19, for each value of i the block of code enclosed by the curlies is evaluated. The block of code is therefore executed 20 times A general for construct always has 3 statements. These are Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 32

33 For loops N.B. A variable such as i, is said to be declared within the scope of the for loop ONLY. You can not access it from OUTSIDE the loop (though you can declare another variable with the same name). This is also true if you declare a variable within the for loop's curlies. The following sumsqs2.cpp calculates the sum-of-squares between 1 and 10: int sum=0; for(int i=1; i<=10; i++){ sum+=i*i; cout << " Sum of squares, i= " << i << " Sum = " << sum <<endl; } Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 33

34 For loops You should generally use an integer variable as the for loop counter to ensure the loop is evaluated the correct number of times. For example, because of possible rounding errors its difficult to be certain, how many times the following loop is evaluated: double delta=1.0/3.0; for(double x=2; x<=5; x+=delta ){... } Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 34

35 For loops To avoid such problems you should use an integer loop counter and derive the floating value from it. So the above bad example could be rewritten as so: or double delta=1.0/3.0; for(int i=0; i<=15; i++ ){ double x = i*delta;... } double delta=1.0/3.0; double x=2.0; for(int i=0; i<=15; i++ ){... x+=delta; } Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 35

36 Introduction to C++ Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 36

Add Subtract Multiply Divide

Add Subtract Multiply Divide ARITHMETIC OPERATORS if AND if/else AND while LOOP Order of Operation (Precedence Part 1) Copyright 2014 Dan McElroy Add Subtract Multiply Divide + Add - Subtract * Multiply / Divide = gives the quotient

More information

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017 Loops! Loops! Loops! Lecture 5 COP 3014 Fall 2017 September 25, 2017 Repetition Statements Repetition statements are called loops, and are used to repeat the same code mulitple times in succession. The

More information

C++ Programming Lecture 7 Control Structure I (Repetition) Part I

C++ Programming Lecture 7 Control Structure I (Repetition) Part I C++ Programming Lecture 7 Control Structure I (Repetition) Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department while Repetition Structure I Repetition structure Programmer

More information

CS106X Handout 03 Autumn 2012 September 24 th, 2012 Getting Started

CS106X Handout 03 Autumn 2012 September 24 th, 2012 Getting Started CS106X Handout 03 Autumn 2012 September 24 th, 2012 Getting Started Handout written by Julie Zelenski, Mehran Sahami, Robert Plummer, and Jerry Cain. After today s lecture, you should run home and read

More information

Introduction to C++ Dr M.S. Colclough, research fellows, pgtas

Introduction to C++ Dr M.S. Colclough, research fellows, pgtas Introduction to C++ Dr M.S. Colclough, research fellows, pgtas 5 weeks, 2 afternoons / week. Primarily a lab project. Approx. first 5 sessions start with lecture, followed by non assessed exercises in

More information

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol. 1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol. B. Outputs to the console a floating point number f1 in scientific format

More information

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program 1 By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program variables. Apply C++ syntax rules to declare variables, initialize

More information

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering Lecture 3 Winter 2011/12 Oct 25 Visual C++: Problems and Solutions New section on web page (scroll down)

More information

[CSE10200] Programming Basis ( 프로그래밍기초 ) Chapter 7. Seungkyu Lee. Assistant Professor, Dept. of Computer Engineering Kyung Hee University

[CSE10200] Programming Basis ( 프로그래밍기초 ) Chapter 7. Seungkyu Lee. Assistant Professor, Dept. of Computer Engineering Kyung Hee University [CSE10200] Programming Basis ( 프로그래밍기초 ) Chapter 7 Seungkyu Lee Assistant Professor, Dept. of Computer Engineering Kyung Hee University Input entities Keyboard, files Output entities Monitor, files Standard

More information

LECTURE 5 Control Structures Part 2

LECTURE 5 Control Structures Part 2 LECTURE 5 Control Structures Part 2 REPETITION STATEMENTS Repetition statements are called loops, and are used to repeat the same code multiple times in succession. The number of repetitions is based on

More information

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay C++ Basics Data Processing Course, I. Hrivnacova, IPN Orsay The First Program Comments Function main() Input and Output Namespaces Variables Fundamental Types Operators Control constructs 1 C++ Programming

More information

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan Lecture 08-1 Programming in C++ PART 1 By Assistant Professor Dr. Ali Kattan 1 The Conditional Operator The conditional operator is similar to the if..else statement but has a shorter format. This is useful

More information

In this chapter you will learn:

In this chapter you will learn: 1 In this chapter you will learn: Essentials of counter-controlled repetition. Use for, while and do while to execute statements in program repeatedly. Use nested control statements in your program. 2

More information

Numerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2

Numerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2 Numerical Computing in C and C++ Jamie Griffin Semester A 2017 Lecture 2 Visual Studio in QM PC rooms Microsoft Visual Studio Community 2015. Bancroft Building 1.15a; Queen s W207, EB7; Engineering W128.D.

More information

Engineering Problem Solving with C++, Etter/Ingber

Engineering Problem Solving with C++, Etter/Ingber Engineering Problem Solving with C++, Etter/Ingber Chapter 2 Simple C++ Programs C++, Second Edition, J. Ingber 1 Simple C++ Programs Program Structure Constants and Variables C++ Operators Standard Input

More information

Unit 3. Operators. School of Science and Technology INTRODUCTION

Unit 3. Operators. School of Science and Technology INTRODUCTION INTRODUCTION Operators Unit 3 In the previous units (unit 1 and 2) you have learned about the basics of computer programming, different data types, constants, keywords and basic structure of a C program.

More information

Lecture 3. Input and Output. Review from last week. Variable - place to store data in memory. identified by a name should be meaningful Has a type-

Lecture 3. Input and Output. Review from last week. Variable - place to store data in memory. identified by a name should be meaningful Has a type- Lecture 3 Input and Output Review from last week Variable - place to store data in memory identified by a name should be meaningful Has a type- int double char bool Has a value may be garbage change value

More information

Chapter 2. C++ Basics

Chapter 2. C++ Basics Chapter 2 C++ Basics Overview 2.1 Variables and Assignments 2.2 Input and Output 2.3 Data Types and Expressions 2.4 Simple Flow of Control 2.5 Program Style Slide 2-2 2.1 Variables and Assignments Variables

More information

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program Objectives Chapter 2: Basic Elements of C++ In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

More information

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3 Programming - 1 Computer Science Department 011COMP-3 لغة البرمجة 1 011 عال- 3 لطالب كلية الحاسب اآللي ونظم المعلومات 1 1.1 Machine Language A computer programming language which has binary instructions

More information

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Chapter 2: Basic Elements of C++ Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

More information

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction Chapter 2: Basic Elements of C++ C++ Programming: From Problem Analysis to Program Design, Fifth Edition 1 Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers

More information

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved. Chapter 2 C++ Basics 1 Overview 2.1 Variables and Assignments 2.2 Input and Output 2.3 Data Types and Expressions 2.4 Simple Flow of Control 2.5 Program Style Slide 2-3 2.1 Variables and Assignments 2

More information

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible) Name Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code necessary C++ statements to perform the action described not a complete program Program a complete C++ program

More information

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible) Name Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code necessary C++ statements to perform the action described not a complete program Program a complete C++ program

More information

Scientific Computing

Scientific Computing Scientific Computing Martin Lotz School of Mathematics The University of Manchester Lecture 1, September 22, 2014 Outline Course Overview Programming Basics The C++ Programming Language Outline Course

More information

Introduction to Programming

Introduction to Programming Introduction to Programming session 6 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Spring 2011 These slides are created using Deitel s slides Sharif University of Technology Outlines

More information

*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN

*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN PowerPoint Slides adapted from *Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* Copyright 2012 Pearson Education Inc. COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN

More information

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS313D: ADVANCED PROGRAMMING LANGUAGE CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science department Lecture 2 : C# Language Basics Lecture Contents 2 The C# language First program Variables and constants Input/output Expressions and casting

More information

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3 More Flow of Control Overview 3.1 Using Boolean Expressions 3.2 Multiway Branches 3.3 More about C++ Loop Statements 3.4 Designing Loops Slide 3-3 Flow Of Control Flow of control refers to the

More information

Chapter 2. Outline. Simple C++ Programs

Chapter 2. Outline. Simple C++ Programs Chapter 2 Simple C++ Programs Outline Objectives 1. Building C++ Solutions with IDEs: Dev-cpp, Xcode 2. C++ Program Structure 3. Constant and Variables 4. C++ Operators 5. Standard Input and Output 6.

More information

Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language. Dr. Amal Khalifa. Lecture Contents:

Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language. Dr. Amal Khalifa. Lecture Contents: Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language Dr. Amal Khalifa Lecture Contents: Introduction to C++ Origins Object-Oriented Programming, Terms Libraries and Namespaces

More information

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5. Week 2: Console I/O and Operators Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.1) CS 1428 Fall 2014 Jill Seaman 1 2.14 Arithmetic Operators An operator is a symbol that tells the computer to perform specific

More information

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols. EEE-117 COMPUTER PROGRAMMING Basic Elements of C++ Objectives General Questions Become familiar with the basic components of a C++ program functions, special symbols, and identifiers Data types Arithmetic

More information

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved. Chapter 2 C++ Basics Overview 2.1 Variables and Assignments 2.2 Input and Output 2.3 Data Types and Expressions 2.4 Simple Flow of Control 2.5 Program Style 3 2.1 Variables and Assignments Variables and

More information

Computer Programming : C++

Computer Programming : C++ The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2003 Muath i.alnabris Computer Programming : C++ Experiment #1 Basics Contents Structure of a program

More information

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++ Repetition Contents 1 Repetition 1.1 Introduction 1.2 Three Types of Program Control Chapter 5 Introduction 1.3 Two Types of Repetition 1.4 Three Structures for Looping in C++ 1.5 The while Control Structure

More information

Chapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments

Chapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments Chapter 2 C++ Basics Overview 2.1 Variables and Assignments 2.2 Input and Output 2.3 Data Types and Expressions 2.4 Simple Flow of Control 2.5 Program Style Copyright 2011 Pearson Addison-Wesley. All rights

More information

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh C++ PROGRAMMING For Industrial And Electrical Engineering Instructor: Ruba A. Salamh CHAPTER TWO: Fundamental Data Types Chapter Goals In this chapter, you will learn how to work with numbers and text,

More information

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements Review: Exam 1 9/20/06 CS150 Introduction to Computer Science 1 1 Your First C++ Program 1 //*********************************************************** 2 // File name: hello.cpp 3 // Author: Shereen Khoja

More information

Units 0 to 4 Groovy: Introduction upto Arrays Revision Guide

Units 0 to 4 Groovy: Introduction upto Arrays Revision Guide Units 0 to 4 Groovy: Introduction upto Arrays Revision Guide Second Year Edition Name: Tutorial Group: Groovy can be obtained freely by going to http://groovy-lang.org/download Page 1 of 8 Variables Variables

More information

Increment and the While. Class 15

Increment and the While. Class 15 Increment and the While Class 15 Increment and Decrement Operators Increment and Decrement Increase or decrease a value by one, respectively. the most common operation in all of programming is to increment

More information

Objectives. In this chapter, you will:

Objectives. In this chapter, you will: Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates arithmetic expressions Learn about

More information

CSI33 Data Structures

CSI33 Data Structures Outline Department of Mathematics and Computer Science Bronx Community College October 24, 2018 Outline Outline 1 Chapter 8: A C++ Introduction For Python Programmers Expressions and Operator Precedence

More information

UNIT- 3 Introduction to C++

UNIT- 3 Introduction to C++ UNIT- 3 Introduction to C++ C++ Character Sets: Letters A-Z, a-z Digits 0-9 Special Symbols Space + - * / ^ \ ( ) [ ] =!= . $, ; : %! &? _ # = @ White Spaces Blank spaces, horizontal tab, carriage

More information

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement Last Time Let s all Repeat Together 10/3/05 CS150 Introduction to Computer Science 1 1 We covered o Counter and sentinel controlled loops o Formatting output Today we will o Type casting o Top-down, stepwise

More information

REPETITION CONTROL STRUCTURE LOGO

REPETITION CONTROL STRUCTURE LOGO CSC 128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING REPETITION CONTROL STRUCTURE 1 Contents 1 Introduction 2 for loop 3 while loop 4 do while loop 2 Introduction It is used when a statement or a block of

More information

PIC 10A Flow control. Ernest Ryu UCLA Mathematics

PIC 10A Flow control. Ernest Ryu UCLA Mathematics PIC 10A Flow control Ernest Ryu UCLA Mathematics If statement An if statement conditionally executes a block of code. # include < iostream > using namespace std ; int main () { double d1; cin >> d1; if

More information

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. Looping. ++ is the increment operator.

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. Looping. ++ is the increment operator. Chapter 5: Looping 5.1 The Increment and Decrement Operators Copyright 2009 Pearson Education, Inc. Copyright Publishing as Pearson 2009 Addison-Wesley Pearson Education, Inc. Publishing as Pearson Addison-Wesley

More information

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords. Chapter 1 File Extensions: Source code (cpp), Object code (obj), and Executable code (exe). Preprocessor processes directives and produces modified source Compiler takes modified source and produces object

More information

Introduction to Programming EC-105. Lecture 2

Introduction to Programming EC-105. Lecture 2 Introduction to Programming EC-105 Lecture 2 Input and Output A data stream is a sequence of data - Typically in the form of characters or numbers An input stream is data for the program to use - Typically

More information

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable Basic C++ Overview C++ is a version of the older C programming language. This is a language that is used for a wide variety of applications and which has a mature base of compilers and libraries. C++ is

More information

CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad

CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad Outline 1. C++ Iterative Constructs 2. The for Repetition Structure 3. Examples Using the for Structure 4. The while Repetition Structure

More information

Lecture 5. Review from last week. Selection Statements. cin and cout directives escape sequences

Lecture 5. Review from last week. Selection Statements. cin and cout directives escape sequences Lecture 5 Selection Statements Review from last week cin and cout directives escape sequences member functions formatting flags manipulators cout.width(20); cout.setf(ios::fixed); setwidth(20); 1 What

More information

The cin Object. cout << "Enter the length and the width of the rectangle? "; cin >> length >> width;

The cin Object. cout << Enter the length and the width of the rectangle? ; cin >> length >> width; The cin Object Short for console input. It is used to read data typed at the keyboard. Must include the iostream library. When this instruction is executed, it waits for the user to type, it reads the

More information

Introduction to Programming

Introduction to Programming Introduction to Programming session 5 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2010 These slides are created using Deitel s slides Sahrif University of Technology Outlines

More information

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons Computer Programming Basic Control Flow - Loops Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons Objectives To learn about the three types of loops: while for do To avoid infinite

More information

CS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma.

CS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma. REVIEW cout Statement The cout statement invokes an output stream, which is a sequence of characters to be displayed to the screen. cout

More information

Programming Fundamentals

Programming Fundamentals Programming Fundamentals Programming Fundamentals Instructor : Zuhair Qadir Lecture # 11 30th-November-2013 1 Programming Fundamentals Programming Fundamentals Lecture # 11 2 Switch Control substitute

More information

Fundamentals of Programming CS-110. Lecture 2

Fundamentals of Programming CS-110. Lecture 2 Fundamentals of Programming CS-110 Lecture 2 Last Lab // Example program #include using namespace std; int main() { cout

More information

2 nd Week Lecture Notes

2 nd Week Lecture Notes 2 nd Week Lecture Notes Scope of variables All the variables that we intend to use in a program must have been declared with its type specifier in an earlier point in the code, like we did in the previous

More information

CS Exam 2 Study Suggestions

CS Exam 2 Study Suggestions CS 131 - Fall 2009 p. 1 last modified: 11-10-09 CS 131 - * Remember: anything covered in lecture, in lab, or on a homework, is FAIR GAME. * You are responsible for all of the material covered through Week

More information

Another Simple Program: Adding Two Integers

Another Simple Program: Adding Two Integers Another Simple Program: Adding Two Integers Another Simple Program: Adding Two Integers This program uses the input stream object std::cin and the stream extrac>, to obtain two integers

More information

Chapter 1 Introduction to Computers and C++ Programming

Chapter 1 Introduction to Computers and C++ Programming Chapter 1 Introduction to Computers and C++ Programming 1 Outline 1.1 Introduction 1.2 What is a Computer? 1.3 Computer Organization 1.7 History of C and C++ 1.14 Basics of a Typical C++ Environment 1.20

More information

C++ Quick Guide. Advertisements

C++ Quick Guide. Advertisements C++ Quick Guide Advertisements Previous Page Next Page C++ is a statically typed, compiled, general purpose, case sensitive, free form programming language that supports procedural, object oriented, and

More information

Homework #3 CS2255 Fall 2012

Homework #3 CS2255 Fall 2012 Homework #3 CS2255 Fall 2012 MULTIPLE CHOICE 1. The, also known as the address operator, returns the memory address of a variable. a. asterisk ( * ) b. ampersand ( & ) c. percent sign (%) d. exclamation

More information

CSCI 123 Introduction to Programming Concepts in C++

CSCI 123 Introduction to Programming Concepts in C++ CSCI 123 Introduction to Programming Concepts in C++ Brad Rippe C++ Basics C++ layout Include directive #include using namespace std; int main() { } statement1; statement; return 0; Every program

More information

CSCE 110 PROGRAMMING FUNDAMENTALS

CSCE 110 PROGRAMMING FUNDAMENTALS CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 2. Overview of C++ Prof. Amr Goneid, AUC 1 Overview of C++ Prof. Amr Goneid, AUC 2 Overview of C++ Historical C++ Basics Some Library

More information

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program Chapter 2: Introduction to C++ 2.1 Parts of a C++ Program Copyright 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Parts of a C++ Program Parts of a C++ Program // sample C++ program

More information

Unit 7. 'while' Loops

Unit 7. 'while' Loops 1 Unit 7 'while' Loops 2 Control Structures We need ways of making decisions in our program To repeat code until we want it to stop To only execute certain code if a condition is true To execute one segment

More information

Variables and Operators 2/20/01 Lecture #

Variables and Operators 2/20/01 Lecture # Variables and Operators 2/20/01 Lecture #6 16.070 Variables, their characteristics and their uses Operators, their characteristics and their uses Fesq, 2/20/01 1 16.070 Variables Variables enable you to

More information

Chapter 3: Operators, Expressions and Type Conversion

Chapter 3: Operators, Expressions and Type Conversion 101 Chapter 3 Operators, Expressions and Type Conversion Chapter 3: Operators, Expressions and Type Conversion Objectives To use basic arithmetic operators. To use increment and decrement operators. To

More information

Creating a C++ Program

Creating a C++ Program Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer. 1 Creating a C++ Program created using an

More information

Chapter 2 - Control Structures

Chapter 2 - Control Structures Chapter 2 - Control Structures 1 Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures 2.5 if Selection Structure 2.6 if/else Selection Structure 2.7 while Repetition Structure

More information

Name SECTION: 12:45 2:20. True or False (12 Points)

Name SECTION: 12:45 2:20. True or False (12 Points) Name SECION: 12:45 2:20 rue or False (12 Points) 1. (12 pts) Circle for true and F for false: F a) Local identifiers have name precedence over global identifiers of the same name. F b) Local variables

More information

IS 0020 Program Design and Software Tools

IS 0020 Program Design and Software Tools 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Spring 2005 Lecture 1 Jan 6, 2005 Course Information 2 Lecture: James B D Joshi Tuesdays/Thursdays: 1:00-2:15 PM Office Hours:

More information

LECTURE 02 INTRODUCTION TO C++

LECTURE 02 INTRODUCTION TO C++ PowerPoint Slides adapted from *Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* Copyright 2012 Pearson Education Inc. COMPUTER PROGRAMMING LECTURE 02 INTRODUCTION

More information

C++ Programming: From Problem Analysis to Program Design, Third Edition

C++ Programming: From Problem Analysis to Program Design, Third Edition C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 2: Basic Elements of C++ Objectives (continued) Become familiar with the use of increment and decrement operators Examine

More information

From Pseudcode Algorithms directly to C++ programs

From Pseudcode Algorithms directly to C++ programs From Pseudcode Algorithms directly to C++ programs (Chapter 7) Part 1: Mapping Pseudo-code style to C++ style input, output, simple computation, lists, while loops, if statements a bit of grammar Part

More information

Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator

Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator 1 A Closer Look at the / Operator Used for performing numeric calculations C++ has unary, binary, and ternary s: unary (1 operand) - binary ( operands) 13-7 ternary (3 operands) exp1? exp : exp3 / (division)

More information

CS2141 Software Development using C/C++ Stream I/O

CS2141 Software Development using C/C++ Stream I/O CS2141 Software Development using C/C++ Stream I/O iostream Two libraries can be used for input and output: stdio and iostream The iostream library is newer and better: It is object oriented It can make

More information

Programming. C++ Basics

Programming. C++ Basics Programming C++ Basics Introduction to C++ C is a programming language developed in the 1970s with the UNIX operating system C programs are efficient and portable across different hardware platforms C++

More information

Lecture 3 Tao Wang 1

Lecture 3 Tao Wang 1 Lecture 3 Tao Wang 1 Objectives In this chapter, you will learn about: Arithmetic operations Variables and declaration statements Program input using the cin object Common programming errors C++ for Engineers

More information

These are notes for the third lecture; if statements and loops.

These are notes for the third lecture; if statements and loops. These are notes for the third lecture; if statements and loops. 1 Yeah, this is going to be the second slide in a lot of lectures. 2 - Dominant language for desktop application development - Most modern

More information

Data Types and the while Statement

Data Types and the while Statement Session 2 Student Name Other Identification Data Types and the while Statement The goals of this laboratory session are to: 1. Introduce three of the primitive data types in C++. 2. Discuss some of the

More information

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University Overview of C, Part 2 CSE 130: Introduction to Programming in C Stony Brook University Integer Arithmetic in C Addition, subtraction, and multiplication work as you would expect Division (/) returns the

More information

Getting started with C++ (Part 2)

Getting started with C++ (Part 2) Getting started with C++ (Part 2) CS427: Elements of Software Engineering Lecture 2.2 11am, 16 Jan 2012 CS427 Getting started with C++ (Part 2) 1/22 Outline 1 Recall from last week... 2 Recall: Output

More information

Your first C++ program

Your first C++ program Your first C++ program #include using namespace std; int main () cout

More information

Simple Java Programming Constructs 4

Simple Java Programming Constructs 4 Simple Java Programming Constructs 4 Course Map In this module you will learn the basic Java programming constructs, the if and while statements. Introduction Computer Principles and Components Software

More information

WEEK 4 OPERATORS, EXPRESSIONS AND STATEMENTS

WEEK 4 OPERATORS, EXPRESSIONS AND STATEMENTS WEEK 4 OPERATORS, EXPRESSIONS AND STATEMENTS OPERATORS Review: Data values can appear as literals or be stored in variables/constants Data values can be returned by method calls Operators: special symbols

More information

Chapter 2: Introduction to C++

Chapter 2: Introduction to C++ Chapter 2: Introduction to C++ Copyright 2010 Pearson Education, Inc. Copyright Publishing as 2010 Pearson Pearson Addison-Wesley Education, Inc. Publishing as Pearson Addison-Wesley 2.1 Parts of a C++

More information

CS2141 Software Development using C/C++ C++ Basics

CS2141 Software Development using C/C++ C++ Basics CS2141 Software Development using C/C++ C++ Basics Integers Basic Types Can be short, long, or just plain int C++ does not define the size of them other than short

More information

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen Chapter 2: Introduction to C++ 2.1 Parts of a C++ Program Copyright 2009 Pearson Education, Inc. Copyright 2009 Publishing Pearson as Pearson Education, Addison-Wesley Inc. Publishing as Pearson Addison-Wesley

More information

Chapter 6. I/O Streams as an Introduction to Objects and Classes. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Chapter 6. I/O Streams as an Introduction to Objects and Classes. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 I/O Streams as an Introduction to Objects and Classes Overview 6.1 Streams and Basic File I/O 6.2 Tools for Stream I/O 6.3 Character I/O Slide 6-3 6.1 Streams and Basic File I/O I/O Streams I/O

More information

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups: JAVA OPERATORS GENERAL Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups: Arithmetic Operators Relational Operators Bitwise Operators

More information

Declaration and Memory

Declaration and Memory Declaration and Memory With the declaration int width; the compiler will set aside a 4-byte (32-bit) block of memory (see right) The compiler has a symbol table, which will have an entry such as Identifier

More information

Lecture 5: Making Decisions

Lecture 5: Making Decisions Lecture 5: Making Decisions Topics 5.1 Relational Operators 5.2 The if Statement 5.3 The if/else Statement 5.4 Logical Operators 5.5 Examples 4-2 3 5.1 Relational Operators Used to compare numbers to determine

More information

BITG 1233: Introduction to C++

BITG 1233: Introduction to C++ BITG 1233: Introduction to C++ 1 Learning Outcomes At the end of this lecture, you should be able to: Identify basic structure of C++ program (pg 3) Describe the concepts of : Character set. (pg 11) Token

More information

COMP Primitive and Class Types. Yi Hong May 14, 2015

COMP Primitive and Class Types. Yi Hong May 14, 2015 COMP 110-001 Primitive and Class Types Yi Hong May 14, 2015 Review What are the two major parts of an object? What is the relationship between class and object? Design a simple class for Student How to

More information

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char Week 1 Operators, Data Types & I/O Gaddis: Chapters 1, 2, 3 CS 5301 Fall 2016 Jill Seaman Programming A program is a set of instructions that the computer follows to perform a task It must be translated

More information