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

Similar documents
CS242 COMPUTER PROGRAMMING

Lecture 3 Tao Wang 1

UNIT- 3 Introduction to C++

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

Introduction to Programming

Computer Programming : C++

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

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

Chapter 1 Introduction to Computers and C++ Programming

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

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

2 nd Week Lecture Notes

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

BITG 1233: Introduction to C++

Creating a C++ Program

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

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

Chapter 1 INTRODUCTION

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

Chapter 2. C++ Basics

The C++ Language. Arizona State University 1

Programming in C++ 5. Integral data types

Objectives. In this chapter, you will:

A First Program - Greeting.cpp

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

Introduction to Programming EC-105. Lecture 2

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

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

Another Simple Program: Adding Two Integers

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

Fundamentals of Programming CS-110. Lecture 2

VARIABLES & ASSIGNMENTS

Chapter 2: Introduction to C++

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

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

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

CSc Introduction to Computing

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.

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

CSCE 110 PROGRAMMING FUNDAMENTALS

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

6.096 Introduction to C++ January (IAP) 2009

Operators. Java operators are classified into three categories:

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

C: How to Program. Week /Mar/05

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

Chapter 2: Overview of C++

Basics of Programming

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.

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

CHAPTER 3 BASIC INSTRUCTION OF C++

Chapter 2 - Introduction to C Programming

Programming. C++ Basics

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Chapter 2 Basic Elements of C++

WARM UP LESSONS BARE BASICS

Chapter 2: Using Data

Unit 3. Operators. School of Science and Technology INTRODUCTION

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

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

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

Differentiate Between Keywords and Identifiers

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

download instant at Introduction to C++

BASIC ELEMENTS OF A COMPUTER PROGRAM

Chapter 2. Outline. Simple C++ Programs

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

LECTURE 02 INTRODUCTION TO C++

Unit-2 (Operators) ANAND KR.SRIVASTAVA

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++

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

Operators in java Operator operands.

Chapter 3: Operators, Expressions and Type Conversion

Lecture 3. More About C

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

Introduction to Programming

by Pearson Education, Inc. All Rights Reserved. 2

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

Unit 3. Constants and Expressions

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

Engineering Problem Solving with C++, Etter/Ingber

PART I. Part II Answer to all the questions 1. What is meant by a token? Name the token available in C++.

Tutorial-2a: First steps with C++ programming

Full file at

3. Java - Language Constructs I

Topic 2: C++ Programming fundamentals

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

Guide for The C Programming Language Chapter 1. Q1. Explain the structure of a C program Answer: Structure of the C program is shown below:

Reserved Words and Identifiers

Scientific Computing

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Add Subtract Multiply Divide

1. In C++, reserved words are the same as predefined identifiers. a. True

Fundamentals of Programming

More Programming Constructs -- Introduction

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

Lecture 2 Tao Wang 1

Transcription:

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 them. Understand memory allocation process. Apply C++ syntax rules to read user input using cin. Understand assignment statements Understand the scope of variables Differentiate between local and global variables Use C++ arithmetic and logical operators to create expressions 2

Write a program that displays the sum of two integers entered at the keyboard: Define the problem precisely. Write the psudocode that will solve the problem Use an Editor to create source code in C++. Use the compiler to Check that source code obeys the language rules. If no errors: Execute your program. 3

In order to define places in memory where the three values will be stored, we need to defined three variables for number1, number2 and sum. 4

Variable: Location on computer s memory to store data then use and change its value in a program. Name(Identifier) Series of letters, digits, underscores Not a keyword( int, float, double char, void, return main) Start with a letter Case sensitive Meaningful Type: Programmer defined Built-in 5

When we wish to store data in a C++ program, we have to tell the compiler which type of data we want to store. The data type will have characteristics such as: The range of values that can be stored. and the operations that can be performed on variables of that type. 6

Called fundamental types or primitives types: Numerical (integer and floating point) Character Logical (Boolean) 7

Type bool unsigned short int short int unsigned long int long int int char float double size 1 byte 2 bytes 2 bytes 4 bytes 4 bytes 2 bytes 1 byte 4 bytes 8 bytes 8

Has two values (true) and (false). Manipulate logical expressions. true and false are called logical values. bool, ture, and fasle are reserved words. For example: bool iseven = false; bool keyfound = true; 9

Used for characters: letters, digits, and special symbols. Each character is enclosed in single quotes. Some of the values belonging to char data type are: A, a, 0, *, +, $, &. A blank space is a character and is written, with a space left between the single quotes. 10

The integer type is used for storing whole numbers. We can use signed, unsigned or plain integer values as follows: signed int index = 4182; signed int temperature = -32; unsigned int count = 0; int height = 100; int balance = -67; 11

Floating point types can contain decimal numbers. Examples: 1.23, -.087. There are three sizes: float (single-precision) double (double-precision) and long double (extended-precision). Examples: float Temp= 37.623; double fahrenheit = 98.415; long double accountbalance = 1897.23; 12

Data type VarName; int num1; int num2; int num3; int num1,num2,num3; All variables must be declared anywhere in program with a name and data type before they used. Begin with a data type then variable name. Variables of the same type can be declared in Multiple lines One line separated by commas. 13

Variables can be initialized when declared: int first=13, second=10; char ch= ; double x=12.6, y=123.456; first and second are int variables with the values 13 and 10 respectively. ch is a char variable whose value is empty. x and y are double variables with 12.6 and 123.456 respectively. 14

Variable names such as : number1, number2 and sum correspond to locations in the computer s memory. Every variable has four parts: Type, name, size and value. Example: char letter= A ; Type? Name? Size? Value? 15

Namespace: std:: Specifies using a name that belong to namespace std Can be removed through use of using statement. Standard input stream object. std::cin Connected to keyboard Defined in input/output stream library <iostream> 16

Stream extraction operator >> Value to left (left operand) inserted into right operand. Waits for user to input value then press enter key Examples: cin>>num1; Inserts the standard input from keyboard into the variable num1. Prints message before cin statement to direct the user to take a specification called prompt. cin and cout facilitate interaction between user and program. 17

#include <stream> int main { cout << "If this text", cout >> " appears on your display, "; cout << " endl;" cout << 'you can pat yourself on ' << " the back!" << endl. return 0; ) In the above program, find the errors (if any) then, give the program output. 18

The scope of variable is the portion of the program where the variable can be used. Scope can be: Local Global Local variables: Defined within a module Can be seen and used only by the module itself Store temporally in memory Erased when he module terminates 19

Global variables: Defined outside any module. Used an seen by all modules Variable name can be duplicated within and outside a modules Differentiate between them by using unary scope resolution operator (::) 20

int main() { int i; char a; } int i; int main() { } char a; i: Local variable i: Global variable 21

Denoted as (::) Used to declare local and global variables have a same name. To avoid conflicts. Syntax: :: variable Example: y= ::x+3 Not needed if names are different 22

#include <iostream> using namespace std; int count = 100; int main() { int count = 10; int Second_count = 50; cout << "Local count = " << count << endl; cout << "Global count = " << ::count << endl; cout << "Local Second count = " << Second_count <<endl; return 0; 23

The assignment statement takes the form: Variable= expression; In C++ ( = ) is called the assignment operator. Has two operands (Binary operator) Expression is evaluated and its values is assigned to the variable on the left side. 24

Syntax: Variable =variable operator expression; Example: c=c+3; Variable operator= expression. Example: c+=3; 25

Assignment operator Sample expression Explanation Assigns Assume : int c=3, d=5,e=4,f=6,g=12 += c+=7 c=c+7 10 to c -= d-=4 d=d-4 1 to d *= e*=5 e=e*5 20 to e /= f/=3 f=f/3 2 to f %= g%=9 g=g%9 3 to g 26

Data connectors within expression or equation Concept related Operand: data that operator connects and processes Resultant: answer when operation is completed Operator types: Arithmetic: addition +, subtraction -, modulo division %, etc. Relational: equal to =, less than >, grater than <, etc Logical: NOT!, AND &&, OR 27

All of them are binary operators C++ Operation C++ arithmetic operators Algebraic expression C++ expression Addition + a+7 a+7 Subtraction - a-b a-b Multiplicatio n * ab a*b Division / a/b a/b Modulus % a mod b a%b 28

Arithmetic expressions appear in straight line form. Parentheses() are used to maintain priority of manipulation. 29

Operators in parentheses evaluated first Nested/embedded parentheses. ( a * ( b + c ) ) Operators in innermost pair firs. If nested parentheses-applied from left to right Multiplication division, modulus applied next Operators applied from left to right Addition, subtraction applied last Operators applied from left to right Examples: Algebra: z=pr % q + w / x - y C++: z=p * r % q + w / x - y; 6 1 2 4 3 5 30

The increase operator (++) and the decrease operator (--) increase or reduce by one the value stored in a variable. They are equivalent to +=1 and to -=1, respectively. 1. c++; 2. c+=1; 3. c=c+1; The three above expressions are all equivalent in its functionality: the three of them increase by one the value of c. 31

In the case that the increase operator is used as a prefix (++a) the value is increased before the result of the expression is evaluated Therefore the increased value is considered in the outer expression. In case that it is used as a suffix (a++) the value stored in a is increased after being evaluated Therefore the value stored before the increase operation is evaluated in the outer expression 32

// apply increment-decrement operators 1. B=3; A=++B; // A contains 4, B contains 4 2. B=3; A=B++; // A contains 3, B contains 4 33

All of them are binary operators Algebraic equality or relational operator C++ equality or relational operator Example of C++ condition Meaning of C++ condition Relational operator > > a>b A is greater than b < < a<b A is less than b >= a>=b A is greater than or equal to b <= a<=b A is less than or equal to b Equality Operator = = a==b A is equal to b 34

Have the same level of precedence Applied from left to right Used with conditions Return the value true or false 35

Used to combine multiple conditions && is the logical AND True if both conditions are true Example: age>=50 $$ gender== f is the logical OR True if either of conditions is true Example: age>=50 hasdiabetes == true 36

! (logical NOT, logical negation) Return true when its condition is false and vice versa Example:!(grade==maximumGrade) Alternative: grade!= maximumgrade 37

Operators () ++ -- ++ -- * / % Associativity Left to right Left to right Right to left Left to right Type parentheses Postfix incrementdecrement Prefix incrementdecrement Multiplicative - + Left to right Additive < <= > >= ==!= && Left to right Left to right Left to right Relational Equality Logical AND Left to right Logical OR 38

bool test1,test2,test3; int x=3,y=6,z=4; test1=x>y // false test2=!(x==y); //true test3=x<y && x<z; //true test3= test1 test2; //true test2=!test1; //true 39

// compound assignment operators #include <iostream> using namespace std; int main () { int a, b=3; a = b; a+=2; // equivalent to a=a+2 cout << a; return 0; } 40

Convert the following algebraic expression to an arithmetic C++ expressions: 41

Evaluate the following C++ expression: y= a * x * x + b * x + c; 42

43