VARIABLES AND DATA TYPES Chapter2:part1 1. Objectives: By the end of this section you should: Understand what the variables are and why they are used.

Slides:



Advertisements
Similar presentations
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
Advertisements

Data types and variables
Overview of C++ Chapter 2 in both books programs from books keycode for lab: get Program 1 from web test files.
CS150 Introduction to Computer Science 1
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
Basic Elements of C++ Chapter 2.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
Chapter 2 Data Types, Declarations, and Displays.
Objectives You should be able to describe: Data Types
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Variables, Data Types and Constants Yared Semu Addis Ababa Institute of Technology Mar 31, 2012.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
C++ Programming: Basic Elements of C++.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
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.
CS346 Javascript -3 Module 3 JavaScript Variables.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
C++ for Engineers and Scientists Second Edition
1 09/03/04CS150 Introduction to Computer Science 1 What Data Do We Have.
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
1 CSC 1111 Introduction to Computing using C++ C++ Basics (Part 1)
LECTEURE # 5 : STRUCTURED PROGRAMMING VARIABLES, INPUT, MEMORY المتغيرات, المدخلات, الذاكرة By Mr. Ali Edan.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
1 A more complex example Write a program that sums a sequence of integers and displays the result. Assume that the first integer read specifies the number.
A Sample Program #include using namespace std; int main(void) { cout
Constants, Data Types and Variables
Chapter 3 Using Variables, Constants, Formatting Mrs. UlshaferSept
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Fundamentals 2.
Chapter 1.2 Introduction to C++ Programming
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
BASIC ELEMENTS OF A COMPUTER PROGRAM
Documentation Need to have documentation in all programs
Computing Fundamentals
Basic Elements of C++.
Data Types, Identifiers, and Expressions
Basic Elements of C++ Chapter 2.
IDENTIFIERS CSC 111.
Chapter 2 Elementary Programming
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Variables Kingdom of Saudi Arabia
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
Chapter 2: Java Fundamentals
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Introduction to C++ Programming
Chapter # 2 Part 2 Programs And data
Chapter 2: Java Fundamentals
Chapter 2: Introduction to C++.
C++ Programming Lecture 3 C++ Basics – Part I
Presentation transcript:

VARIABLES AND DATA TYPES Chapter2:part1 1

Objectives: 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 2

Variables Variable: Location on computer’s memory to store data then use and change its value in a program. Name(Identifier) You can use upper and lowercase letters, digits from 0 to 9, and underscore(_). The first character must be letter or underscore ( _ ). Don’t contains any blank,,&,!,%,^,*,…etc. C++ is case sensitive –uppercase and lowercase letters are different, so a1 and A1 are different identifier. Not a keyword( int, float, double char, void, return main) Meaningful Don’t give the same name to two different variables. Type: Programmer defined Built-in 3

Exercises Ex1: Time, TIME, time Are three correct names for three different identifier? Ex2:Which of the following is a correct identifier: 4 Correct identifier_num Incorrect, the symbol –cannot be used in identifier DD-X Incorrect, an identifier cannot begin with digits12abc Correct identifier Name55 Incorrect, keywordreturn Correct identifierA1 Incorrect, the symbol /cannot be used in identifier Name1/4

What is a data type? 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. 5

C++ Built-in Data Types Called fundamental types or primitives types: Numerical (integer and floating point) Character Logical (Boolean) 6

int Data Type Integer variables represent integer numbers like 1, 30,000 and -45. Integer variables do not store real numbers. 7

Integer Variables Example #include int main ( ) { int var1;//define var1 int var2;//define var2 var1 = 20;//assign value to var1 var2 = var1 + 10;//assign value to var2 std::cout<<“Result =”; std:: cout<<var2<< endl; //displaying the sum of var1+10 return 0; } 8

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

Example #include int main ( ) { float rad, area; float PI =3.14; std::cout << “Enter radius of circle”; std::cin >> rad; area = PI * rad * rad; std::cout <<“Area is” << area << std::endl; return 0; } 10

char Data Type 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. 11

Examble include int main ( ) { char charvar1 = ‘A’; //define char variable char charvar2 = ‘\t’; std::cout << charvar1; // display character std::cout << charvar2; charvar1 = ‘B’; //reassigning charvar1 to B std::cout << charvar1; return (0); } 12

bool Data type 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; 13

Variable declaration 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. 14 Data type VarName; int num1; int num2; int num3; int num1,num2,num3;

Assigning initial value to variable Three way to do that: 1- in the declaration of the variable. Ex: char w=‘A’; int sum=0; 2- Using assign statement. Ex: i=1; w=‘A’; 3- Accepting the value from user using cin function. Ex: cin>>number; 15

16

Memory Concepts 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? 17

Scope Of Variable 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 18

Scope Of Variable 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 (::) 19

Examples: int main() { int i; char a; } 20 int i; int main() { char a; } i: Local variablei: Global variable

Unary Scope Resolution Operator 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 21

Example 22 #include 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; }