VARIABLES & ASSIGNMENTS

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

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

CHAPTER 3 BASIC INSTRUCTION OF C++

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Chapter 2: Introduction to C++

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

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

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

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.

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

LECTURE 02 INTRODUCTION TO C++

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

Lecture 3 Tao Wang 1

Chapter 1 Introduction to Computers and C++ Programming

Fundamentals of Programming CS-110. Lecture 2

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Engineering Problem Solving with C++, Etter/Ingber

Introduction to Programming

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

BITG 1233: Introduction to C++

A First Program - Greeting.cpp

Introduction to Programming EC-105. Lecture 2

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

The C++ Language. Arizona State University 1

Introduction. Arizona State University 1

Chapter 2 Basic Elements of C++

Chapter 2 Working with Data Types and Operators

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

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs.

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

Computer Programming : C++

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

Creating a C++ Program

CSCE 206: Structured Programming in C++

CSCE 206: Structured Programming in C++

CSc Introduction to Computing

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

UNIT- 3 Introduction to C++

Chapter 2: Overview of C++

Operators & Expressions

Full file at C How to Program, 6/e Multiple Choice Test Bank

Introduction to C++ Programming. Adhi Harmoko S, M.Komp

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

Introduction to C++ Programming Pearson Education, Inc. All rights reserved.

Chapter 2. Outline. Simple C++ Programs

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

Lecture 2 Tao Wang 1

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

Chapter 2: Basic Elements of C++

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

Data types Expressions Variables Assignment. COMP1400 Week 2

Homework #3 CS2255 Fall 2012

CS2255 HOMEWORK #1 Fall 2012

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

pointers + memory double x; string a; int x; main overhead int y; main overhead

Programming for Engineers Introduction to C

IT 374 C# and Applications/ IT695 C# Data Structures

Computational Physics Operating systems

CSc Introduc/on to Compu/ng. Lecture 8 Edgardo Molina Fall 2011 City College of New York

Basics of Java Programming

Another Simple Program: Adding Two Integers

CS112 Lecture: Working with Numbers

DHA Suffa University CS 103 Object Oriented Programming Fall 2015 Lab #01: Introduction to C++

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

Lab # 02. Basic Elements of C++ _ Part1

DaMPL. Language Reference Manual. Henrique Grando

Unit 3. Constants and Expressions

Expressions and Casting

Chapter 2 C++ Fundamentals

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

Chapter 2. C++ Basics

Programming in C++ 5. Integral data types

6.096 Introduction to C++ January (IAP) 2009

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

Introduction to the C++ Programming Language

CS242 COMPUTER PROGRAMMING

Exercise 1.1 Hello world

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

Basic Source Character Set for C++ Language:

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

Add Subtract Multiply Divide

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

CS112 Lecture: Variables, Expressions, Computation, Constants, Numeric Input-Output

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Unit 7. 'while' Loops

MATVEC: MATRIX-VECTOR COMPUTATION LANGUAGE REFERENCE MANUAL. John C. Murphy jcm2105 Programming Languages and Translators Professor Stephen Edwards

Full file at

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

Fundamentals of Programming. Lecture 3: Introduction to C Programming

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

Programming Fundamentals. With C++ Variable Declaration, Evaluation and Assignment 1

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

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

2 nd Week Lecture Notes

Assignment 1 Expressions Data Types Formatted Printing Variables Scanning CSC 123 Fall 2018 Answer Sheet Short Answers

Programming. C++ Basics

Chapter 2. Lexical Elements & Operators

Transcription:

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 to determine what variables you need in your program what data do we need to handle? 1

Fall 2018 CS150 - Intro to CS I 3 Variable Definition int number; Tells the compiler the variable s type (int) the variable s name (number) int is short for integer Variable definitions end with a semicolon Every variable must be defined Fall 2018 CS150 - Intro to CS I 4 C++ Assignment Statement number = 5; = is an operator that copies the value from the right into a variable on the left The item to the left of the = operator must be a variable 2

Fall 2018 CS150 - Intro to CS I 5 = is not equals!! You cannot write 5 = number; Fall 2018 CS150 - Intro to CS I 6 Variables in a program 1 // This program has a variable called number 2 #include <iostream> 3 #include <string> 4 using namespace std; 5 6 int main() // what is the output of this program? 7 { 8 int number; 9 10 number = 5; 11 cout << Number is " << number << endl; 12 13 number = 7; 14 cout << "Now number is " << number << endl; 15 16 return EXIT_SUCCESS; 17 } 3

Fall 2018 CS150 - Intro to CS I 7 Variables on the left and right of assignments What is the output? #include <iostream> using namespace std; int main() { int x = 8; x = x + 1; x = x * 5; x = 7; cout << "x is: " << x << endl; } return 0; Fall 2018 CS150 - Intro to CS I 8 Variable Definition We now know that: What is an identifier?! 4

Fall 2018 CS150 - Intro to CS I 9 Identifiers (Variables) Programmer-defined names that represent some element of a program C++ limits on variable names: 1. Identifiers must begin with a letter or an underscore 2. Identifiers must consist of letters, numbers and underscore, nothing else 3. Identifiers cannot be a keyword Fall 2018 CS150 - Intro to CS I 10 Identifiers (Variables) Identifiers are case sensitive int totalcost; int TotalCost; Use meaningful variable names int width; int w; 5

Fall 2018 CS150 - Intro to CS I 11 Lower Camel Case You must use lower camel case for all of your CS 150 assignments In lower camel case, words are joined together, where the first letter of the entire is lowercase, but subsequent first letters are uppercase thisisanexample ThisIsNotAnExample Fall 2018 CS150 - Intro to CS I 12 Arithmetic Expressions Arithmetic expressions manipulate numeric data The main arithmetic operators are + addition - subtraction * multiplication / division 6

Fall 2018 CS150 - Intro to CS I 13 Mathematical Expressions Complex mathematical expressions are created by using multiple operators and grouping symbols expression: programming statement that has value sum = 21 + 3; expression number = 3; In these two examples, we assign the value of an expression to a variable Fall 2018 CS150 - Intro to CS I 14 Arithmetic Operators Operators allow us to manipulate data Unary: operator operand Binary: left operand operator right operand 7

Fall 2018 CS150 - Intro to CS I 15 Operator Precedence result = 4 * 2-3; result =? result = 12 + 6 / 3; result =? Rules on how to evaluate an arithmetic expression 1. arithmetic expressions are evaluated left to right 2. do them in order of precedence 3. grouping symbols ( ) Fall 2018 CS150 - Intro to CS I 16 Operator Precedence Operator Description () Items within parentheses are evaluated first unary - - used for negation (unary minus) is next * / % Next to be evaluated are *, /, and %, having equal precedence. + - Finally come + and - with equal precedence. left-to-right If more than one operator of equal precedence could be evaluated, evaluation occurs left to right. 8

Fall 2018 CS150 - Intro to CS I 17 Practice int x = 3; double y = 2.5; cout << 5 + 2 * 3; If you are unsure, you can always type up and run the code in Visual Studio cout << (10 / 2 y); cout << 3 + 12 * 2-3; cout << 4 + 17 / 3.0 + 9; cout << (6 - y) * 9 / x * 4-9; Fall 2018 CS150 - Intro to CS I 18 Problem Write a program that will compute the tip on a restaurant bill for a patron with a $42.80 meal charge. The tip must be 15% of the total bill. Read in the bill amount from the user, then display the meal cost, tip amount, and total bill on the screen. The program output should look like the following: Enter the cost of the meal: 42.80 The tip amount is: 6.42 The total cost is: 49.22 Press any key to continue... 9