Download presentation
Presentation is loading. Please wait.
1
Introduction to Programming - 3
Dr. Khizar Hayat Associate Prof. of Computer Science
2
Scope of a variable All the variables that we intend to use in a program must be declared first with a valid identifier (name) and data type. A variable may either be global or local in scope. A global variable is declared outside all the functions of the program while a local variable is declared inside a block of code (e.g. function) where needed. A global variable is visible (known) from all parts of the program that come after it’s declaration, whereas a local variable is limited to only its delimiters {}, i.e. ‘{‘ just before it’s declaration and it’s corresponding ‘}’.
3
Scope of a variable: Two examples
4
Initialization of Variables
When declaring a regular local variable, its value is undetermined by default You may want a variable to store a concrete value at the moment it is declared; this is called initialization. In C++, there are two ways to initialize a variable: C-like initialization by using the ‘=‘ sign, e.g. int sum = 0; Constructor initialization using parenthesis, e.g. int sum(0);
5
Constants Constants are expressions with a fixed value. Three types:
Literals, e.g. in the statement a=5; 5 is the constant Defined constants (#define) #define PI 3.14 Declared constants (const) const int pathw=25; /* pathw like a normal variable but you cannot modify its value*/
6
Literal constants (using the const keyword)
//program-area of circle #include<iostream> using namespace std; int main() { float r,area; cout<<"Enter the value for radius"<<endl; cin>>r; area=3.14*r*r; cout<<"Area of Circle is"<<area<<endl; return(0); }
7
Defined constants (#define)
//program-area of circle #include<iostream> #define PI 3.14 using namespace std; int main() { float r,area; cout<<"Enter the value for radius"<<endl; cin>>r; area=PI*r*r; cout<<"Area of Circle is"<<area<<endl; return(0); }
8
Declared constants (using the const keyword)
//program-area of circle #include<iostream> using namespace std; int main() { float r,area; const float pi=3.14; cout<<"Enter the value for radius"<<endl; cin>>r; area=pi*r*r; cout<<"Area of Circle is"<<area<<endl; return(0); }
9
Constants: Integer Literals
An integer literal can be a decimal, octal, or hexadecimal constant. A prefix specifies the base or radix: 0x or 0X for hexadecimal, 0 for octal, and nothing for decimal. An integer literal can also have a suffix that is a combination of U and L, for unsigned and long, respectively. The suffix can be uppercase or lowercase and can be in any order.
10
Constants: Integer Literals
11
Constants: Floating-point Literals
You can represent floating point literals either in decimal form or exponential form. A floating-point literal has an integer part, a decimal point, a fractional part, and/or an exponent part. The signed exponent is introduced by e or E.
12
Constants: Boolean Literals
Data type bool Has just two valid values, true and false In the classic C, 1 meant true and 0 meant false Even with C++ the bool values, true and false, stands for small integer values of 1 and 0, respectively.
13
Constants: Character and String Literals
Non-numerical constants, like: 'z’ // character constant 'p' //character constant "Hello world" // String constant "How do you do?“// String constant To represent a single character we enclose it between single quotes (') To express a string (generally consists of more than one character) we enclose it between double quotes ("). Escape sequence Meaning \\ \ character \' ' character \" " character \a Alert or bell \b Backspace \f Form feed \n Newline \r Carriage return \t Horizontal tab \v Vertical tab \o Octal number (follows) \x Hexadecimal number (follows)
14
Example to demonstate data types page 16 of notes
#include<iostream> using namespace std; int main() { int a; long b; unsigned long c; short int d; a=45; b=189000; c= ; d=12; cout<<“Memory size of integer is "<<sizeof(a)<<" bytes"<<endl; cout<<“The value of a is "<<a<<endl; cout<<“Memory size of long integer is "<<sizeof(b)<<" bytes"<<endl; cout<<“The value of b is "<<b<<endl; cout<<“Memory size of unsigned long integer is "<<sizeof(c)<<" bytes"<<endl; cout<<“The value of c is "<<c<<endl; cout<<“Memory size of short integer is "<<sizeof(d)<<" bytes"<<endl; cout<<“The value of d is "<<d<<endl; return(0); }
15
Example 2: The character literals
#include<iostream> using namespace std; int main() { char letter; letter=‘A’; cout<<letter<<endl; letter=‘B’; cout<<“The size of character is "<<sizeof(letter)<<“ bytes"<<endl; return(0); }
16
Example: The character literals
#include<iostream> using namespace std; int main() { char letter; int n; letter=48; cout<<letter<<endl; letter=97; n=‘9’; cout<<n<<endl; n=‘Z’; return(0); }
17
Example 3: Floating point literals
#include<iostream> using namespace std; int main() { float distance; double mass; distance= E11f; mass=1.989E30; cout<<“The sun is "<<distance<<“ meters away\n”; cout<<“The sun’s mass is "<<mass<<“ kilograms\n”; cout<<“The size of float is "<<sizeof(distance)<<endl; cout<<“The size of double is "<<sizeof(mass)<<endl; cout<<“The size of long double is "<<sizeof(long double)<<endl; return(0); }
18
What happens to your C++ code?
19
Manipulators Operators used with the insertion operator << tomodify or manipulate the way of data display. Examples: endl for linefeed, i.e ‘\n’ or ‘\r’ setw(n) like an imaginary box with a certain width n - see examples on page in notes setprecision(n) for the number of digits to be printed on the right of decimal point. Don’t forget (for the latter 2) #include<iomanip>
20
setw(n) #include<iostream> #include<iomanip> using namespace std; int main() { long int pop1= , pop2=47, pop3=9761; cout<<“LOCATION "<<“POPULATION”<<endl; cout<<“Portcity "<<pop1<<endl; cout<<“Hightown "<<pop2<<endl; cout<<“Lowville "<<pop3<<endl; return(0); }
21
setw(n) #include<iostream> #include<iomanip> using namespace std; int main() { long int pop1= , pop2=47, pop3=9761; cout<<“LOCATION "<<“POPULATION”<<endl; cout<<“Portcity "<<setw(12)<<pop1<<endl; cout<<“Hightown "<<setw(12)<<pop2<<endl; cout<<“Lowville "<<setw(12)<<pop3<<endl; return(0); }
22
setw(n) #include<iostream> #include<iomanip> using namespace std; int main() { long int pop1= , pop2=47, pop3=9761; cout<<“LOCATION "<<“POPULATION”<<endl; cout<<setw(8)<<“Portcity "<<setw(12)<<pop1<<endl; cout<<setw(8)<<“Hightown "<<setw(12)<<pop2<<endl; cout<<setw(8)<<“Lowville "<<setw(12)<<pop3<<endl; return(0); }
23
setprecision(n) #include<iostream> #include<iomanip> using namespace std; int main() { double b= cout<<setprecision(2)<<b<<endl; return(0); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.