Download presentation
Presentation is loading. Please wait.
Published byTimothy Campbell Modified over 9 years ago
1
Lecture 5 Computer programming -1-
2
Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout): Use to display data to screen. Example: cout<< age ;
3
Variables and Constants Variable : - is a location the memory in which you can store a value and from which you can retrieve that value. - Has a name and a type of data. - Must be defined before it can be used. - Example : int hours_worked;
4
2-4 Variable Definition
5
Variables and Constants Constants: are fixed values which cannot change in program. - Example : Pi = 3.14 This constant used to calculate the circle area.
6
Variables and Constants Variable name constraints : 1- it contain letters and numbers but it must start with a letter. 2- it do not contain space. 3- it do not contain special character such as (x,*,+,-,?,/ ) but Can use only underscore ( _ ) 4- don’t use any keyword to named the variable such as main, cout, for, while, int, char. Legal (valid) Variable : Variables is case sensitivity ( Small and capital letter )
7
Variables and Constants : Example : Do the following variables is true or false ? 1- m2 2- a.b 3- s um 4- x_y 5- 2name 6- H 7- char
8
Variables and Constants : Data type : ExampleDescriptionType 9integerint aCharacterchar 9,55Floating point number.float 9,5555555Double floating point numberdouble True, FalseBoolean valuebool
9
Variables and constants : Declaration: Variables and Constants must be declared before being used in code. Declaration includes: - Data Type. - Name, follow Naming Convention Rules. - Required Value for Constants. - Optional Initial Value for Variables.
10
Variables and constants : Variables : Declaration syntax: [Data type] [ Variable name] Example : int age ; int weight ; float width, length ; char f ;
11
Variables and constants : Constants : * CONST used to declare Constants. Declaration syntax: const [data type] [constant name] = [value] Example : const int student_no=25;
12
Assignment values to the variables: int num1; num1=25; float width =5.6, length = 8.4 ; char ch='m’ ; Example : #include int main() { int number; // declare number as an integer int square; // variable in which square will be stored number = 5; // assign 5 to number square = number * number; // compute square cout << "The square is = " ; cout << square << endl; // display 25 return 0; } OR int num1 = 25;
13
Examples 1- This simple program gets the user's age as input and displays it back to the screen : #include int main() { int age; cout << "My Second C++ Program" << endl; cout << "Please enter your age : "; cin >> age; cout << " You are " << age << " years old." << endl; return 0; }
14
Examples 2- Write a C++ program to find the area of the circle? #include int main () { float r; double area; const double pi=3.14285; cout<<"Enter the radius of the circle: "; cin >> r ; area = pi*r*r; cout << "the area of the circle= " ; cout << area << endl; return 0; } Output : Enter the radius of the circle: the area of the circle=
15
Examples 3- Write a C++ program to find the area of the rectangle ? #include int main () { float w; float L ; float area ; cout << " enter the width : " ; cin >> w ; cout << " enter the length :" ; cin >> L ; area = w*L ; cout << " the area = " << area <<endl; return 0 ; }
16
2-16 C++ Key Words You cannot use any of the C++ key words as an identifier. These words have reserved meaning.
17
2-17 Valid and Invalid Identifiers IDENTIFIERVALID?REASON IF INVALID totalSales Yes total_Sales Yes total.Sales NoCannot contain. 4thQtrSales NoCannot begin with digit totalSale$ NoCannot contain $
18
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Integer Data Types
19
2-19 Integer Data Types Integer variables can hold whole numbers such as 12, 7, and -99.
20
2-20 Defining Variables Variables of the same type can be defined - On separate lines: int length; int width; unsigned int area; - On the same line: int length, width; unsigned int area; Variables of different types must be in different definitions
21
2-21 Defining Variables //This program has variables of several of the integer types. #include using namespace std; int main() { int checking; unsigned int miles; long days; checking= -20; miles = 4276; days = 189000; cout<<"We have made a long journey of "<<miles; cout<<"miles. \n"; cout<<"our checking account balance is"<<checking; cout<<"\n About "<<days <<"days ago Columbus "; cout<<"stood on this spot. \n"; return 0; } This program has three variables: checking, miles, and days
22
2-22 Integer Literals An integer literal is an integer value that is typed into a program’s code. For example: itemsOrdered = 15; In this code, 15 is an integer literal.
23
2-23 //This program has variables of several of the integer types. #include using namespace std; int main() { int checking; unsigned int miles; long days; checking = -20; miles = 4276; days=189000; cout<<"We have made a long journey of "<<miles; cout<<"miles.\n"; cout<<"our checking account balance is "<< checking; cout<<"\n About "<<days <<"days ago Columbus"; cout<<"stood on this spot.\n"; return 0; } Integer Literals
24
2-24 Integer Literals Integer literals are stored in memory as int s by default To store an integer constant in a long memory location, put ‘ L ’ at the end of the number: 1234L Constants that begin with ‘ 0 ’ (zero) are base 8: 075 Constants that begin with ‘ 0x ’ are base 16: 0x75A
25
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The char Data Type
26
2-26 The char Data Type Used to hold characters or very small integer values Usually 1 byte of memory Numeric value of character from the character set is stored in memory: CODE: char letter; letter = 'C'; MEMORY: letter 67
27
2-27 Character Literals Character literals must be enclosed in single quote marks. Example: 'A'
28
2-28
29
2-29 Character Strings A series of characters in consecutive memory locations: "Hello" Stored with the null terminator, \0, at the end: Comprised of the characters between the " " Hello\0
30
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Floating-Point Data Types
31
2-31 Floating-Point Data Types The floating-point data types are: float double long double They can hold real numbers such as: 12.45 -3.8 Stored in a form similar to scientific notation All floating-point numbers are signed
32
2-32 Floating-Point Data Types
33
2-33 Floating-point Literals Can be represented in Fixed point (decimal) notation: 31.41590.0000625 E notation: 3.14159E16.25e-5 Are double by default Can be forced to be float ( 3.14159f ) or long double ( 0.0000625L )
34
2-34 // This program uses floating point data types. # include using namespace std; int main() { float distance; double mass; distance= 1.495979E11; cout<<"The sun is "<<distance<<"meters away. \n"; cout<<"The sun \' s mass is "<< mass << " kilograms. \n"; return 0; }
35
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The bool Data Type
36
2-36 Represents values that are t rue or f alse bool variables are stored as small integers false is represented by 0, t rue by 1: bool allDone = true; bool finished = false; The b ool Data Type allDone 10 finished
37
2-37
38
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Determining the Size of a Data Type
39
2-39 Determining the Size of a Data Type The s izeof operator gives the size of any data type or variable: double amount; cout << "A double is stored in " << sizeof(double) << "bytes\n"; cout << "Variable amount is stored in " << sizeof(amount) < < "bytes\n";
40
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Variable Assignments and Initialization
41
2-41 Variable Assignments and Initialization An assignment statement uses the = operator to store a value in a variable. item = 12; This statement assigns the value 12 to the i tem variable.
42
2-42 Assignment The variable receiving the value must appear on the left side of the = operator. This will NOT work: // ERROR! 12 = item;
43
2-43 Variable Initialization To initialize a variable means to assign it a value when it is defined: int length = 12; Can initialize some or all variables: int length = 12, width = 5, area;
44
2-44
45
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Scope
46
2-46 Scope The scope of a variable: the part of the program in which the variable can be accessed A variable cannot be used before it is defined
47
2-47
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.