cs4443 - modern programming language - i lecture (2)

48
CS4443 MODERN PROGRAMMING LANGUAGE – I Mr. Dilawar Lecturer, Computer Science Faculty, Bakhtar University Kabul, Afghanistan.

Upload: dilawar-khan

Post on 20-Mar-2017

9 views

Category:

Education


1 download

TRANSCRIPT

Page 1: CS4443 - Modern Programming Language - I  Lecture (2)

CS4443MODERN PROGRAMMING

LANGUAGE – I Mr. Dilawar

Lecturer,Computer Science Faculty,

Bakhtar UniversityKabul, Afghanistan.

Page 2: CS4443 - Modern Programming Language - I  Lecture (2)

Previous Outline• The C# language

• The .NET Architecture and .NET Framework

• CLR, MSIL, JITers, FCL, CLS, CTS and GC

• C# compare to C++

• Development Environment

• Console Applications and Windows Form Application

• Working with First Hello World Program in C#

• Understanding namespaces, using keyword, class keyword, main() method and comments in C#

Page 3: CS4443 - Modern Programming Language - I  Lecture (2)

C# Language FundamentalsChapter 2

Page 4: CS4443 - Modern Programming Language - I  Lecture (2)

Chapter Outline• Basic Data Types and their Mapping to CTS

• Variables, Constants, and Operators

• Working with Flow Control and Conditional Statements

• Type Conversion, String Manipulation and Complex Variable Types

• Arrays in C#

• Foreach loop

Page 5: CS4443 - Modern Programming Language - I  Lecture (2)

Lecture Outline• Basic Data Types and their Mapping to CTS

• Variables, Constants, and Operators

• Working with Flow Control and Conditional Statements

• Type Conversion, String Manipulation and Complex Variable Types

• Arrays in C#

• Foreach loop

Page 6: CS4443 - Modern Programming Language - I  Lecture (2)

Basic Data Types and their Mapping to CTS• Data types are implemented based on their classification.

• Value types (implicit data types, structures, and enumeration)• Reference types (objects, strings, arrays, delegates)

• A data type is a value type if it holds the data within its own memory allocation.• A variable of a value type is passed to method by passing an exact copy.

• A reference type contains a pointer to another memory location that holds the data.• A variable of a reference type is passed to method by passing only their reference.

Page 7: CS4443 - Modern Programming Language - I  Lecture (2)

Basic Data Types and their Mapping to CTS• Implicit data types are defined by the language vender.

• Explicit data types are composed or made by using the implicit data type.

• Implicit data types are complaint with .NET complaint languages and are mapped to types in the CTS.• Hence each implicit data type in C# has its corresponding .NET type.

• The implicit data types in C# are given in the next slide.

Page 8: CS4443 - Modern Programming Language - I  Lecture (2)

Basic Data Types and their Mapping to CTS

C# Type .NET Type Size in Bytes Description

byte Byte 1 May contain integers from 0 – 255

sbyte Sbyte 1 Signed byte from -128 to 127

short Int16 2 Ranges from -32768 to +32767

ushort UInt16 2 Unsigned, ranges from 0 to 65535

int Int32 4 Ranges from -2147483648 to +2147483647

uint UInt32 4 Unsigned, ranges from 0 to 4294967295

long Int64 8 -9223372036854775808 to +9223372036854775808

ulong UInt64 8 Unsigned, ranges from 0 to 18446744073709551615

Page 9: CS4443 - Modern Programming Language - I  Lecture (2)

Basic Data Types and their Mapping to CTS

C# Type .NET Type Size in Bytes Description

float Single 4 Ranges from to precision. Requires the suffix f ot F.

double Double 8 Ranges from to with 15-16 digits precision.

bool Boolean 1 Contains either true or false

char Char 2 Contains any single Unicode character enclosed in single quotation mark such as ‘c’

decimal Decimal 12 Ranges from to with 28-29 digits precision. Requires the suffix ‘m’ or ‘M’

Page 10: CS4443 - Modern Programming Language - I  Lecture (2)

Basic Data Types and their Mapping to CTS• Implicit data types are represented in language using keywords in C#.

• string is also a keyword in C#.

• Implicit data types – are value types and they are stored on stack.• A stack is a data structure that store items in FIFO fashion.• It is an area of memory supported by the processor and its size is determined at

the compile time.

• While user defined types or referenced types are stored using heap.• A heap consists of memory available to the program at runtime.• Reference types are allocated using memory available from the heap dynamically.

Page 11: CS4443 - Modern Programming Language - I  Lecture (2)

Variables• A variable is the name given to a memory location holding a particular

type of data.• Each variable has a data type and a value.

• In C#, variable are declared as: <data type> <variable_name>;

• You can initialize the variable as you declare it (on the fly).• You can also declare/initialize multiple variables of the same type in a single

statement.

Page 12: CS4443 - Modern Programming Language - I  Lecture (2)

Variables• In C#, like other languages you must declare variables before using

them.

• Definite Assignment – Local variables must be initialized before being used.

• C# does not assign default values to local variables.

• C# is also a type safe language.• Values of particular data type can only be stored in their respective data type.• Can’t store integer values in Boolean data types like we do in C/C++.

Page 13: CS4443 - Modern Programming Language - I  Lecture (2)

Naming Conventions for Variable and Methods• Microsoft suggests using Carrel Notation for variables.• First letter in lowercase.

• Pascal Notation for methods.• First letter in uppercase.

• Each word after the first word in the name of both variables and methods should start with a capital letter.

Page 14: CS4443 - Modern Programming Language - I  Lecture (2)

Constants• Constants are variables whose values, once defined, can not be

changed by the program.

• Constant variables are declared using the CONST keyword like: const double PI = 3.142;

• Constant variables must be initialized as they are declared.

• It is conventional to use capital letters when naming constant variables.

Page 15: CS4443 - Modern Programming Language - I  Lecture (2)

Operators • Symbols used to perform operation on the data.

• Arithmetic operators +, -, *, /, %• Increment and Decrement ++, --

• Prefix and Postfix notation

• In prefix, the compiler will increment the variable and then will use it.

• In postfix, the compiler will first use and then increment it.

• Arithmetic Assignment Operators S+=, -=, *=, /=, %=• Relational Operators ==, !=, >, <, >=, <=• Logical and Bitwise Operators&, |, ^, !, &&, ||• Other operators <<, >>, ., [], (), ? :

Page 16: CS4443 - Modern Programming Language - I  Lecture (2)

Expressions• C# contains number of operators for this purpose.• By combining operators with variables and literal value (together referred as

operands), you can create expressions, which is the building blocks of computation.

Page 17: CS4443 - Modern Programming Language - I  Lecture (2)

Operator Precedence

Page 18: CS4443 - Modern Programming Language - I  Lecture (2)

Flow Control and Conditional StatementsThe if Statement

Page 19: CS4443 - Modern Programming Language - I  Lecture (2)

Flow Control and Conditional StatementsThe if…….else….. Statement

Nested if and Nested if-else

Page 20: CS4443 - Modern Programming Language - I  Lecture (2)

Flow Control and Conditional StatementsThe switch……..case Statement

Page 21: CS4443 - Modern Programming Language - I  Lecture (2)

Flow Control and Conditional StatementsThe for loop Statement

Use of Continue and Break Statement.

Page 22: CS4443 - Modern Programming Language - I  Lecture (2)

Flow Control and Conditional StatementsThe do……while Loop Statement

Page 23: CS4443 - Modern Programming Language - I  Lecture (2)

Flow Control and Conditional StatementsThe while Loop Statement

Page 24: CS4443 - Modern Programming Language - I  Lecture (2)

Flow Control and Conditional StatementsThe goto Statement

Page 25: CS4443 - Modern Programming Language - I  Lecture (2)

Type Conversion• There are two types of type conversion:• Implicit and Explicit conversion

• Implicit conversion requires no work on your part and no additional code.

Page 26: CS4443 - Modern Programming Language - I  Lecture (2)

Type Conversion

Page 27: CS4443 - Modern Programming Language - I  Lecture (2)

Type Conversion• As the name suggests, an explicit conversion occurs when you

explicitly ask the compiler to convert a value from one data type to another. • Requires extra code, and format of this code may vary, depending on the

exact conversion method.

Page 28: CS4443 - Modern Programming Language - I  Lecture (2)

Type Conversion• Before working with explicit conversion, try the code below:

Page 29: CS4443 - Modern Programming Language - I  Lecture (2)

Type Conversion• To get the code compile, you need to add some code.• in this context, you have to cast the short variable into a byte (as suggested by

the preceding error string).

• Casting basically means forcing data from one type into another, and it uses the following simple syntax:

<(destinationType)sourceVar>

Page 30: CS4443 - Modern Programming Language - I  Lecture (2)

Type Conversion

Page 31: CS4443 - Modern Programming Language - I  Lecture (2)

Type Conversion

Page 32: CS4443 - Modern Programming Language - I  Lecture (2)

Type Conversion• Attempting to fit a value into a variable when that value is too big for

the type of that variable results in an overflow, and this is the situation you want to check for.

• Two keywords exist for setting what is called the overflow checking context for an expression: checked and unchecked. checked (<expressions>)

unchecked (<expressions>)

Page 33: CS4443 - Modern Programming Language - I  Lecture (2)

Type Conversion

Page 34: CS4443 - Modern Programming Language - I  Lecture (2)

Type Conversion• You can also use Convert command to make explicit conversion.

Page 35: CS4443 - Modern Programming Language - I  Lecture (2)

String Manipulation• Writing strings to console, reading strings from the console, and

concatenating strings using the + operator.

• A string type variable can be treated as a read-only array of char variables.• You can access individual characters using syntax like the following:

Page 36: CS4443 - Modern Programming Language - I  Lecture (2)

String Manipulation• To get a char array you have to use ToCharArray() command of

the array variable.

• Later, you can manipulate it as normal array of characters.

• You can access the number of elements using myString.Length.

Page 37: CS4443 - Modern Programming Language - I  Lecture (2)

String Manipulation• You can work with other operations like <string.ToUpper()>

and <string.ToLower()> and etc…

• As it not changes the original value so it’s better to assign it into another variable again.

Page 38: CS4443 - Modern Programming Language - I  Lecture (2)

String Manipulation• For removing extra spaces for ease of

interpretation so you can use <string>.Trim() command.

• You can also specify which string to trim from the string.

• You can work <string>.TrimStart() and <string>.TrimEnd()• Spaces from the beginning and end of the string.

• You can use <string>.PadLeft() and <string>.PadRight().

Page 39: CS4443 - Modern Programming Language - I  Lecture (2)

Complex Variable Types• Enumeration (often referred to as enum), structs (referred to as

structures) and arrays.

• Enumeration allow the definition of a type that can take one of a finite set of values that you supply.• Orientation example – creating enum type called orientation.

• It creates a user-defined type and then it is applied to a variable.

Page 40: CS4443 - Modern Programming Language - I  Lecture (2)

Complex Variable Types• You can use enum keyword to define

enumeration as given.

• Next, you can declare variable of this new type.

• You can assign values using the given syntax.

Page 41: CS4443 - Modern Programming Language - I  Lecture (2)

Complex Variable Types• structs are data structures

that are composed of several pieces of data, possibly of different types.

• You can use struct keyword to define structure as given.

Page 42: CS4443 - Modern Programming Language - I  Lecture (2)

Arrays• Situations where you want to store a lot of data, so you have to

declare individual variables as

• Arrays are indexed lists of variables stored in a single array type variable.• An array called friendNames that stores the three names (elements) and can

be accessed through number (index) in square brackets, as shown:

Page 43: CS4443 - Modern Programming Language - I  Lecture (2)

Arrays• Arrays are declared in the following way:

• Arrays can be initialized in two ways:

Page 44: CS4443 - Modern Programming Language - I  Lecture (2)

Arrays• Alternative ways

Page 45: CS4443 - Modern Programming Language - I  Lecture (2)

foreach Loop• A foreach loop enables you to address each element in an array

using this simple syntax:

• This loop will cycle through each element, placing it in the variable <name> in turn, without danger of accessing illegal elements.

• It gives you read-only access to the array contents.

Page 46: CS4443 - Modern Programming Language - I  Lecture (2)

foreach Loop

Page 47: CS4443 - Modern Programming Language - I  Lecture (2)

Summery• Basic Data Types and their Mapping to CTS

• Variables, Constants, and Operators

• Working with Flow Control and Conditional Statements

• Type Conversion, String Manipulation and Complex Variable Types

• Arrays in C#

• foreach loop

Page 48: CS4443 - Modern Programming Language - I  Lecture (2)

Thank YouFor your Patience