KEMBAR78
Programming Fundamentals | PPTX
Programming
Fundamentals
Lecture#03
Delivered By:
Hassan iqbal
Superior College Toba Tek Singh
Basic Elements of a
C++ Program
Keywords:
 The predefined words of the C++ that are
used for special purposes in the source
program.
 Also known as reserved words
 Always written in lowercase
 There are 63 keywords in c++.
Tokens:
 In C++, a source program consists of
keywords, variables(or identifiers), constants,
operators, punctuators.
 These elements of a c++ program are called
tokens.
Identifier:
 The unique names used in the program to
represent the variables, constants, functions,
and labels etc. are called identifiers.
 The name of identifier may be 31 characters
long. If the name is more than 31 characters
long , then first 31 characters will be used by
the compiler.
 An identifier may:
 consist of alphabets, digits, and underscore.
 The first character of identifier name must be an
alphabetic letter or underscore.
 The keywords cannot be used as identifier
name.
Types of identifiers:
 Standard Identifier
 There are also predefined identifiers in c++
 The predefined identifiers that are used for
special purposes in the source program are
called the standard identifiers.
 E.g cin, cout
 User-defined identifier
 the identifiers defined by the user in the
program are called user-defined identifers.
 E.g variables, user-defined functions, labels
Data Types in C++:
 Data types specifies the type of the data
that a variable will hold.
 The data types defines the set of values
and set of operations on those values.
 Two categories:
 Standard Data Types
 The data types that are predefined as a part
of language.
 User defined Data Types
 The data types defined by the user
 The “int” data type:
Data Type
Name
Storage
capacity
Range
Int 2 bytes -32768 to 32767
Short int 2 bytes -32768 to 32767
Long int 4 bytes -2147483648 to
2147483647
Signed int Default Default
Unsigned int 2 bytes 0 to 65535
Unsigned long
int
4 bytes 0 to 4294967295
Real Type Data:
 The numeric values that have decimal
point in them are called real type data.
 Also known as floating point type data
 The real type data can be represented as
exponential notation i.e in terms of
mantissa and exponent.
 6432157.0 as 6.432157*10^6
 Where 6432157 is mantissa and 6 is the
exponent.
 As 6.432157E6
The “float” Data Type
Name Storage Capacity Range
Float 4 bytes 3.14*10−38
to
3.4*1038
Double 8 bytes 1.7*10−308 to
1.7*10308
Long double 10 bytes 3.14*10−4932
to
3.4*104932
The “char” data type:
 Used to store a single character such as
“a”, “$” etc.
 When the character is stored into a
variable, the ACII value is stored into the
variable.
 The ASCII value of ‘A’ is 65 and ‘a’ is 97.
 By default the character data type is
unsigned because the ASCII values are
positive.
 The range for unsigned ‘char’ data type is
from 0 to 255.
 For signed ‘char’ its -128 to127
Overflow and Underflow:
 An overflow occurs when the value
assigned to a variable is more than the
maximum allowable limit.
 An underflow occurs when the value
assigned to a variable is less then the
minimum allowable limit.
Variables:
 A quantity whose value may change
during program execution.
 Used to store program’s input data and
results during program execution.
 There are two basic operations that are
performed onto the variables.
 Declaration
 Initialization
Rules for Naming Variables:
 A variable name may consist of:
 Letter, digit, underscore
 The 1st character must be a letter or underscore.
 Special characters except underscore can’t be
used
 The keyword can’t be used as variable name
 Blank spaces are not allowed
 Length may be from 1 to 31 characters
 A variable name declared for one data type
can’t be used for another data type.
 Both uppercase and lowercase letters can be
used
 C++ is a case sensitive language.
 The meaningful name should be given to the
variable.
Constants:
 A quantity whose value does not change
during program execution
 Types:
 Literal constants
 Symbolic constants
Literal Constants:
 The word ‘literal’ means exact or
accurate.
 A literal is a constant having independent
value that is used in the program source
code.
 E.g 10,10.5, “excellent”
 Types:
 Integer Constant
 Integer constant must lie within the range of
the integer.
 The +ve and –ve signs can also be used.
 Floating-Point Constant:
 Letter ‘F’ or ‘L’ is used with the constant to
specify its data type.
 E.g 7.619F, 21.62L
 Character Constant:
 A single character enclosed in single
quotation marks.
 E.g ‘a’, ‘+’
 String Constant:
 A string of characters enclosed in double
quotation marks is known as string constant.
 E.g “abc”, “659329”
Symbolic Constants:
 is a constant identifer.
 Once a value is assigned to a symbolic
constant, it can’t be changed during
program execution.
 Can be declared in two ways:
 The ‘const’ keyword
 Using ‘define’ directive
 The ‘const’ keyword:
 Used to declare a constant identifier
 E.g const float pi=3.1417;
 The’ define’ directive:
 It’s a preprocessor directive
 Used to define a constant known as
constant macro
 A constant Macro is an identifier, which is
assigned a particular constant value
 Syntax:
 #define identifier expression
 # define pi 3.141593
Difference between define
and const
‘define’ directive ‘const’ Qualifier
Used as preprocessor
directive
Used as a statement
Not terminated with the ; Terminated with the ;
Data type of constant
identifier is not specified
Data type is specified
Operators and Expressions:
 operators are the special symbols that are
used to perform special operations on the
data.
 Arithmetic operators
 Increment and decrement operator
 Assignment operator
 Relational operator
 Logical operator
 The ‘sizeof’ operator
 Categories of operators:
 unary
 binary
Data Type of an Expression
Data types of operands Data type of expression
Int, float, long Float
int., long Long
Int, double, long double Long double
o Operators precedence:
o The order in which the arithmetic operators are
evaluated
o Without parenthesis:
o All multiplications and divisions first from left to
right first
o All additions and subtractions are then
performed from left to right
o Operators associativity:
o The order in which the operators of the same
precedence are evaluated
o For example in case of * and / operators, the
operations are performed from left to right
Operators Associativity
(), ++postfix, -- postfix Left to right
++ prefix, ++prefix Left to right
*,/,% Left to right
+,- Left to right
=,+=,-=,*=,/= Right to left
Assignment Statement
 A statement that is used to assign a value to a
variable.
 E.g c=a+b;
 Rvalue and Lvalue:
 The rvalue is an operand that can be written on
the right side of the assignment operator. It can
be a constant value, variable or na expression
 The lvalue is an operand that can be written on
the left side of the assignment operator.
 It must be a variable.
 Lvalue can be used as Rvalue but Rvalue
cannot be used as Lvalue.
 Compound assignment Statement:
 An assignment statement that is used to
assign the same value to many variables
 Also known as multiple assignment
statement
 Compound assignment operator
 Is a combination of arithmetic operator and
the assignment operator
Simple assignment
Statement
Assignment Statement using
Compound assignment
operator
X=x+12 X+=12
x=x-4 X-=4
X=x/3 x/=3
Increment and decrement operator:
Postfix, prefix
Type Casting
 Explicit type casting
 Cast operator
 (type) expression
int x;
x=(int) 3.156;
cout<<x;
 Implicit type casting
Comments:
 Single line
 Multi line
Programming Fundamentals
Programming Fundamentals

Programming Fundamentals

  • 1.
  • 2.
    Basic Elements ofa C++ Program
  • 3.
    Keywords:  The predefinedwords of the C++ that are used for special purposes in the source program.  Also known as reserved words  Always written in lowercase  There are 63 keywords in c++. Tokens:  In C++, a source program consists of keywords, variables(or identifiers), constants, operators, punctuators.  These elements of a c++ program are called tokens.
  • 4.
    Identifier:  The uniquenames used in the program to represent the variables, constants, functions, and labels etc. are called identifiers.  The name of identifier may be 31 characters long. If the name is more than 31 characters long , then first 31 characters will be used by the compiler.  An identifier may:  consist of alphabets, digits, and underscore.  The first character of identifier name must be an alphabetic letter or underscore.  The keywords cannot be used as identifier name.
  • 5.
    Types of identifiers: Standard Identifier  There are also predefined identifiers in c++  The predefined identifiers that are used for special purposes in the source program are called the standard identifiers.  E.g cin, cout  User-defined identifier  the identifiers defined by the user in the program are called user-defined identifers.  E.g variables, user-defined functions, labels
  • 6.
    Data Types inC++:  Data types specifies the type of the data that a variable will hold.  The data types defines the set of values and set of operations on those values.  Two categories:  Standard Data Types  The data types that are predefined as a part of language.  User defined Data Types  The data types defined by the user
  • 7.
     The “int”data type: Data Type Name Storage capacity Range Int 2 bytes -32768 to 32767 Short int 2 bytes -32768 to 32767 Long int 4 bytes -2147483648 to 2147483647 Signed int Default Default Unsigned int 2 bytes 0 to 65535 Unsigned long int 4 bytes 0 to 4294967295
  • 8.
    Real Type Data: The numeric values that have decimal point in them are called real type data.  Also known as floating point type data  The real type data can be represented as exponential notation i.e in terms of mantissa and exponent.  6432157.0 as 6.432157*10^6  Where 6432157 is mantissa and 6 is the exponent.  As 6.432157E6
  • 9.
    The “float” DataType Name Storage Capacity Range Float 4 bytes 3.14*10−38 to 3.4*1038 Double 8 bytes 1.7*10−308 to 1.7*10308 Long double 10 bytes 3.14*10−4932 to 3.4*104932
  • 10.
    The “char” datatype:  Used to store a single character such as “a”, “$” etc.  When the character is stored into a variable, the ACII value is stored into the variable.  The ASCII value of ‘A’ is 65 and ‘a’ is 97.  By default the character data type is unsigned because the ASCII values are positive.  The range for unsigned ‘char’ data type is from 0 to 255.  For signed ‘char’ its -128 to127
  • 11.
    Overflow and Underflow: An overflow occurs when the value assigned to a variable is more than the maximum allowable limit.  An underflow occurs when the value assigned to a variable is less then the minimum allowable limit.
  • 12.
    Variables:  A quantitywhose value may change during program execution.  Used to store program’s input data and results during program execution.  There are two basic operations that are performed onto the variables.  Declaration  Initialization
  • 13.
    Rules for NamingVariables:  A variable name may consist of:  Letter, digit, underscore  The 1st character must be a letter or underscore.  Special characters except underscore can’t be used  The keyword can’t be used as variable name  Blank spaces are not allowed  Length may be from 1 to 31 characters  A variable name declared for one data type can’t be used for another data type.  Both uppercase and lowercase letters can be used  C++ is a case sensitive language.  The meaningful name should be given to the variable.
  • 14.
    Constants:  A quantitywhose value does not change during program execution  Types:  Literal constants  Symbolic constants
  • 15.
    Literal Constants:  Theword ‘literal’ means exact or accurate.  A literal is a constant having independent value that is used in the program source code.  E.g 10,10.5, “excellent”  Types:  Integer Constant  Integer constant must lie within the range of the integer.  The +ve and –ve signs can also be used.
  • 16.
     Floating-Point Constant: Letter ‘F’ or ‘L’ is used with the constant to specify its data type.  E.g 7.619F, 21.62L  Character Constant:  A single character enclosed in single quotation marks.  E.g ‘a’, ‘+’  String Constant:  A string of characters enclosed in double quotation marks is known as string constant.  E.g “abc”, “659329”
  • 17.
    Symbolic Constants:  isa constant identifer.  Once a value is assigned to a symbolic constant, it can’t be changed during program execution.  Can be declared in two ways:  The ‘const’ keyword  Using ‘define’ directive
  • 18.
     The ‘const’keyword:  Used to declare a constant identifier  E.g const float pi=3.1417;  The’ define’ directive:  It’s a preprocessor directive  Used to define a constant known as constant macro  A constant Macro is an identifier, which is assigned a particular constant value  Syntax:  #define identifier expression  # define pi 3.141593
  • 19.
    Difference between define andconst ‘define’ directive ‘const’ Qualifier Used as preprocessor directive Used as a statement Not terminated with the ; Terminated with the ; Data type of constant identifier is not specified Data type is specified
  • 20.
    Operators and Expressions: operators are the special symbols that are used to perform special operations on the data.  Arithmetic operators  Increment and decrement operator  Assignment operator  Relational operator  Logical operator  The ‘sizeof’ operator  Categories of operators:  unary  binary
  • 21.
    Data Type ofan Expression Data types of operands Data type of expression Int, float, long Float int., long Long Int, double, long double Long double
  • 22.
    o Operators precedence: oThe order in which the arithmetic operators are evaluated o Without parenthesis: o All multiplications and divisions first from left to right first o All additions and subtractions are then performed from left to right o Operators associativity: o The order in which the operators of the same precedence are evaluated o For example in case of * and / operators, the operations are performed from left to right
  • 23.
    Operators Associativity (), ++postfix,-- postfix Left to right ++ prefix, ++prefix Left to right *,/,% Left to right +,- Left to right =,+=,-=,*=,/= Right to left
  • 24.
    Assignment Statement  Astatement that is used to assign a value to a variable.  E.g c=a+b;  Rvalue and Lvalue:  The rvalue is an operand that can be written on the right side of the assignment operator. It can be a constant value, variable or na expression  The lvalue is an operand that can be written on the left side of the assignment operator.  It must be a variable.  Lvalue can be used as Rvalue but Rvalue cannot be used as Lvalue.
  • 25.
     Compound assignmentStatement:  An assignment statement that is used to assign the same value to many variables  Also known as multiple assignment statement  Compound assignment operator  Is a combination of arithmetic operator and the assignment operator
  • 26.
    Simple assignment Statement Assignment Statementusing Compound assignment operator X=x+12 X+=12 x=x-4 X-=4 X=x/3 x/=3 Increment and decrement operator: Postfix, prefix
  • 27.
    Type Casting  Explicittype casting  Cast operator  (type) expression int x; x=(int) 3.156; cout<<x;  Implicit type casting Comments:  Single line  Multi line