Size: px
Start display at page:

Transcription

1 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 % Mod = gives the remainder for integer division 1

2 Division Integer Division 6 r 3 4 ) 27 ` Floating Point Division ) 27 int Q1 = 27 / 4; // quotient = 6 int R1 = 27 % 4; // remainder = 3 double Q2 = 27.0 / 4.0; // quotient = 6.75 Data Type Mismatch int Q1 = 27 / 4; // int quotient = 6 int R1 = 27 % 4; // int remainder = 3 double Q2 = 27.0 / 4.0; // double quotient = 6.75 Watch out for an integer division being assigned to a double. You lose the digits past the decimal. double Q2 = 27 / 4; // double quotient = 6.0 // because 27/4 is an // integer division equal 6 2

3 Data Type Mismatch Watch out for an integer division being assigned to a double. You lose the digits past the decimal. double Q2 = 27 / 4; // double quotient = 6.0 // because 27/4 is an // integer division equal 6 If either the 27 is a double (27.0) or the 4 is a double (4.0), or both, the result would be 67.5 double Q2 = 27 / 4.0; // double quotient = 6.75 Data Type Mismatch Compilers will promote an integer to a double, but will either issue a Loss of Precision Warning or an Error when converting from a double to an integer. If only a warning is issued, the program will still compile and run, but you may not get the results you were expecting. int Q1 = 27.0 / 4; // double quotient = 6.75 is // converted to an integer 6 by // the assignment = statement 3

4 Standard Calculator Left to Right 2 * * 5 2*3=6 6+4=10 10*5=50 Order of Operation Scientific Calculator Multiply & Divide before Add & Subtract 2 * * Order of Operation Multiply and Divide before Add and Subtract 2 * * Precedence ( ) Parentheses ( ) * / % Multiply, Divide, Mod + Add, Subtract = Assignment You can change the order of computation by using parentheses int x = 2 * ( ) * 5 ; // answer is 70 // 2 * ( ) * 5 2 * 7 * 5 70 int x = ( 2 * ) * 5 ; // answer is 50 // ( 2 * ) * 5 (6+4) * 5 20 *

5 Arithmetic Assignment Operators int a = 10; int b = 20; a = a + b; // now a = 30 int a = 10; int b = 20; a += b; // now a = 30 a += b; is the same as a = a + b; a = b; is the same as a = a b; a *= b; is the same as a = a * b; Arithmetic Assignment Operators a += b; is the same as a = a + b; a = b; is the same as a = a b; a *= b; is the same as a = a * b; a /= b; is the same as a = a / b; a %= b; is the same as a = a % b; Other arithmetic and logical operators work the same. 5

6 Auto Increment Auto Decrement The C language introduced the auto-increment and auto-decrement. The auto-increment adds one to a numeric variable. For example: int a = 5; a++; // after the statement, a equals 6 int b = 10; b--; // after the statement, b equals 9 Pre-Increment Post-Increment The increment (or decrement) can be applied either at the beginning of the statement or after everything else in the statement has executed int a = 5; int x = ++a + 10; // a = 6, x = 16; int b = 5; int x = b ; // b = 6, x = 15 The variable b does not get incremented to a 6 until the rest of the statement has executed. The value of b was still a 5 when 10 was added to get a 15. The 15 was stored into x before b was incremented. 6

7 The if Statement if / else A simple if statement if (score >= 90) cout << "Great work!" << endl; A simple if/else statement if (score >= 90) cout << "Great work!" << endl; else cout << "Work harder." << endl; 7

8 if / else A simple if statement if (score >= 90) cout << "Great work!" << endl; A simple if/else statement if (score >= 90) cout << "Great work!" << endl; else cout << "Work harder." << endl; if / else if / else if / else if (score >= 90) cout << "Grade is A" << endl; else if (score >= 80) cout << "Grade is B" << endl; else if (score >= 70) cout << "Grade is C" << endl; else if (score >= 60) cout << "Grade is D" << endl; else cout << "Grade is F" << endl; 8

9 if / else if / else if / else if (score >= 90) cout << "Grade is A" << endl; else if (score >= 80) cout << "Grade is B" << endl; else if (score >= 70) cout << "Grade is B" << endl; else if (score >= 60) cout << "Grade is D" << endl; else cout << "Grade is F" << endl; if / else if / else if / else if (score >= 90) cout << "Grade is A" << endl; else if (score >= 80) cout << "Grade is B" << endl; else if (score >= 70) cout << "Grade is B" << endl; else if (score >= 60) cout << "Grade is D" << endl; else cout << "Grade is F" << endl; 9

10 Compound if A compound if statement can have a block of code attached to the if statement by using the open and close curly braces. if (score >= 90) cout << "Great work!" << endl; cout << "Your grade is an A" << endl; The curly braces can even be used if there is only one statement attached to the if. Compound if A compound if statement can have a block of code attached to the if statement by using the open and close curly braces. if (score >= 90) cout << "Great work!" << endl; cout << "Your grade is an A" << endl; The curly braces can even be used if there is only one statement attached to the if. 10

11 Compound if / else if (score >= 70) cout << "Good work." << endl; cout << "You passed." << endl; else cout << "Try harder." << endl; cout << "You can do it." << endl; Programming Style The opening curly brace can be on the same line as the if if (time >= 2) cout << "Takes too long." << endl; cout << "Get some help." << endl; or if (time >= 2) cout << "Takes too long." << endl; cout << "Get some help." << endl; 11

12 Simple Loops int i = 0; while ( i < 5 ) cout << i << endl; cout << "Done << endl; Done int i = 0; do cout << i << endl; while ( i < 5 ) ; cout << "Done << endl; Done Repetition Without Loops // Input and add three numbers int total = 0; int number; cout << "Enter a number: "; cin >> number; total += number; cout << "Enter a number: "; cin >> number; total += number; cout << "Enter a number: "; cin >> number; total += number; 12

13 Repetition With at Loop // Input and add three numbers int total = 0; int number; int = 0; while (i < 3) cout << "Enter a number: "; total += number; Simple Loops int i = 0; while ( i < 5 ) cout << i << endl; cout << "Done << endl; Done int i = 0; do cout << i << endl; while ( i < 5 ) ; cout << "Done << endl; Done The test for looping is done at the top of the loop. Code may not be executed if the loop condition fails. The test for looping is done at the bottom of the loop. Code will be executed at least one time. 13

14 Simple Loops int i = 10; while ( i < 5 ) cout << i << endl; cout << "Done << endl; Done int i = 10; do cout << i << endl; while ( i < 5 ) ; cout << "Done << endl; 10 Done The test for looping is done at the top of the loop. Code may not be executed if the loop condition fails. The test for looping is done at the bottom of the loop. Code will be executed at least one time. The Sum of Input Values int value = 0; // value from keyboard int total = 0; // initialize the total int i = 0; // initialize loop counter while (i < 5) // get 5 values cout << "Enter a number: "; // prompt cin >> value; // input from keyboard total += value; // total = total + value; // increment loop counter 14

15 Be Careful of the Semi-colon int value = 0; // value from keyboard int total = 0; // initialize the total int i = 0; // initialize loop counter while (i < 5) // get 5 values cout << "Enter a number: "; // prompt cin >> value; // input from keyboard total += value; // total = total + value; // increment loop counter Sample Execution int value = 0; // value from keyboard int total = 0; // initialize the total int i = 0; // initialize loop counter while (i < 5) // get 5 values cout << "Enter a number: "; // prompt cin >> value; // input from keyboard total += value; // total = total + value; // increment loop counter i = times in the loop Done 15

16 Sample Execution int value = 0; // value from keyboard int total = 0; // initialize the total int i = 0; // initialize loop counter while (i < 5) // get 5 values cout << "Enter a number: "; // prompt cin >> value; // input from keyboard total += value; // total = total + value; // increment loop counter i = 0 (at start of loop) value = 12 (from the user) total = 12 i++ = 1 (at end of loop) i<5 (y/n) y Loop? (y/n) y Sample Execution int value = 0; // value from keyboard int total = 0; // initialize the total int i = 0; // initialize loop counter while (i < 5) // get 5 values cout << "Enter a number: "; // prompt cin >> value; // input from keyboard total += value; // total = total + value; // increment loop counter i = 0 1 value = total = i++ = 1 2 i<5 (y/n) y y Loop? (y/n) y y 16

17 Sample Execution int value = 0; // value from keyboard int total = 0; // initialize the total int i = 0; // initialize loop counter while (i < 5) // get 5 values cout << "Enter a number: "; // prompt cin >> value; // input from keyboard total += value; // total = total + value; // increment loop counter i = value = total = i++ = i<5 (y/n) y y y Loop? (y/n) y y y Sample Execution int value = 0; // value from keyboard int total = 0; // initialize the total int i = 0; // initialize loop counter while (i < 5) // get 5 values cout << "Enter a number: "; // prompt cin >> value; // input from keyboard total += value; // total = total + value; // increment loop counter i = value = total = i++ = i<5 (y/n) y y y y Loop? (y/n) y y y y 17

18 Sample Execution int value = 0; // value from keyboard int total = 0; // initialize the total int i = 0; // initialize loop counter while (i < 5) // get 5 values cout << "Enter a number: "; // prompt cin >> value; // input from keyboard total += value; // total = total + value; // increment loop counter i = value = total = i++ = i<5 (y/n) y y y y n Loop? (y/n) y y y y n Sample Execution int value = 0; // value from keyboard int total = 0; // initialize the total int i = 0; // initialize loop counter while (i < 5) // get 5 values cout << "Enter a number: "; // prompt cin >> value; // input from keyboard total += value; // total = total + value; // increment loop counter i = value = total = i++ = i<5 (y/n) y y y y n Loop? (y/n) y y y y n 18

19 Counters Start from Zero It is most common to start counting from zero because the first index (position) in an array starts with zero. = 0 This may be confusing for most Americans who count the ground floor of a building as the first floor, but the rest of the world counts the first floor as the floor above ground. The End The End The do while loop will be covered later 19

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

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

### 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

### C/C++ Programming Lecture 7 Name:

1. The increment (++) and decrement (--) operators increase or decrease a variable s value by one, respectively. They are great if all you want to do is increment (or decrement) a variable: i++;. HOWEVER,

### 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

### 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

### 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

### Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1 Topics 1. Expressions 2. Operator precedence 3. Shorthand operators 4. Data/Type Conversion 1/15/19 CSE 1321 MODULE 2 2 Expressions

### 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

### Operators. Lecture 3 COP 3014 Spring January 16, 2018

Operators Lecture 3 COP 3014 Spring 2018 January 16, 2018 Operators Special built-in symbols that have functionality, and work on operands operand an input to an operator Arity - how many operands an operator

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

Introduction to C++ Lecture Set 2 Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 1 More Arithmetic Operators More Arithmetic Operators In the first session the basic arithmetic operators were introduced.

### 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

### Chapter 4: Control Structures I (Selection)

Chapter 4: Control Structures I (Selection) 1 Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate logical (Boolean) expressions Discover

### Looping. Arizona State University 1

Looping CSE100 Principles of Programming with C++, Fall 2018 (based off Chapter 5 slides by Pearson) Ryan Dougherty Arizona State University http://www.public.asu.edu/~redoughe/ Arizona State University

### 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

### 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

### 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

### Operators. Java operators are classified into three categories:

Operators Operators are symbols that perform arithmetic and logical operations on operands and provide a meaningful result. Operands are data values (variables or constants) which are involved in operations.

### 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.

### 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

### 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

### 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

### Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Class 2: Variables and Memory Variables A variable is a value that is stored in memory It can be numeric or a character C++ needs to be told what type it is before it can store it in memory It also needs

### 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

### LECTURE 3 C++ Basics Part 2

LECTURE 3 C++ Basics Part 2 OVERVIEW Operators Type Conversions OPERATORS Operators are special built-in symbols that have functionality, and work on operands. Operators are actually functions that use

### Chapter 03: Computer Arithmetic. Lesson 09: Arithmetic using floating point numbers

Chapter 03: Computer Arithmetic Lesson 09: Arithmetic using floating point numbers Objective To understand arithmetic operations in case of floating point numbers 2 Multiplication of Floating Point Numbers

### 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

### 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

### LAB 4.1 Relational Operators and the if Statement

LAB 4.1 Relational Operators and the if Statement // This program tests whether or not an initialized value of num2 // is equal to a value of num1 input by the user. int main( ) int num1, // num1 is not

### CAMBRIDGE SCHOOL, NOIDA ASSIGNMENT 1, TOPIC: C++ PROGRAMMING CLASS VIII, COMPUTER SCIENCE

CAMBRIDGE SCHOOL, NOIDA ASSIGNMENT 1, TOPIC: C++ PROGRAMMING CLASS VIII, COMPUTER SCIENCE a) Mention any 4 characteristic of the object car. Ans name, colour, model number, engine state, power b) What

### 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

### 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

### 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

### Chapter 3 Structure of a C Program

Chapter 3 Structure of a C Program Objectives To be able to list and describe the six expression categories To understand the rules of precedence and associativity in evaluating expressions To understand

### Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

Chapter 4: Control Structures I (Selection) In this chapter, you will: Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate logical (Boolean)

### Week 4 Lecture 1. Expressions and Functions

Lecture 1 Expressions and Functions Expressions A representation of a value Expressions have a type Expressions have a value Examples 1 + 2: type int; value 3 1.2 + 3: type float; value 4.2 2 More expression

### C: How to Program. Week /Mar/05

1 C: How to Program Week 2 2007/Mar/05 Chapter 2 - Introduction to C Programming 2 Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers

### 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

### 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

### 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

### 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

### Object Oriented Programming with Java

Object Oriented Programming with Java What is Object Oriented Programming? Object Oriented Programming consists of creating outline structures that are easily reused over and over again. There are four

### 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.

### 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

### 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)

### 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++

### 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

### 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

### A First Program - Greeting.cpp

C++ Basics A First Program - Greeting.cpp Preprocessor directives Function named main() indicates start of program // Program: Display greetings #include using namespace std; int main() { cout

### 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

### Chapter 2 - Introduction to C Programming

Chapter 2 - Introduction to C Programming 2 Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers 2.4 Memory Concepts 2.5 Arithmetic

### ! 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

### Introduction to Programming using C++

Introduction to Programming using C++ Lecture One: Getting Started Carl Gwilliam gwilliam@hep.ph.liv.ac.uk http://hep.ph.liv.ac.uk/~gwilliam/cppcourse Course Prerequisites What you should already know

### CHAPTER 3 Expressions, Functions, Output

CHAPTER 3 Expressions, Functions, Output More Data Types: Integral Number Types short, long, int (all represent integer values with no fractional part). Computer Representation of integer numbers - Number

### Operators & Expressions

Operators & Expressions Operator An operator is a symbol used to indicate a specific operation on variables in a program. Example : symbol + is an add operator that adds two data items called operands.

### 1. C++ Overview. C++ Program Structure. Data Types. Assignment Statements. Input/Output Operations. Arithmetic Expressions.

1. C++ Overview 1. C++ Overview C++ Program Structure. Data Types. Assignment Statements. Input/Output Operations. Arithmetic Expressions. Interactive Mode, Batch Mode and Data Files. Common Programming

### Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

Week 2 Branching & Looping Gaddis: Chapters 4 & 5 CS 5301 Spring 2018 Jill Seaman 1 Relational Operators l relational operators (result is bool): == Equal to (do not use =)!= Not equal to > Greater than

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

### REVIEW. The C++ Programming Language. CS 151 Review #2

REVIEW The C++ Programming Language Computer programming courses generally concentrate on program design that can be applied to any number of programming languages on the market. It is imperative, however,

### Structured Program Development

Structured Program Development Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Outline Introduction The selection statement if if.else switch The

### 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

### Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

Objectives Chapter 4: Control Structures I (Selection) In this chapter, you will: Learn about control structures Examine relational and logical operators Explore how to form and evaluate logical (Boolean)

### 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

### 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

### Fundamental of Programming (C)

Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 3 Constants, Variables, Data Types, And Operations Department of Computer Engineering

### Compilation and Execution Simplifying Fractions. Loops If Statements. Variables Operations Using Functions Errors

First Program Compilation and Execution Simplifying Fractions Loops If Statements Variables Operations Using Functions Errors C++ programs consist of a series of instructions written in using the C++ syntax

### CS2255 HOMEWORK #1 Fall 2012

CS55 HOMEWORK #1 Fall 01 1.What is assigned to the variable a given the statement below with the following assumptions: x = 10, y = 7, and z, a, and b are all int variables. a = x >= y; a. 10 b. 7 c. The

### Theory of control structures

Theory of control structures Paper written by Bohm and Jacopini in 1966 proposed that all programs can be written using 3 types of control structures. Theory of control structures sequential structures

### 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

### Computer Science II Lecture 1 Introduction and Background

Computer Science II Lecture 1 Introduction and Background Discussion of Syllabus Instructor, TAs, office hours Course web site, http://www.cs.rpi.edu/courses/fall04/cs2, will be up soon Course emphasis,

### In Fig. 3.5 and Fig. 3.7, we include some completely blank lines in the pseudocode for readability. programs into their various phases.

Formulating Algorithms with Top-Down, Stepwise Refinement Case Study 2: Sentinel-Controlled Repetition In Fig. 3.5 and Fig. 3.7, we include some completely blank lines in the pseudocode for readability.

### Basics of Java Programming

Basics of Java Programming Lecture 2 COP 3252 Summer 2017 May 16, 2017 Components of a Java Program statements - A statement is some action or sequence of actions, given as a command in code. A statement

### C++ PROGRAMMING SKILLS Part 2 Programming Structures

C++ PROGRAMMING SKILLS Part 2 Programming Structures If structure While structure Do While structure Comments, Increment & Decrement operators For statement Break & Continue statements Switch structure

### 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

### On a 64-bit CPU. Size/Range vary by CPU model and Word size.

On a 64-bit CPU. Size/Range vary by CPU model and Word size. unsigned short x; //range 0 to 65553 signed short x; //range ± 32767 short x; //assumed signed There are (usually) no unsigned floats or doubles.

### 7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

KEY CONCEPTS COMP 10 EXPLORING COMPUTER SCIENCE Lecture 2 Variables, Types, and Programs Problem Definition of task to be performed (by a computer) Algorithm A particular sequence of steps that will solve

### 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

### 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

### Lesson 3: Arithmetic & Casting. Pic 10A Ricardo Salazar

Lesson 3: Arithmetic & Casting Pic 10A Ricardo Salazar (2.4) Constants Sometimes we want a 'variable' that does not vary!? (OK that does not make sense... but how about a 'house' whose guest is always

### 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

### Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Basic memory model Using functions Writing functions Basics Prototypes Parameters Return types Functions and memory Names and namespaces When a program runs it requires main memory (RAM) space for Program

### The C++ Language. Arizona State University 1

The C++ Language CSE100 Principles of Programming with C++ (based off Chapter 2 slides by Pearson) Ryan Dougherty Arizona State University http://www.public.asu.edu/~redoughe/ Arizona State University

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

### 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

### CSCI Wednesdays: 1:25-2:15 Keller Thursdays: 4:00-4:50 Akerman 211

C++ Basics CSCI 1113 Wednesdays: 1:25-2:15 Keller 1-250 Thursdays: 4:00-4:50 Akerman 211 Peer-assisted Learning sessions - PAL Weekly practice with course concepts Work with peers to reinforce understanding

### Why Is Repetition Needed?

Why Is Repetition Needed? Repetition allows efficient use of variables. It lets you process many values using a small number of variables. For example, to add five numbers: Inefficient way: Declare a variable

### QUIZ: What value is stored in a after this

QUIZ: What value is stored in a after this statement is executed? Why? a = 23/7; QUIZ evaluates to 16. Lesson 4 Statements, Expressions, Operators Statement = complete instruction that directs the computer

### More Complex Versions of the if Statement. Class 13

More Complex Versions of the if Statement Class 13 if-else the if-else statement is an expansion of the plain if statement as with the if statement, an expression is evaluated to give a Boolean result

### Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Announcements Lab Friday, 1-2:30 and 3-4:30 in 26-152 Boot your laptop and start Forte, if you brought your laptop Create an empty file called Lecture4 and create an empty main() method in a class: 1.00

### 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

### Lecture 3 Operators MIT AITI

Lecture 3 Operators MIT AITI - 2004 What are Operators? Operators are special symbols used for mathematical functions assignment statements logical comparisons Examples: 3 + 5 // uses + operator 14 + 5

### C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 4: Control Structures I (Selection)

C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection) Objectives In this chapter, you will: Learn about control structures Examine relational

### 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

### Unit 3, Lesson 2 Data Types, Arithmetic,Variables, Input, Constants, & Library Functions. Mr. Dave Clausen La Cañada High School

Unit 3, Lesson 2 Data Types, Arithmetic,Variables, Input, Constants, & Library Functions Mr. Dave Clausen La Cañada High School Vocabulary Variable- A variable holds data that can change while the program

### VARIABLES & ASSIGNMENTS

Fall 2018 CS150 - Intro to CS I 1 VARIABLES & ASSIGNMENTS Sections 2.1, 2.2, 2.3, 2.4 Fall 2018 CS150 - Intro to CS I 2 Variables Named storage location for holding data named piece of memory You need

### Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Introduction Operators are the symbols which operates on value or a variable. It tells the compiler to perform certain mathematical or logical manipulations. Can be of following categories: Unary requires

### Lab 2: Structured Program Development in C

Lab 2: Structured Program Development in C (Part A: Your first C programs - integers, arithmetic, decision making, Part B: basic problem-solving techniques, formulating algorithms) Learning Objectives