Lecture 17 & 18
Lecture 17 & 18
Wah Campus
• C# Language
– enum days {Mon, Tue, Wed, Thu, Fri, Sat, Sun};
C++
Character Types: It stores a single • Characters: char - Stores a single ASCII or
character and requires a single byte Unicode character.
• Booleanos: bool - Stores a true value (true)
of memory in almost all compilers. the fake (false), similar to Java.
C# Python
• Booleanos: bool - Similar to Java and C++, • Booleanos: bool - Stores a true value (True)
stores a true value (true) the fake (false) the fake (False), used in logical expressions
• Characters: char - Stores a single Unicode and decision making
character. • Characters: str (text string) - Stores a
sequence of characters. Represents a text
string, such as "Hello", "Python", "123".
Theory Of Programming Languages
Fall 2024 7
~~~ Dr. Khalid Iqbal Khattak ~~~
Character String Types
A character string type is one in which the values consist of sequences of
characters
The length can be static and set when the string is created. Such a string is
called a static length string. e.g. Java, Python.
Limited dynamic length strings are those strings which are allowed to have
varying length up to adeclared and fixed maximum set by the variable’s
definition. e.g. C, C++.
Dynamic length strings are those strings which have varying length with no
maximum. e.g. JavaScript, Perl, and the standard C++ library.
Theory Of Programming Languages
Fall 2024 8
~~~ Dr. Khalid Iqbal Khattak ~~~
Character String Types
Syntax
• Examples:
– type digit=0..9; letter='A'..'Z';
– Var num:digit; alpha:letter
• A subrange type is a data type whose value range only covers
a subset of a base type.
Only integer types are possible as the base type.
• Syntax: <Name> : <Inttype> (<lower bound>..<upper bound>)
<Inttype> Data type of the subrange
(SINT, USINT, INT, UINT, DINT, UDINT, BYTE, WORD, DWORD, LINT, ULINT, LWORD).
<lower Lower bound of the range: Constant that must be compatible with the base data type. The lower bound itself is
bound> included in this range.
<opper Upper bound of the range: Constant that must be compatible with the base data type. The upper bound itself is
bound> included in range.
Theory Of Programming Languages
Fall 2024 13
~~~ Dr. Khalid Iqbal Khattak ~~~
Subrange Type
. • Syntax: <Name> : <Inttype> (<lower bound>..<upper bound>)
<Inttype> Data type of the subrange
(SINT, USINT, INT, UINT, DINT, UDINT, BYTE, WORD, DWORD, LINT, ULINT,
LWORD).
<lower Lower bound of the range: Constant that must be compatible with the base
bound> data type. The lower bound itself is included in this range.
<upper Upper bound of the range: Constant that must be compatible with the base
bound> data type. The upper bound itself is included in range.
DWORD variable
A DWORD is a 32-
• Sample bit unsigned
can hold values in
the range 0 to
– VAR integer
4,294,967,295
nVarA: INT (-4095…4095);
nVarB : UINT (0…10000);
END_VAR
#include <iostream>
using namespace std;
int main() {
seasons s;
s = autumn;
cout << “autumn= " << s << endl;
return 0;
}
Output
autumn= 9
Theory Of Programming Languages
Fall 2024 19
~~~ Dr. Khalid Iqbal Khattak ~~~
Implementation
#include <iostream>
using namespace std; bitwise OR | operator
enum designFlags {
ITALICS = 1,
BOLD = 2,
UNDERLINE = 4
};
int main()
{
int myDesign = BOLD | UNDERLINE;
// 00000010
// | 00000100
// ___________
// 00000110
Output
cout << myDesign;
return 0;} 6
Theory Of Programming Languages
Fall 2024 20
~~~ Dr. Khalid Iqbal Khattak ~~~
Implementation
C++ Program to find direction using Enumeration
#include <iostream>
using namespace std;
Direction: 2
Theory Of Programming Languages
Fall 2024 21
~~~ Dr. Khalid Iqbal Khattak ~~~
Record Types
• field END;
/
/
const user = #{ const address = #{ t
// a const enemies = const friends = #[“Taha",
name: “T.Iqbal", street: "21B Mehr
record
age: 27 } Street", city: ‘Wah'} u #[ “Tom", “Taha"] “Jawad Ahmed"]
p
l
e
The above list expressions are not equal because the values are not in the
same order.
To be equal, the second statement must be:
Output
list1 = ["Taha", "Saif", "Jawad"]
['Taha', 'Saif', 'Jawad']
list2 = [10, 50, 70, 90, 30]
[10, 50, 70, 90, 30]
list3 = [True, False, False]
[True, False, False]
print(list1)
print(list2)
print(list3)
Output
Note : Size of the union is the size of its largest field because sufficient number of bytes must
be reserved to store the largest sized field.
Theory Of Programming Languages
Fall 2024 29
~~~ Dr. Khalid Iqbal Khattak ~~~
Union Types
size of
size of structure
union = = 40
Output
32
Example (C++ )
Example
variables a int * ptr1;
and b are int a = 0;
aliases. b is int * ptr2 = new int[100];
reference to int &b = a;
a. ptr1 = ptr2;
int &p;
int &ref = i;
p = a; // It is incorrect as we should declare and
initialize references at single step
Theory Of Programming Languages
Fall 2024 33
~~~ Dr. Khalid Iqbal Khattak ~~~
Pointer and Reference Types
Output
In C++, Strong types are user-defined types that do not allow implicit
conversions (coercion) to other types.
#include <iostream>
using namespace std;
int main() { int num_int;
double num_double = 9.99;
// implicit conversion
// assigning a double value to an int variable
num_int = num_double;
cout << "num_int = " << num_int << endl;
cout << "num_double = " << num_double << endl;
return 0;}
Theory Of Programming Languages
Fall 2024 36
~~~ Dr. Khalid Iqbal Khattak ~~~
Strong Typing
Output Sum = 2
Algol-68, Modula-3, C, ML
use structural type
equivalence.
struct Rec2{
char x;
int y;
char z[10];};
struct Rec3{
int y;
char x;
char z[10];};
In a C-like syntax, the types struct Rec1 and struct Rec2 are structurally
equivalent, but struct Rec1 and struct Rec3 are not
Theory Of Programming Languages
Fall 2024 40
~~~ Dr. Khalid Iqbal Khattak ~~~
Type Equivalence
END
_______________________________