0% found this document useful (0 votes)
58 views

Introduction To Programming CS1133: Engr. Rabia Afzal Minhas

The document provides an introduction to programming and C++. It discusses key concepts such as programming languages, variables, data types, comments, special symbols, and input/output. The three main data types in C++ are integers, floating point numbers, and characters. Variables must be declared before use and can be initialized. Comments are used to explain code and are of two types: single-line and multi-line. Important headers and functions like cout and endl are described.

Uploaded by

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

Introduction To Programming CS1133: Engr. Rabia Afzal Minhas

The document provides an introduction to programming and C++. It discusses key concepts such as programming languages, variables, data types, comments, special symbols, and input/output. The three main data types in C++ are integers, floating point numbers, and characters. Variables must be declared before use and can be initialized. Comments are used to explain code and are of two types: single-line and multi-line. Important headers and functions like cout and endl are described.

Uploaded by

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

Introduction to Programming

CS1133

Engr. Rabia Afzal Minhas


Programming language

• A set of rules, symbols, and special words.


C vs C++
• Advantages:
• 1. Faster development time (code reuse)
• 2. Creating / using new data types is easier
• 3. Easier memory management
• 4. Stricter syntax & type checking => less bugs
• 5. Easier to implement Data hiding
• 6. Object Oriented concepts
IDEs on Windows platform
• Turbo C++

• Microsoft Visual C++

• Dev C++
Comments
• Two types of comments
1. Single line comment //….
2. Multi-line (paragraph) comment /* */

• When compiler sees // it ignores all text after this on


same line

• When it sees /*, it scans for the text */ and ignores any
text between /* and */
Special Symbols

+ - * /
. ; ? ,
<= != == >=
Special characters
• Text string special characters (Escape
Sequences)
• \n = newline
• \r = carriage return
• \t = tab
• \" = double quote
• \? = question
• \\ = backslash
• \' = single quote

• Examples:
cout << "Hello\t" << "I\’m Ali\n";
cout << "123\nabc “;
C++ Reserve Words
• Reserved words are also called keywords. The letters that
make up a reserved word are always lowercase

• auto break int long


• case char register return
• const continue short signed
• default do sizeof static
• double else struct switch
• enum extern typedef union
• float for unsigned void
• goto if volatile while
Identifiers
• Identifier: A C++ identifier consists of letters, digits, and
the underscore character (_) and must begin with a letter
or underscore.
• Identifiers can be made of only letters, digits, and the
underscore character; no other symbols are permitted to
form an identifier
• NOTE: C++ is case sensitive—uppercase and
lowercase letters are considered different. Thus, the
identifier NUMBER is not the same as the identifier
number. Similarly, the identifiers X and x are
different
Variables
- Variables are identifiers which represent
some unknown, or variable-value.

- A variable is named storage (some


memory address’s contents)

x = a + b;
Speed_Limit = 90;
Names
Valid Names
• Start with a letter
• Contains letters
• Contains digits
• Contains underscores

• Do not start names with underscores: _age

• Don’t use C++ Reserve Words


Names
• Choose meaningful names
• Don’t use abbreviations and acronyms: mtbf, TLA, myw, nbv

• Don't use overly long names


• Ok:
partial_sum
element_count
staple_partition

• Too long (valid but not good practice):


remaining_free_slots_in_the_symbol_table
Which are Legal Identifiers?
 AREA  area_under_the_curve
 2D  _Marks
 Last Chance
 #values
 x_yt3
 areaoFCirCLe
 Num-2
 Grade***
 %done
 Hello!  return
 If
Header files (functionality declarations)
(Turbo C++) (Visual C++)
• #include<iostream.h> or #include <iostream>
• #include<stdlib.h> or #include<stdlib>
•…
std:: Prefix
• std::cout<“Hello World”;
• std::cout<<Marks;
• std::cout<<“Hello ”<<name;
• Scope Resolution Operator ::

• std is a namespace, Namespaces ?

using namespace std;


cout<<“Hello World”;
cout<<Marks;
cout<<“Hello ”<<name;
main( ) function
• Every C++ program start executing from main ( )

• A function is a construct that contains/encapsulates


statements in a block.

• Block starts from “{“ and ends with “}” brace

• Every statement in the block must end with a semicolon


(;)

• Examples…
cout and endl
• cout (console output) and the operator
• << referred to as the stream insertion operator
• << “Inserts values of data-items or string to screen.”
• >> referred as stream extraction operator, extracts
value from stream and puts into “Variables”

• A string must be enclosed in double-quotation marks.

• endl stands for end line, sending ‘endl’ to the console


outputs a new line
Input and type
• cin>>name; reads characters until a whitespace
character is seen

• Whitespace characters:
• space,
• tab,
• newline {enter key}
rvalue and lvalue
• Are the two occurrences of “a” in this expression the
same?
a = a + 1;
One on the left of the assignment refers to the location
of the variable (whose name is a, or address);
One on the right of the assignment refers to the value of
the variable (whose name is a);

• Two attributes of variables lvalue and rvalue


• The lvalue of a variable is its address
• The rvalue of a variable is its value
rvalue and lvalue

• Assignment Rule: On the left side of an


assignment there must be a lvalue or a
variable (address of memory location)

int i, j;
i = 7;
7 = i;
j * 4 = 7;
Data Types
Three basic PRE-DEFINED data types:
1. To store whole numbers
• int, long int, short int, unsigned int

2. To store real numbers


• float, double

3. Characters
• char
Data Types
• Integral, which is a data type that deals with integers, or
numbers without a decimal part
• Floating-point, which is a data type that deals with
decimal numbers
• Enumeration, which is a user-defined data type
Data Types
Types and literals
• Built-in types Literals
• Boolean type • Boolean: true, false
• bool
• Character types
• Character literals
• char
• 'a', 'x', '4', '\n', '$', ' '
• Integer types
• int
• and short and • Integer literals
long • 0, 1, 123, -6,
• Floating-point types
• double • Floating point literals
• and float • 1.2, 13.345, 0.3, -0.54,

• Standard-library types • String literals


• string
Types and Size in Bytes

TYPE SIZE IN BYTES DATA STORAGE RANGE


int 2 bytes -32768 to 32767

long int 4 bytes -2147483648 to 2147483647

float 4 bytes 3.4 x 10-38 to 3.4x10+38

double 8 bytes 1.7x10-308 to 1.7x10+308

char 1 byte In case of strings, storage


capacity is from 1 byte to
65535 bytes.
Types
• C++ provides a set of types
• E.g. bool, char, int, double called “built-in types”

• C++ programmers can define new types


• Called “user-defined types”

• The C++ standard library provides a set of types


• E.g. string, vector, ..
• (for vector type  #include<vector> )
Declaring Variables

• Before using you must declare the variables


Variable declaration

TYPE <Variable Name> ;

Examples:
int marks;
double Pi;
int suM;
char grade;

- NOTE: Variable names are case sensitive in C++ ??


Variable declaration
• C++ is case sensitive
• Example:
area
Area
AREA
ArEa

are all seen as different variables


Declaring Variables…
• When we declare a variable, what happens ?
• Memory allocation
• How much memory (data type)

• Memory associated with a name (variable name)


• The allocated space has a unique address

Marks

int Marks; %$^%$%$*^%

FE07
Declaration and initialization
int a = 7; 7

int b = 9; 9

char c = 'a'; a

double x = 1.2; 1.2

Hello, world
string s1 = "Hello, world";

1.2
string s2 = "1.2";
Using Variables: Initialization
• Variables may be given initial values, or initialized, when
declared. Examples:

length
int length = 7 ;
7
diameter
float diameter = 5.9 ; 5.9

initial
char initial = ‘A’ ;
‘A’
string type
• Special data type supports working with “strings”
#include <string>
string <variable_name> = “string literal”;

• string type variables in programs:


string firstName, lastName;

• Using with assignment operator:


firstName = “Mohammad";
lastName = “Ali";

• Display using cout


cout << firstName << " " << lastName;
Getting input in string with Spaces
string s1;
cin>> s1; //Spaces will not be input in s1

//Following statements read spaces in “string”


string s1;
getline(cin, s1); //Spaces will be input in s1
String input (Variables)
// Read first and second name
#include<iostream>
#include<string>
int main() {
string first;
string second;
cout << “Enter your first and second names:";
cin >> first >> second;
cout << "Hello “ << first << “ “ << second;
return 0;
}
char type
• Reserves 8 bits or 1 byte of memory
• A char variable may represent:
• ASCII character 'A‘, 'a‘, '1‘, '4‘, '*‘
• signed integers 127 to -128 (Default)
• unsigned integer in range 255 to 0

Examples:
• char grade;
• unsigned char WeekNumber= 200;
• char cGradeA = 65;
• char cGradeAA = ‘A';
int type
• 16 bits (2 bytes) on Windows 16-bits
• int -32,768 to 32,767
• unsigned int 0 to 65,535
• Also on Turbo C++, 2 bytes for int

• 32 bits (4 bytes) on Win32 (Visual C++)


• int -2,147,483,648 to 2,147,483,647
• unsigned int 0 to 4,294,967,295
int type
• Examples:
int earth_diameter;
int seconds_in_week= 604800;
unsigned int Height = 100;
unsigned int Width = 50000;
int type (long and short)
• long int
• reserves 32 bits (4 bytes) of memory
• signed long -2,147,483,648 to 2,147,483,647
• unsigned long int 0 to 4,294,967,295

• short int
• reserves 16 bits (2 bytes) of memory
• signed short int -32,768 to 32,767
• unsigned short int 0 to 65,535
int (long and short)
• Examples:
long int light_speed=186000;
unsigned long int seconds= 604800;
short int Height = 30432;
unsigned short int Width = 50000;
Home Work-1
• Use Visual C++ on Windows and get information for
following data types:
• int
• short
• long int
• short int
• char
• Use ( cout << sizeof( intVar ); ) operator to get
this information, Example:…
Real Values
• float
• Reserves 32 bits (4 bytes) of memory
+38
• + 1.180000x10 , 7-digit precision
• Example: float radius= 33.4221;

• double
• Reserves 64 bits (8 bytes) of memory
+308
• + 1.79000000000000x10 , 15-digit precision
• Example: double Distance = 257.5434342;

• long double
• Reserves 80 bits (10 bytes) of memory , 18-digit precision
• Example: long double EarthMass = 25343427.53434233;
Home Work-2
• Use Visual C++ on Windows and get information for
following data types:
• float
• double
• long double

• Use ( cout << sizeof(floatVar); ) operator to get


this information, Example:…
bool Type
• Only 1 bit of memory required
• Generally, 1 byte because is reserved

• Literal values:
• true
• false

• Can be used in logical conditions:


• Examples:
bool RainToday=false;
bool passed;
passed = GetResult(80);

You might also like