Basic Programming
Basic Programming
Basic Programming
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#
.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 + * / % ++ --
Operators Contd
Prefix and Postfix notation Both the ++ and -operators can be used as prefix or postfix operators
Sample Code
Operators Contd
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.
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.
Operators Contd
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
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
-=
14
/=
14
%=
14
>>=
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.
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.
15
16
17
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