VARIABLES DATA TYPES AND INPUT STATEMENT IN C

VARIABLES, DATA TYPES, AND INPUT STATEMENT IN C++

What is a Variable? It is a symbol that represent a storage location in the computer’s memory. What is a Value? It is the information stored in that memory location.

Variable DATA Value In memory To input any data you should define a variable. The syntax for declaring one variable or multiple variables is data. Type identifier ; data. Type identifier, . . . ;

Data Types • Data Type: A set of values together with a set of operations is called a data type. • C++ data types fall into three categories § Simple Data Type. § Structured Data Type. § Pointers.

Simple Data Type C++ simple data can be classified into three categories 1. Integral, which is a data type that deals with integers, or numbers without a decimal part. 2. Floating-point, which is a data type that deals with decimal numbers. 3. Enumeration type, which is a user-defined data type.

Integral data types

1. int Data Type -6728, -67, 0, 78, 36782, +763, . . . • Declaration Syntax for int Data Type int identifier ; • The memory allocated for the int, unsigned int, long and unsigned long data types are 4 bytes. • Positive integers do not have to have a + sign in front of them. No commas are used within an integer. • • -2147483648 Range: Example #1 int num; long num; unsigned int num; To +2147483647

2. float Data Types • Scientific notation 43872918 = 4. 3872918 *10^7. 0000265 = 2. 65 * 10^(-5) 47. 9832 = 4. 7983 * 10^1 {10 to the power of seven}, {10 to the power of minus five}, {10 to the power of one} • There are two notation to represent real numbers in C++: – Decimal notation 35. 5 – Exponential notation 3. 55 E 1


• Declaration syntax for float Data Type float identifier ; • The memory allocated for the float data type is 4 bytes , and for the double data type is 8 bytes. • float Range: To 3. 4 E+38 -3. 4 E+38 • double Range: -1. 7 E+308 Example #2 float conversion; double pay. Rate; To 1. 7 E+308

3. Identifiers • A C++ identifier consists of letters, digits, and the under score character (_), and must begin with a letter or underscore. • Other symbols and spaces are not allowed to be used in naming identifires. • Do not use keywords. What is keywords? ? • C++ is case sensitive—uppercase and lower case letters are different. SALARY is different than salary

Example# 3 The following are legal identifiers in C++. first conversion Pay_Rate counter 1

Example#4 float amount. Due; int counter; int x, y; In C++, all identifiers must be declared before they can be used. If we refer to an identifier without declaring it, the compiler will generate an error message indicating that the identifier is not declared. x=8+3; int x; x=8+3;
![4. Variables Initialization The syntax for a variable initialization is: float identifier [=initial value]; 4. Variables Initialization The syntax for a variable initialization is: float identifier [=initial value];](http://slidetodoc.com/presentation_image_h/a732760232e7a5234c6528a21864fe5d/image-14.jpg)
4. Variables Initialization The syntax for a variable initialization is: float identifier [=initial value]; For example int x = 10 ; Optional : the value inside x int x ; the computer will allocate default value Find place in the memory with size 4 byte As integer type Identifier : The name of this place

5. Storing Data into Variables In C++ there are two ways for placing data into a variable 1. Using C++’s Assignment Statement * The assignment statement takes the form variable = expression; * The expression is evaluated and its value is assigned to the variable. int x = 10; Or int x; Or x=10; * In C++, = is called the assignment operator. 2. Using Input (Read) Statement int x; x=3+5;

Example#5 I J int I, J; 543 345 Default values I = 4; J = 8; I = 7; J = 5; cout<<I<<‘ ‘<<J; 75 I 4 J 345 4 8 7 5

6. Declaration and Initializing Variables · Variables can be initialized when they are declared int first=13, second=10; float x=12. 6; Equivalently, we can write the following C++ statements. int first, second; float x; first = 13; second = 10; x = 12. 6;

7. Printing variables Example#6 int a, b, c, d; a = 65 ; b = 78 ; cout<<"a"<<endl; cout<<a<<endl; cout<<b<<endl; cout<<c<<'n'; cout<<d<<endl; Output a 65 78 6749684 4203005

8. Types of statements • The body of the main is enclosed between { } and has two types of statements. • Declaration statements. • Executable statements.

A. Declaration Statements int a, b, c; float x, y; · Variables (or identifies) can be declared anywhere in the program, but they must be declared before they can be used. B. Executable Statements Example a = 4; //assignment statement b = a+1; //equation statement cout<<a<<endl<<b<<endl; //output statement

9. Form and Style Consider the following ways of declaring variables. int feet, inch; float x, y; or int a, b; float x, y; int a, b, float x, y; or int feet; int inch; float x; float y;

Blank spaces int a, b, c; int a, b, c; The blanks between the identifiers in the second statement are meaningless. In the statement, inta, b, c; Error no blank between the t and a changes the reserved word int and the identifier a into a new identifier inta.

10. Naming Identifiers float a = 2. 54; //conversion constant float x; //variable to hold centimeters float y; //variable to hold inches x = y * a; Consider the following float conversion = 2. 54; float centimeters; float inches; centimeters = inches * conversion;

Example # 7 • Find the 4 syntax errors. int x; int a; b; int y Float z; y = w + x; //Line 1 //Line 2 //Line 3 //Line 4 //Line 5

Input (Read) Statement

Input (Read) Statement Syntax of cin together with >>: cin>>variable; cin>>variable 1>>variable 2. . . ; § Suppose miles is a variable of the type float. § The statement cin>>miles;

Example void main() { int num; cin>>num; cout<<"bye"; getch(); } waiting for an input 14 bye input 14 then press Enter then bye displayed 1. the screen is empty waiting for an input. 2. The word “bye” is not displayed until the user press enter. so the compiler executes the next statement.

· By using more than one variable in cin, more than one value can be read at the same time. Suppose feet and inch are variables of the type int. A statement like cin>>feet>>inch; gets two integers (entered from the keyboard and separated by space) and places them in the memory location feet and inch, respectively. as input: 34 (leave space) 54 (press enter) 34 (press enter) 54 (press enter) feet inch 34 54 34 waiting 54

Reading more than one variable · You can read both pay. Rate and hours. Worked via a single cin statement by using the following code: cin>>pay. Rate>>hours. Worked; . OR cin>>pay. Rate; cin>>hours. Worked; · When scanning for the next input, >> skips all whitespaces. · Whitespace characters consist of blanks and certain nonprintable characters, such as tabs and the newline character.

Whether the input is only one space 15. 50 48. 30 or 15. 50 48. 30 tab or 15. 50 48. 30 newline (enter) The input statement: cin>>pay. Rate>>hours. Worked; would store 15. 50 in pay. Rate and 48. 30 in hours. Worked.

Prompt Lines Ask user to input the required data by displaying an suitable message. cout<<"Please enter a number between 1 and 10 and" <<" press the return key"<<endl; cin>>num; When these two statements execute in the order given, first the cout statement causes the following line of text to appear on the screen: Please enter a number between 1 and 10 and press the return key After seeing this line, users know that they must enter a number and press the return key.

Example /*Program to read two numbers */ #include <iostream. h> #include <conio. h> void main() { int num 1, num 2; cout << “Enter two numbers” << “ separated by space: “ ; cin>>num 1>>num 2; cout<< “nt“ <<num 1<<“t”<<num 2; getch(); }

Consider the statement cin>>a; where a is a variable of some simple data type. float

Example (1) int a, b; float z; Statement 1. cin>>a; Input Value Stored in Memory 48 a=48 2. cin>>a; held input 3. cin>>z; 46. 35 a=46, . 35 is for later 74. 35 z=74. 35 4. cin>>z; 39 z=39. 0

5. cin>>z>>a; 6. cin>>a>>b; 65. 78 38 z=65. 78, a=38 4 60 a=4, b=60 7. cin>>a>>z; 57 8. cin>>z>>a; 57 26. 9 9. cin>>a>>z; 10. cin>>a>>b>>z; b=34, the 26. 9 a=57, z=26. 9 z=57. 0, a=26 57 26. 9 a=57, z=26. 9 11 34 a=11, Computer waits for next number

11. cin>>a>>z; is input 12. cin>>a>>z; 46 32. 4 68 35. 5 9. 5 a=46, z=32. 4, 68 held for later a = 35, z = 0. 5

Example (2) int one, two; float z; one two ? ? z ? 1. one = 4; 4 ? ? 2. cin>>two; //8 4 8 ? 3. cin>>z; //4. 5 4 8 4. 5 4. one = two; 8 8 4. 5

What about an attempt to read invalid data? What would happen if you tried to input a letter into an int variable? • If the input data did not match the corresponding variables, the program would run into problems. • Trying to read a letter into an int or float variable would result in an input failure.

Consider the following statements int a, b, c; float x; the statement cin>>a>>b; the input is W 54 a input failure b input failure would result in an input failure because you are trying to input the character 'W' into the int variable a.
- Slides: 39