CS242 COMPUTER PROGRAMMING

Similar documents
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

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

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

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

Objectives. In this chapter, you will:

CSc Introduction to Computing

Fundamentals of Programming CS-110. Lecture 2

Chapter 1 Introduction to Computers and C++ Programming

Introduction to Programming EC-105. Lecture 2

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

The C++ Language. Arizona State University 1

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

Introduction to Programming

IT 1033: Fundamentals of Programming Data types & variables

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

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

Another Simple Program: Adding Two Integers

Introduction to C++ Systems Programming

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++

Computer Programming : C++

Creating a C++ Program

BASIC ELEMENTS OF A COMPUTER PROGRAM

Data Types & Variables

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

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 program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

Chapter 15 - C++ As A "Better C"

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

BTE2313. Chapter 2: Introduction to C++ Programming

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

C++ As A "Better C" Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.

UNIT- 3 Introduction to C++

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

Getting started with C++ (Part 2)

by Pearson Education, Inc. All Rights Reserved. 2

Differentiate Between Keywords and Identifiers

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

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

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

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

Lecture 3 Tao Wang 1

LECTURE 02 INTRODUCTION TO C++

Exercise: Using Numbers

! A literal represents a constant value used in a. ! Numbers: 0, 34, , -1.8e12, etc. ! Characters: 'A', 'z', '!', '5', etc.

WARM UP LESSONS BARE BASICS

Chapter 1 INTRODUCTION

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

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.

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

3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space.

Chapter-8 DATA TYPES. Introduction. Variable:

Chapter 2: Introduction to C++

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

Lecture 4. 1 Statements: 2 Getting Started with C++: LESSON FOUR

6.096 Introduction to C++ January (IAP) 2009

Introduction to C# Applications

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

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

Functions and Recursion

Chapter 2 Basic Elements of C++

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

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

Introduction to C++ (Extensions to C)

2 nd Week Lecture Notes

Chapter 2: Overview of C++

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

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

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

Lecture 2 Tao Wang 1

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

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

Fundamental of Programming (C)

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

Full file at

Numbers. John Perry. Spring 2017

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

FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each):

5. Assuming gooddata is a Boolean variable, the following two tests are logically equivalent. if (gooddata == false) if (!

Starting Out with C++: Early Objects, 9 th ed. (Gaddis, Walters & Muganda) Chapter 2 Introduction to C++ Chapter 2 Test 1 Key

Chapter 2. Outline. Simple C++ Programs

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

Programming. C++ Basics

Chapter 2 C++ Fundamentals

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

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

C++ Basics. Brian A. Malloy. References Data Expressions Control Structures Functions. Slide 1 of 24. Go Back. Full Screen. Quit.

Connecting with Computer Science, 2e. Chapter 15 Programming II

Integer Data Types. Data Type. Data Types. int, short int, long int

Computer Programming C++ Classes and Objects 6 th Lecture

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

A First Program - Greeting.cpp

Topic 2: C++ Programming fundamentals

Exercise: Inventing Language

Fundamentals of Programming

Cours de C++ Introduction

Transcription:

CS242 COMPUTER PROGRAMMING I.Safa a Alawneh Variables

Outline 2 Data Type C++ Built-in Data Types o o o o bool Data Type char Data Type int Data Type Floating-Point Data Types Variable Declaration Initializing Variable Using cin Scope of Variables Local Scope Global Scope o Unary Scope Resolution Operator Variable and Memory Constants

Variable 3 Location on computer s memory to store data then use and change its value in a program. Every variable has name, type, size and value: 1. Name (identifier) Series of letters, digits, underscores. Not a keyword ( for example : int, float, double, char, void, return, main). Start with a letter. Case sensitive. Meaningful. 2. Type Programmer-defined. Built-in.

Data Type 4 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: 1. The range of values that can be stored. 2. Operations that can be performed on variables of that type.

C++ Built-in Data Types 5 Called fundamental types or primitives types: numeric (integer and floating point), character, logical (Boolean).

bool Data Type 6 Has two values, true and false. Manipulate logical (Boolean) expressions. true and false are called logical values. bool, true, and false are reserved words. Example: bool iseven = false ; bool keyfound = true ;

char Data Type 7 Used for characters letters, digits, and special symbols. Each character is enclosed in single quotes. Examples: 'A', 'a', '0', '*', '+', '$', '&' A blank space is a character and is written ' ' with a space left between the single quotes. Example: char grade ; char star = * ;

int Data Type 8 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;

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

Declaring Variables 10 All variables must be declared anywhere in program with a name and data type before they used. Syntax rule: begin with a data type then variable name. datatype varname; Variables of the same type can be declared in: Multiple lines One line separated by commas int num1; int num2; int num3; int num1, num2, num3;

Initializing Variable 11 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.

Using cin 12 Stream extraction operator >> Value to left (left operand) inserted into right operand. Waits for user to input value then press Enter key. Example std::cin >> num1; Inserts the standard input from keyboard into 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.

Using cin (cont.) 13 Write a program that asks the user to enter 2 numbers (number1) and (number2), then adds the numbers and prints the result.

1 2 // Addition program that display the sum of two numbers. 3 #include <iostream> // allow program to perform input and output 4 5 // function main begins program execution 6 int main() 7 { 8 // variable declaration 9 int number1; // first integer to add 10 int number2; // second integer to add 11 int sum; // sum of number1 and number2 12 13 std::cout << "Enter first integer: \n"; // prompt user for data 14 std::cin >> number1; // read first integer from user to number1 15 16 std::cout << "Enter second integer: \n"; // prompt user for data 17 std::cin >> number2; // read second integer from user to number2 18 19 sum = number1 + number2; // add the numbers; stor result in sum 20 21 std::cout << "Sum is " << sum << std::endl; // display sum; end line 22 23 return 0; // indicate that program ended successfully 24 } // end function main Declare integer variables. Use stream extraction operator with standard input stream to obtain user input. Calculations can be performed in output statements: alternative for lines 19 and 21: std::cout << "Sum is " << number1 + number2 << std::endl; Enter first integer 45 Enter second integer 72 Sum is 117 Stream manipulator std::endl outputs a newline, then flushes output buffer. 14

Variable Scope 15 Portion of the program where the variable can be used. Scope can be: Local Global 1. Local Variables Defined within a module. Can be seen and used only by module itself. Store temporally in memory. Erased when the module terminates. int main() { int i; char a; return 0; }

Variable Scope (cont.) 16 2. Global Variables Defined outside any module. Used and seen by all modules. int i; int main(){ char a; return 0;} NOTES Variable name can be duplicated within and outside a modules. Differentiate between them by using unary scope resolution operator (::). int a; int main(){ int a; return 0;}

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

Unary Scope Resolution Operator (cont.) 18 1 // using the unary scope resolution operator. 2 #include <iostream> 3 using std::cout; 4 using std::endl; 5 int number = 7; // global variable named number 6 int main() 7 { 8 double number = 10.5; // local variable named number 9 // display values of local and global variables 10 cout << "local double value of number = " << number 11 << "\nglobal int value of number = " << ::number << endl; 12 system("pause"); 13 return 0; // indicate successful termination 14 } // end main Local double value of number = 10.5 Global int value of number = 7

Variables and Memory 19 Variables names correspond to location in the computer s memory(ram). Every variable has type, name, size and value, for example: char letter = A ; Type name size value char letter 1byte A Placing new value into variable (memory location), overwrites old value called destructive. Reading value of variable in memory called nondestructive.

Variables and Memory (cont.) 20 std::cin >> number1; Assume user entered 45 number1 45 std::cin >> number2; Assume user entered 72 number1 45 number2 72 sum = number1 + number2; number1 45 number2 72 sum 117

Constants 21 Like variables: data storage locations. Unlike variables: Values never changed during program execution. Any attempt to change a constant creates a compilation error. Declared in two ways and follow identifier naming rules: With const keyword. const char Gender = F ; With #define keyword. #define studentsperclass 15

Exercise - 1 22 1. Write a program that declares two constant A and B. 2. Initialize A =1 and B=2.2 3. Declare an int named C and float named D. 4. Initialize C =1 and D=2.2 5. Write statements to print C and D to screen.