Introduction to C#.net PROF. S. LAKSHMANAN,

Slides:



Advertisements
Similar presentations
ICE1341 Programming Languages Spring 2005 Lecture #13 Lecture #13 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Advertisements

C# Language Report By Trevor Adams. Language History Developed by Microsoft Developed by Microsoft Principal Software Architect Principal Software Architect.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
Introduction to Programming G51PRG University of Nottingham Revision 2 Essam Eliwa.
JAVA Control Statement.
UNIT II Decision Making And Branching Decision Making And Looping
L EC. 03: C ONTROL STATEMENTS Fall Java Programming.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
Fundamentals of C and C++ Programming Control Structures and Functions.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
DiagrammaticRepresentation Iteration Construct False True Condition Exit from Statement (s) loop Sequence construct Selection construct Statement 1 Statement.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
C# Programming Fundamentals Control Flow Jim Warren, COMPSCI 280 S Enterprise Software Development.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
Controlling Execution Dong Shao, Nanjing Unviersity.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX.
J AVA P ROGRAMMING 2 C H 03: C ONTROL STATEMENTS if, for loop (review) switch, while, do while break, continue Fall Java Programming.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Session 1 C# Basics.
Application development with Java Lecture 6 Rina Zviel-Girshin.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 8 Java Fundamentals Control Structures Fri.
Control Flow Statements
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Unit – 3 Control structures. Condition Statements 1.If.…..else :- Has someone ever told you, "if you work hard, then you will succeed"? And what happens.
1 Statements © University of Linz, Institute for System Software, 2004 published under the Microsoft Curriculum License.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Module 6 – Decision Control Statements Objectives  Understands Increment/Decrement operators, Conditional and special operators in C  Understands significance.
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
Information and Computer Sciences University of Hawaii, Manoa
Branching statements.
Core Java Statements in Java.
‘C’ Programming Khalid Jamal.
Chapter 7 User-Defined Methods.
Chapter 6: Loops.
REPETITION CONTROL STRUCTURE
Def: A control structure is a control statement and
Statements (6 of 6) A statement causes an action to be performed by the program. It translates directly into one or more executable computer instructions.
Lecture 7: Repeating a Known Number of Times
Yanal Alahmad Java Workshop Yanal Alahmad
Java Primer 1: Types, Classes and Operators
Chapter 5: Control Structures II
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Hector Cen Fall 2017
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
Programming Fundamentals
Chapter 8: Control Structures
Programmazione I a.a. 2017/2018.
CS360 Windows Programming
11/10/2018.
Objectives Explain selection constructs Describe loop constructs
Arrays, For loop While loop Do while loop
CET 3640 – Lecture 2 Java Syntax Chapters 2, 4, 5
Lecture 3 Functions Simple functions
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I UTPA – Fall 2012 This set of slides is revised from lecture slides.
Starting JavaProgramming
Java - Data Types, Variables, and Arrays
Chapter 7 Additional Control Structures
Structured Program
CISC124 Labs start this week in JEFF 155. Fall 2018
C Programming Getting started Variables Basic C operators Conditionals
Methods.
C – Programming Language
Loops CGS3416 Spring 2019 Lecture 7.
Introduction to C Programming
Presentation transcript:

Introduction to C#.net PROF. S. LAKSHMANAN, DEPT. OF B. VOC. (SD & SA), ST. JOSEPH'S COLLEGE.

WHAT IS C# ? C# is an object oriented programming language developed by Microsoft. That intends to simple, modern and general purpose programming language for application development. It is a combination of c++ and java. Using is the keyword. We can develop c# both in windows and console applications.

Input Statement ReadLine() – used for input. ReadKey() Read the character form keyboard Read() Output Statement Write()- used to print in the same line. WriteLine()- used to print in the next line. WriteError()-Used to print in the error statement

VALUE TYPE VARIABLES Value can be stored in variables by two methods which are byvalue the Value type store the supplied value directly. EXAMPLE: int num; num=5;

by reference Reference type store a reference to the memory where the actual value is located. EXAMPLE: string str=“HELLO”; 0 1 2 3 4 H E L O

Pre-defined reference Type Data Types Data Type Reference Type Value Type Pre-defined Value Type int,float ,char boolean,string User-defined Value Type Structure, Enum Pre-defined reference Type Object, String User-defined reference Type Class,Array,Delegate Interface

Data Types and Sizes Type Meaning Size (bytes) Size (bits) char a single byte, capable of holding one character 2 byte 16 bits int an integer 4 bytes 32 bits float single-precision floating point number Double double-precision floating point number 8 bytes 64 bits String Depends on the length of string

Binary representation Qualifier Size (bytes) Size (bits) ushort int 2 bytes 16 bits long int 8 bytes 64 bits long double 16 bytes 128 bits

Example program Hello world using System; namespace sample { class prg Example program Addition of two number using System; namespace add { class prg static void Main(string[] args) int a,b,total; Console.WriteLine("Enter the number A="); // for same as printf statement relative c lang’ a=int.Parse(Console.ReadLine()); // for same as scanf statement relative c lang’ Console.WriteLine("Enter the number B="); b=int.Parse(Console.ReadLine()); total=a+b; Console.WriteLine("Addtiton="+total); Console.Read(); } Example program Hello world using System; namespace sample { class prg static void Main(string[] args) Console.WriteLine(“Hello World"); Console.Read(); }

Boxing Boxing a value type packages it inside an instance of the Object reference type. This allows the value type to be stored on the garbage collected heap. Conversion of value type on the stack to reference type on the heap. Example: int i = 123; object om =i; // boxing Console.WriteLine(“integer value=“+i); Console.WriteLine(“Object value=“+om);

Unboxing Conversion of reference type on the heap to value type on the stack Unboxing extracts the value type from the object. In this example, the integer variable i is boxed and assigned to object om. Example int i=123; object om=i; //box i int n=(int)om; // unboxing om back to an int

Example program for Boxing-Unboxing using System; namespace ConsoleApplication4 { class Program static void Main(string[] args) int i = 10; Console.WriteLine(" i= {0} ", i); Object obj = (Object)i; // Boxing Console.WriteLine(" obj= {0} ", obj); Console.WriteLine(" obj type= {0} ", obj.GetType()); string i1 = "Hello"; Console.WriteLine(" i= {0} ", i1); Object obj1 = (Object)i1; // Boxing Console.WriteLine(" obj= {0} ", obj1); Console.WriteLine(" obj type= {0} ",obj1.GetType()); int j = (int)obj; // Unboxing Console.WriteLine("j= {0} ", j); Console.Read(); } } }

conversion Type of conversion Implicit conversion(Arithmetic operation) Explicit conversion(casting operation) Implicit conversion An implicit conversion is also known as automatic type conversion. The process of assigning a smaller type to large one is known as widening. Explicit conversion The process of assigning a large type to smaller one is known as narrowing. SYNTAX (destination type) Source variable;

public static void Main() Implicit conversion Explicit conversion Class sample { public static void Main() ushort dest; char source=‘b’; dest=source; Console.WriteLine(“Source={0}”,source); Console.WriteLine(“Dest={0}”,dest); Console.ReadLine(); } int dest=10; byte source; source=(byte) dest;

Function A function allows you to encapsulate a piece of code and call it from other parts of your code. it is building a block of C# programs It can be invoked from program other parts the program. Syntax: <visibility><return type> <name>(<parameters>) { <function code> }

Needs of the function Reduces the program size When a program with large, a single list of instruction becomes difficult to understand so we can group the codes as single unit knows as methods It is easy to understand the particular task We can call a function any number of times Function with argument arguments or parameter function are the data that the function receives when call function other function Value parameter Reference parameter Output parameter Params Array

Value parameter Value of an argument is passed to a method & changes way to a parameter do not affect then original value(call by value). Example: using System; Class sample { public static void F1(int val) val++; } public static void main() { int a1=10; Console.WriteLine(“Before calling={0}”,a1); F1 (a1); Console.WriteLine(“After calling={0}”,a1); Console.ReadLine(); }

Reference parameter While passing the reference argument a reference to variable in calling program is passed the parameter does not itself define a variable but refer of corresponding argument it can be declared using keyword as ref public static void main() { int a1=10; Console.WriteLine(“Before calling={0}”,a1); F1(ref a1); Console.WriteLine(“After calling={0}”,a1); Console.ReadLine(); } Example: using System; Class sample { public static void F1(ref int val) val++; }

output parameter It is same as reference parameter except the initial value of the argument is an important it is declared using out keyword Example: using System; Class sample { public static void F1(int a,int b, out int c) c=a+b; } public static void main() { int a,b,c; Console.WriteLine(“Enter a&b Number”); a=int.Parse(Console.ReadLine()); b=int.Parse(Console.ReadLine()); F1(a,b,out c); Console.WriteLine(“{0}+{1}={2}”,a,b,c); Console.ReadLine(); }}

Params array It is used for passing number of variable a method it can take only one single dimensional array as parameter. it can be declare using params Keyword public static void Main() { F1(10); F1(10,20); F1(10,20,30,45); Console.ReadLine(); } } Example: using System; Class sample { public static void F1( params int [] arr) Console.WriteLine(“Array Elements are”); foreach(int x in arr) Console.WriteLine(“value”+x); Console.ReadLine(); }

Return value the function can return a single value to a calling program which can be travel data type return value has to be specified when definition of function. return keyword is used to return a value to calling function Data type void indicate a function return nothing .

Example program using System; Calss sample { public static int F1(int a,int b) { int c=a+b; return c; } public static void Main() { int a,b,c; Console.writeLine(“Enter a & b Number”); a=int.Parse(Console.ReadLine()); b=int.Parse(Console.ReadLine()); c=F1(a,b); Console.WriteLine(“result{0}”,c); Console.ReadLine(); }

Conditional Statements Normally statement are executed sequentially that is one after the other But we may like to change the order of execution in the statement based on particular condition. Repeat the group of statement until certain satisfied.

If Statement if (condition) code to be executed if condition is true; Other wise exit the program SYNTAX {  code to be executed if condition is true; }

flow chart if (condition) Statement Exit False True

Example program for if Statement using System; namespace ConsoleApplication3 { class Program static void Main(string[] args) int n; Console.WriteLine("Enter number"); n = int.Parse(Console.ReadLine()); if (n>=0) Console.WriteLine("positive"); } Console.ReadLine();

If … else Statement Use the if....else statement to execute some code if a condition is true and another code if a condition is false. Syntax if (condition)   code to be executed if condition is true; else   code to be executed if condition is false;

flow chart True False True Statement False Statement Exit if (condition) True Statement Exit False Statement

Example program for if else Statement using System; namespace ConsoleApplication3 { class Program static void Main(string[] args) int n; Console.WriteLine("Enter number"); n = int.Parse(Console.ReadLine()); if (n >= 0) Console.WriteLine("positive"); } else Console.WriteLine("Negative"); Console.ReadLine();

Nested If-else Statement if condition is true be executed another condition ; if condition is true to execute some code if a condition is true and another code if a condition is false. Other wise execute main else block statement SYNTAX if (condition1) { if (condition2)  code to be executed if condition is true; } else { code to be executed if condition is false; }

True False if (condition) Statement Exit flow chart

else { Console.WriteLine("C Biggest" + c); } if (b > c) Console.WriteLine(" B Biggest" + b); Console.WriteLine(" c Biggest" + c); Console.ReadLine(); Example program for Nested if Statement using System; namespace ConsoleApplication3 { class Program static void Main(string[] args) int a, b, c; Console.WriteLine("Enter number"); a = int.Parse(Console.ReadLine()); b = int.Parse(Console.ReadLine()); Console.WriteLine("Enter number"); c = int.Parse(Console.ReadLine()); if (a>b) if (a > c) Console.WriteLine("A Biggest" + a); }

If…elseif…else Statement Syntax if (condition)    code to be executed if condition is true; elseif (condition)    code to be executed if condition is true; else    code to be executed if condition is false;

flow chart False Statement True False Statement Exit if (condition) if else (condition) Exit Statement if (condition) flow chart if else (condition) False Statement

Example program for if else Statement using System; namespace ConsoleApplication3 { class Program static void Main(string[] args) int a, b, c; Console.WriteLine("Enter number"); a = int.Parse(Console.ReadLine()); b = int.Parse(Console.ReadLine()); c = int.Parse(Console.ReadLine()); if (a > b && a > c) { Console.WriteLine(" Biggest A =" + a); } else if (a == b && b == c && c == a) Console.WriteLine("All are equals"); else if (b > c) Console.WriteLine(" Biggest B=" + b); else Console.WriteLine(" Biggest c=" + c); Console.ReadLine();

Switch Statement Used to select one of many blocks of code to be executed. switch (n) { case label1:    code to be executed if n=label1;    break; case label2:    code to be executed if n=label2;    break; default:  code to be executed if n is different from both label1 and label2; }

flow chart Exit Switch Case 1 Statement Case2 Default

Console.WriteLine("Enter Choice"); choice = int.Parse(Console.ReadLine()); switch (choice) { case 1: result = a + b; Console.WriteLine("addition value" + result); break; case 2: result = a - b; Console.WriteLine(" Sub value" + result); case 3: result = a * b; Console.WriteLine(" Mul value" + result); case 4: result = a / b; Console.WriteLine(" div value" + result); default: Console.WriteLine("Invalid choice"); } Example program for switch Statement using System; namespace ConsoleApplication4 { class Program static void Main(string[] args) int a, b, result, choice, v; prm:// Label Statement do Console.WriteLine("Enter number"); a = int.Parse(Console.ReadLine()); b = int.Parse(Console.ReadLine()); Console.WriteLine("1.Add"); Console.WriteLine("2.Sub"); Console.WriteLine("3.mul"); Console.WriteLine("4.div");

Console.WriteLine(" do u want continue press 1 or 0 extit"); v = int.Parse(Console.ReadLine()); if (v == 1) { goto prm; } else if(v==0) break; while (choice <= 4); Console.ReadLine();

Looping in C# In looping structure the sequence of statement are executed until some conditions for the termination of the loop are satisfied. A program loop consists of two segments for body of Loop. Entry control Loop. Exit control Loop. Entry control Loop In entry control loop the conditions are tested before the start of the loop execution. If the conditions are not satisfied then the body of the loop will not be executed Exit control loop The case of exit control loop the condition is tested only at the end at the body is executed unconditionally one time.  

While Loop The while loop executes a block of code while a condition is true. Syntax while (condition)  {   code to be executed;  }

While Loop Flow chart while False True Exit Initialization Body of the Loop while condition Exit False True

Example Program for while using System; namespace ConsoleApplication3 { class Program static void Main(string[] args) int i = 1, n; Console.WriteLine("Enter number"); n = int.Parse(Console.ReadLine()); while (i <= n) Console.WriteLine("Natural number:" + i); i++; } Console.ReadLine();

do…while Loop The do...while statement will always execute the block of code once, it will then check the condition, and repeat the loop while the condition is true. Syntax do   {   code to be executed;   } while (condition);

do While Loop Flow chart Body of the Loop while condition Initialization Exit False True

Example Program for Do while using System; namespace ConsoleApplication3 { class Program static void Main(string[] args) int i = 1, n; Console.WriteLine("Enter number"); n = int.Parse(Console.ReadLine()); do Console.WriteLine("Natural number:" + i); i++; } while (i <= n); Console.ReadLine();

for…Loop Syntax for (init; condition; increment/decrement)   {   code to be executed;   } Parameters: init: Mostly used to set a counter (but can be any code to be executed once at the beginning of the loop) condition: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends. increment: Mostly used to increment a counter (but can be any code to be executed at the end of the loop) Note: Each of the parameters above can be empty, or have multiple expressions (separated by commas).

For Loop Flow chart for True False Exit Initialization Body of the Loop for condition Increment/decrement Initialization False True Exit

Example Program in for loop using System; namespace ConsoleApplication3 { class Program static void Main(string[] args) int i, n; Console.WriteLine("Enter number"); n = int.Parse(Console.ReadLine()); for(i=1; i<=n; i++) Console.WriteLine("Natural number:" + i); } Console.ReadLine();

foreach loop The foreach loop is used to loop through arrays. Syntax foreach ( data type variable in collection)   {    code to be executed;   } For every loop iteration, the value of the current array element is assigned to collection(or )list (and the array pointer is moved by one) - so on the next loop iteration, you'll be looking at the next array value.

Example program for each Loop using System; namespace ConsoleApplication3 { class Program static void Main(string[] args) int i, n; int[] a = new int[10]; Console.WriteLine("Enter number"); n = int.Parse(Console.ReadLine()); for(i=0; i<=n; i++) a[i] = int.Parse(Console.ReadLine()); } foreach ( int x in a) { Console.WriteLine("The Array elements:" + x); } Console.ReadLine();

Conditional Jumping break 2.continue Break The break statement can be used with in while dowhile,for and switch statement. SYNTAX: break; When break statement is encountered with in the body of loop, the execution of loop is interrupted(Exit) And the program control is transfer to exit point of loop Then the program continues with the statement immediately following the loop

// break example program using System; namespace ConsoleApplication3 { class Program static void Main(string[] args) int i, n; Console.WriteLine("Enter number"); n = int.Parse(Console.ReadLine()); for(i=1; i<=n; i++) if (i == 5) Console.WriteLine(" Welcome"); break; } Console.WriteLine("number" + i); Console.ReadLine();

The continue statement is another loop interruption statement SYNTAX: continue; It does not terminate the loop instead of it only interrupts a particular iteration. (i.e.) if caused loop to be continued with next iteration after skipping some statement The continue statement can be evaluated while, do while ,for loop all statement in body of loop following statement or skipping

// continue example using System; namespace ConsoleApplication3 { class Program static void Main(string[] args) int i, n; Console.WriteLine("Enter number"); n = int.Parse(Console.ReadLine()); for(i=1; i<=n; i++) if (i == 5) continue; } Console.WriteLine("number" + i); Console.ReadLine();

Un Conditional Jumping go to the go to statement is unconditional looping statement General form Backward: go to statement _Label; Label: Backward Statement; Forward: Label: Statement; Forward

The goto statement is used to alter the normal sequence of program execution by transferring control to some other part of program Where label: is identifier of statement to which branching an appears in same function in form label: statement The statement can have same labels the execution of goto statement transferred program control to the statement indicated by the label these statement is executed next.

// goto example program using System; namespace ConsoleApplication3 { class Program static void Main(string[] args) int i, n; Console.WriteLine("Enter number"); n = int.Parse(Console.ReadLine()); for(i=1; i<=n; i++) if (i == 5) goto Error; } Error: Console.WriteLine(" Welcome"); Console.WriteLine("number" + i); Console.ReadLine();

Thank You.