0% found this document useful (0 votes)
30 views17 pages

C++ Lec 2

Uploaded by

Fatima Akeel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views17 pages

C++ Lec 2

Uploaded by

Fatima Akeel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

1 Computer fundamentals and programming

Basic Language Features

❑ Values and Statements


First, a few definitions:
A statement is a unit of code that does something –a basic building block of a program.
An expression is a statement that has a value – for instance, a number, a string, the sum of two numbers, etc. 4+2, x-1,
and "Hello world!\n" are all expressions.

Not every statement is an expression. It makes no sense to talk about the value of an #include statement, for instance.

#include<iostream.h>
main(){
int k;
k=4+2;
cout<<“Hello world”;
}
2 Computer fundamentals and programming
❑ Data Types
Every expression has a type – a formal description of what kind of data its value is. For instance,0 is an integer,3.142
is a floating-point (decimal)number, and "Hello, world!\n“ is a string value(a sequence of characters). Data of
different types take a different amounts of memory to store. Here are the built-in datatypes we will use most often:

short int/ short Short integer 2 bytes Signed:-32768 to 32767

Double Double precision float point number 8 bytes -1.7e+308 to 2.3e-308 (15 digits)

float Floating point number 4 bytes -3.4e+38 to 1.2e-38 (6 digits)


3 Computer fundamentals and programming

Sizeof()
In order to determine the size of data types on a particular machine, C++ provides an operator named sizeof. This operator
accepts one parameter, which can be either a type or a variable itself and returns the size in bytes of that type or object:
Format Example:
sizeof(type) int a; cout<<sizeof(char);
sizeof(variable) a = sizeof (char); O/P → 1
cout<<a;
O/P → 1

#include <iostream.h> O\P


main() bool: 1 bytes
{ char: 1 bytes
cout << "bool:\t\t" << sizeof(bool) << " bytes" << endl; short: 2 bytes
cout << “char:\t\t" << sizeof(char) << " bytes" << endl; int: 4 bytes
cout << "short:\t\t" << sizeof(short) << " bytes" << endl; float: 4 bytes
cout << "int:\t\t" << sizeof(int) << " bytes" << endl; double: 8 bytes
cout << "float:\t\t" << sizeof(float) << " bytes" << endl;
cout << "double:\t\t" << sizeof(double) << " bytes" << endl;
return 0;
}
4 Computer fundamentals and programming

C++ Program
5 Computer fundamentals and programming
❑ Variables
We might want to give a value a name so we can refer to it later. We do this using variables. A variable is a named
location in memory.
For example, say we wanted to use the value 4+2 multiple times. We might call it x and use it as follows:
(Note how we can print a sequence of values by “chaining” the <<
symbol.)
The name of a variable is an identifier token. Identifiers may contain
numbers, letters, and underscores(_), and may not start with a number.

Line 3 is the declaration of the variable x. We must tell the compiler


2 what type x will be so that it knows how much memory to reserve for it
3 and what kinds of operations may be performed on it.
4
5 Line 4 is the initialization of x, where we specify an initial value for it.
This introduces a new operator: =, the assignment operator. We can
6 also change the value of x later on in the code using this operator.

We could replace lines 5 and 6 with a single statement that does both declaration and initialization:
int x = 4 + 2;
6 Computer fundamentals and programming

❑ Initialization of variables
Tow ways of initializing variables are valid and equivalent in C++
• type identifier = initial_value ; → int a = 0;
• type identifier (initial_value) ;→ int a (0);

#include <iostream.h>
int main () O/P
{ it main
6 ()
int a=5; // initial value = 5
int b(2); // initial value = 2
int result;// initial value undetermined
a = a + 3;
result = a - b;
cout << result;
return 0;
}
7 Computer fundamentals and programming

EX:

// operating with variables


#include <iostream>
int main ()
{
// declaring variables: result= 4
int a, b; a=6
int result;
// process:
a = 5;
b = 2;
a = a + 1;
result = a - b;
// print out the result:
cout << “result=”<< result<<endl;
cout << “a=“<<a<<endl;
// terminate the program:
return 0;
}
8 Computer fundamentals and programming

Naming Rules for Variables


The names of variables in the C++ language are referred to as identifiers. The best naming convention is to choose a variable name that
will tell the reader of the program what the variable represents.

Rules for naming variables:


▪ Variable names in Visual C++ can range from 1 to 255 characters. To make variable names portable to other environments stay within a 1 to 31
character range.

▪ All variable names must begin with a letter of the alphabet or an underscore( _ ). For beginning programmers, it may be easier to begin all
variable names with a letter of the alphabet.
▪ After the first initial letter, variable names can also contain letters and numbers. No spaces or special characters, however, are allowed.

▪ Uppercase characters are distinct from lowercase characters. All uppercase letters is used primarily to identify constant variables.
▪ You cannot use a C++ keyword (reserved word) as a variable name.

Tip: Some programmers use an underscore in variable names to separate


parts of the name, such as shipping_weight. Others prefer a "capital style"
notation, such as shippingWeight to separate parts of the name.

Grid2 Grid test


9 Computer fundamentals and programming

❑ Constants/Literals.
Constants refer to fixed values that the program may not alter and they are called literals. Constants can be of
any of the basic data types and can be divided into Integer Numerals, Floating-Point Numerals, Characters,
Strings and Boolean Values.

• Integer Numerals

- They are numerical constants that identify integer decimal values. Notice that to express a numerical constant we do
not have to write quotes (") nor any special character → Ex. 1777, 453,..

- In addition to decimal numbers ,C++ allows the use as literal constants of octal numbers (base 8) and hexadecimal
numbers (base 16). If we want to express an octal number we have to precede it with a 0 (zero character). And in
order to express a hexadecimal number we have to precede it with the characters 0x (zero, x). For example, the
following literal constants are all equivalent to each other:
Ex.
75 // decimal
0113 // octal
0x4b // hexadecimal
10 Computer fundamentals and programming

• Floating Point Numbers


They express numbers with decimals and/or exponents. They can include either a decimal point, an e character (that
expresses "by ten at the Xth height", where X is an integer value that follows the e character), or both a decimal point
and an e character:

Note

• Character and string literals


There also exist non-numerical constants, like: To represent single character we enclose it between single
'z' quotes (') and to express a string (which
'p' generally consists of more than one character) we enclose it
"Hello world" between double quotes (").
"How do you do?"
11 Computer fundamentals and programming

Constants
A constant, like a variable, is a memory location where a value can be stored. Unlike variables, constants
never change in value. You must initialize a constant when it is created.

➢ Declared constants (const)


With the const prefix you can declare constants with a specific type in the same way
as you would do with a variable:
const int Kilo = 1000;
// Declared Constant O/P = 10000
#include <iostream.h>
const int Kilo=1000;
int main ()
{
int a=5;
double r;
r = 2 * kilo * a;
cout << r;
return 0; }
12 Computer fundamentals and programming

➢ Defined constants (#define)


There's another way to define constants that dates back to early versions of the C language, the precursor of C .++

For example:
#define PI 3.14159265
#define NEWLINE '\n'

// defined constants O/P = 31.4159


#include <iostream.h>
#define PI 3.14159
#define NEWLINE '\n'
int main ()
{
double r=5.0; // radius
double circle;
circle = 2 * PI * r;
cout << circle;
cout << NEWLINE;
return 0; }
13 Computer fundamentals and programming

C++ Program
11 Computer fundamentals and programming

C++ Program
15 Computer fundamentals and programming

Input
Now that we know how to give names to values, we can have the user of the program input values.

.h>

Just as cout << is the syntax for out putting values, cin >> is the syntax for inputting values.
16 Computer fundamentals and programming

C++ Program
17 Computer fundamentals and programming
Example:
Write a C++ to input two values using cin operator and print sum and average of those values.

You might also like