Anda di halaman 1dari 24

C# - Basic Programming

www.1000projects.com www.chetanasprojects.com www.campusmiles.com

Course Topics
1. 2. 3. 4. 5. 6. 7. Basic Data Types and their mapping to CTS Operators Variables Arrays in C# Flow Control and Conditional Statements Switch case statement Loops in C#

Basic Data Types


 There are 2 kinds of data types in C#  Value Types implicit data types, structs and enumeration)  Reference Types objects, delegates
C# Type bool char Integral Types byte sbyte short ushort int uInt long ulong Floating Point Types float double decimal Single Double decimal
3

.NET Type Boolean Char Byte SByte Int16 UInt16 Int32 UInt32 Int64 UInt64

Operators
 Arithmetic Operators  Several common arithmetic operators are allowed in C#
Existing Operators

Operand + * / % ++ --

Description Add Subtract Multiply Divide Reminder or Modulo Increment by 1 Decrement by 1

Operators Contd
 Prefix and Postfix notation  Both the ++ and -operators can be used as prefix or postfix operators
Sample Code

Operators Contd


Assignment Operators  Assignment operators are used to assign values to variables.

Common assignment operators in C# are:

Operand = += -= *= /= %=

Description Simple assignment Additive assignment Subtractive assignment Multiplicative assignment Division assignment Modulo assignment

Operators Contd


Relational Operators  Relational operators are used for comparison purposes in conditional statements.

Common relational operators in C# are:

Operand == != > < >= <=

Description Equality check Un-equality check Greater than Less than Greater than or equal to Less than or equal to

Operators Contd...


Logical & Bitwise Operators  These operators are used for logical and bitwise calculations.

Common logical and bitwise operators in C# are:


Operand & | ^ ! && || Description Bitwise AND Bitwise OR Bitwise XOR Bitwise NOT Logical or short circuit AND Logical or short circuit OR

Operators Contd


Other Operators  There are some other operators present in C#.

A short description of these is given below:

Operand << >> . [] () ?:

Description Left Shift Bitwise Operator Right Shift Bitwise Operator Member access for Objects indexing operator used in arrays and collections Cast operator Ternary operator

Operators Contd


Operator Precedence  All operators are not treated equally. There is a concept of "operator precedence" in C#  A*B + C-D/E
Step 1 : A*B Step 2 : D/E Step 3 : (A*B) + C Step 4 : ((A*B) + C) (D/E)

Name Parentheses Post-increment Post-decrement Address Bitwise NOT Typecast Logical NOT Negation Plus Sign Pre-increment Pre-decrement Size of data Modulus Multiplication Division Addition Subtraction Bitwise Shift Left Bitwise Shift Right

Operator () ++ -& ~ (type) ! + ++ -sizeof % * / + << >>

Direction Left -> Right Left -> Right Left -> Right Right -> Left Right -> Left Right -> Left Right -> Left Right -> Left Right -> Left Right -> Left Right -> Left Right -> Left Left -> Right Left -> Right Left -> Right Left -> Right Left -> Right Left -> Right Left -> Right

Precedence 1 2 2 2 2 2 2 2 2 2 2 2 3 3 3 4 4 5 5

10

Contd
Name Less Than Less Than or Equal Greater Than Greater Than or Equal Equal Not Equal Bitwise AND Bitwise XOR Bitwise OR Logical AND Logical OR Condition Expression Assignment Operator < <= > >= == != & ^ | && || ?: = Direction Left -> Right Left -> Right Left -> Right Left -> Right Left -> Right Left -> Right Left -> Right Left -> Right Left -> Right Left -> Right Left -> Right Right -> Left Right -> Left Precedence 6 6 6 6 7 7 8 9 10 11 XOR Assignment 12 OR Assignment 13 14 Comma ^= , Right -> Left Left -> Right 14 15 |= Right -> Left 14 Right Shift Assignment AND Assignment <<= &= Right -> Left Right -> Left 14 14 Multiplicative Assignment Divisional Assignment Modulating Assignment Left Shift Assignment *= Right -> Left 14 Name Additive Assignment Subtractive Assignment Operator Direction Precedence += Right -> Left 14

-=

Right -> Left

14

/=

Right -> Left

14

%=

Right -> Left

14

>>=

Right -> Left

14

11

Variables


A variable is the name given to a memory location holding a particular type of data. So, each variable has associated with it a data type and a value.

Sample code

12

Constant Variables


 

Constants are variables whose values, once defined, can not be changed by the program. Constant variables are declared using the const keyword const. Constant variables must be initialized as they are declared.

Sample code

13

Naming Conventions


Microsoft suggests using Camel Notation (first letter in lowercase) for variables and Pascal Notation (first letter in uppercase) for methods. Each word after the first word in the name of both variables and methods should start with a capital letter.

Sample Variable Declaration

14

Arrays in C#
 

An Array is a collection of values of a similar data type. Technically, C# arrays are a reference type. Each array in C# is an object and is inherited from the System.Array class. The size of an array is fixed and must be defined before using it. You can optionally do declaration and initialization in separate steps.

Sample code: Syntax:


<data type> [] <identifier> = new <data type>[<size of array>];

15

Accessing the values stored in an array


 To access the values in an Array, we use the indexing operator [int index].  It's important to note that index values in C# start from 0.
Sample code

16

Flow Control and Conditional Statements


 If else statement  Condition checking has always been the most important construct in any language right from the time of the assembly language days. C# provides conditional statements in the form of the if...else statement.
Sample code:

17

Switch Case Statement


 If you need to perform a series of specific checks, switch...case is present in C# just for this.  It takes less time to use switch...case than using several if...else if statements.
Sample code

18

Loops in C#
 Loops are used for iteration purposes, i.e., doing a task multiple times (usually until a termination condition is met)  for Loop The most common type of loop in C# is the for loop.
Sample code

19

Loops in C# Contd
 do...while Loop Sample code:  The statements in a do...while() loop always execute at least once.  There is a semicolon ; after the while statement.

20

Loops in C# Contd
 while Loop The while loop is similar to the do...while loop, except that it checks the condition before entering the first iteration (execution of code inside the body of the loop).
Sample code

21

Loops in C# Contd
 foreach Loop  There is another type of loop that is very simple and useful to iterate through arrays and collections. This is the foreach loop.
Sample code

22

Exercises
1. 2. 3. Find out the biggest number out of N numbers (without using Arrays) Display entered year is Leap year or not? Create a console based calculator program with the following menu options: 1. Addition 2. Subtraction 3. Division 4. Multiplication 5. Exit. The program should accept two numbers and also a choice from the user as an input and displays the result based on the input. Accept an input from the user and display ASCII value Accept a character from the user and display the character uppercase to lowercase and vice-versa Accept 3 digit number and display in reverse order Accept N numbers from the user and display the numbers in Ascending and Descending Order (use Arrays) Create a console based program to work with strings with the following menu options: 1. String length 2. String Reverse 3. String Comparison 4. Strings Concatenation. 5. Change Case 6. Exit
23

4. 5. 6. 7. 8.

References
URL : http://msdn2.microsoft.com/en-us/library/ms173104.aspx http://msdn2.microsoft.com/en-us/library/9b9dty7d.aspx http://msdn2.microsoft.com/en-us/library/ms173142.aspx

Basic_Programming

24

Anda mungkin juga menyukai