By: Md Rezaul Huda Reza
UNIVERSITY of South Asia 2 Md Rezaul Huda Reza UNIVERSITY of South Asia 2 2 Program written in any.NET supported language(s) (e.g. VB.NET, C#, J# etc) Program written in any.NET supported language(s) (e.g. VB.NET, C#, J# etc) Common Intermediate Language (CIL) (.exe or.dll) Common Intermediate Language (CIL) (.exe or.dll) compile Common Language Runtime Executes code, garbage collects etc Common Language Runtime Executes code, garbage collects etc execute
UNIVERSITY of South Asia 3 Md Rezaul Huda Reza UNIVERSITY of South Asia 3 A command that performs an action They are found inside methods A method is a named sequence of statements Must follow a set of rules = syntax Example of syntax rules: You must terminate all statements with “;” The specification of what statements do is known as semantics The trick to good programming is learning its syntax and semantics Extra reading: Syntax vs semantics: what's the difference? Syntax vs semantics: what's the difference? 3
UNIVERSITY of South Asia 4 Md Rezaul Huda Reza UNIVERSITY of South Asia 4 Identifiers are names of various program elements in the code that uniquely identify an element. names of things like variables or fields. specified by the programmer. Keywords are reserved words in the C# language. they can't be used as identifiers. Examples : class, public, or void 4
UNIVERSITY of South Asia 5 Md Rezaul Huda Reza UNIVERSITY of South Asia 5 A Variable represents a storage location that holds a value. A “box” holding temporary information Has a unique name The value of a variable can be changed through assignment or through use of the ++ and -- operators. A variable must be definitely assigned before its value can be obtained. 5
UNIVERSITY of South Asia 6 Md Rezaul Huda Reza UNIVERSITY of South Asia 6 Combination of alphabetic characters (a–z, and A–Z), numeric digits (0–9), and the underscore ( _ ) The name must start with a letter or an underscore No embedded spaces concatenate (append) words together Keywords cannot be used Be descriptive with meaningful names Eg footballTeamScore Convention: start with lower case 6
UNIVERSITY of South Asia 7 Md Rezaul Huda Reza UNIVERSITY of South Asia In-Line Comments (//) // This is traditionally the first program written. Everything to the right of the slashes ignored by the compiler. Carriage return (Enter) ends the comment Multi-line Comment (/* */) block comments /* This is the beginning of a block multi-line comment. It can go on for several lines or just be on a single line. */ Example: int x = 3 // this is z comment stating that a variable of type integer was //declared and initialized to 3 7 E. I. Teodorescu
UNIVERSITY of South Asia 8 Md Rezaul Huda Reza if (boolExpression) { statement; } 8 Either the true statement(s) executed or the false statement(s) — but not both if (boolExpression) { statement; } else { statement; } if (boolExpression) { statement; } else { statement; } One-way Selection Statement Two-way Selection Statement Curly brackets are required with multiple statements Try should use them even if you have one statement. Why?
UNIVERSITY of South Asia 9 Md Rezaul Huda Reza UNIVERSITY of South Asia 9 int seconds ; if (seconds == 59) { seconds =0; } else { seconds++; } E. I. Teodorescu 9 // Note : == is different than = if (seconds = 59) { // Compile error seconds =0; } else …... //Note : you can use Boolean var as an expresion bool isReady; if ( isReady == true) // OK ….. if (isReady) // OK …... What is the difference between “==“ and “=“ ?
UNIVERSITY of South Asia 10 Md Rezaul Huda Reza UNIVERSITY of South Asia 10 You can nest if statements inside other if statements if(Condition1) Statement1; else if(Condition2) Statement2; else if(Condition3) Statement3; else Statement-n; E. I. Teodorescu 10
UNIVERSITY of South Asia 11 Md Rezaul Huda Reza UNIVERSITY of South Asia // some code before age = int.Parse(textBox1.Text); if (age<18) { label1.Text = "to young to drink!"; } else if(18<=age and age<65) { label1.Text = “Work hard, Party Hard"; } else if(65<=age and age<100) { label1.Text = “Easy on Work, Still Party Hard"; } else { label1.Text = “Wow! Tell me your secret"; } E. I. Teodorescu 11
UNIVERSITY of South Asia 12 Md Rezaul Huda Reza UNIVERSITY of South Asia 12 Useful because you can repeat instructions with many data sets repetition or iteration structures statements are executed in order, except when a jump statement is encountered. keywords are used in iteration statements: while do for foreach – later, when we study collections in E. I. Teodorescu 12
UNIVERSITY of South Asia 13 Md Rezaul Huda Reza UNIVERSITY of South Asia 13 while (conditional expression){ statement(s); // the while body } The while statement executes a statement or a block of statements until a specified expression evaluates to false No semicolon after the conditional expression the test of expression takes place before each execution of the loop a while loop executes zero or more times. infinite loop = BAD E. I. Teodorescu 13
UNIVERSITY of South Asia 14 Md Rezaul Huda Reza Post test do statement is executed at least once regardless of the value of the expression The do statement executes a statement or a block of statements repeatedly until the expression evaluates to false 14 do { statement; } while( conditional expression); do { statement; } while( conditional expression);
UNIVERSITY of South Asia 15 Md Rezaul Huda Reza UNIVERSITY of South Asia 15 int y = 0; string s ; while (y < 3) // No semicolon { s= “Number: “ + y ; y++ ; } What is the value of s at the end of the loop? E. I. Teodorescu 15 int y = 0; string s ; do // No semicolon on this line { s= “Number: “ + y ; y++ ; } while (y < 3); What happens in the do…while loop?
UNIVERSITY of South Asia 16 Md Rezaul Huda Reza UNIVERSITY of South Asia 16 int y = 3; string s ; while (y < 3) // No semicolon { s= “Number: “ + y ; y++ ; } What is the value of s at the end of the loop? E. I. Teodorescu 16 int y = 3; string s ; do // No semicolon on this line { s= “Number: “ + y ; y++ ; } while (y < 3); What is the value of s at the end of the loop?
UNIVERSITY of South Asia 17 Md Rezaul Huda Reza UNIVERSITY of South Asia 17 executes a statement or a block of statements repeatedly until a specified expression evaluates to false. Usually associated with counter-controlled types packages initialization, test, and update all on one line General form is: for (initializers; conditional expression; iterator) { statement; } Interpreted as: for (initialize; test; update) statement E. I. Teodorescu 17
UNIVERSITY of South Asia 18 Md Rezaul Huda Reza UNIVERSITY of South Asia 18 The for statement executes the statement repeatedly as follows: First, the initializers are evaluated. Then, while the expression evaluates to true, the statement(s) are executed and the iterators are evaluated. When the expression becomes false, control is transferred outside the loop. Example E. I. Teodorescu 18 for(i = 0; i<5; i++) { s = s + i + “\n”; }
UNIVERSITY of South Asia 19 Md Rezaul Huda Reza UNIVERSITY of South Asia 19 Loop can be nested inside an outer loop Inner nested loop is totally completed before the outside loop is tested a second time Example: int inner; string s = “ “; for (int outer = 0; outer < 3; outer++) { for(inner = 10; inner > 5; inner --) { s = s + outer + “ “ + inner + “\n”; } MessageBox.Show(s); E. I. Teodorescu 19
UNIVERSITY of South Asia 20 Md Rezaul Huda Reza UNIVERSITY of South Asia 20 It has: A name: first line stating important information about the method A body: Enclosed in a block { } Can be called (invoked) one or more times All programs consist of at least one method, Main( ) Methods are defined inside classes E. I. Teodorescu 20
UNIVERSITY of South Asia 21 Md Rezaul Huda Reza UNIVERSITY of South Asia 21 [Method access] returnType methodName ( [ParametersList] ) { //method body statements go here } E. I. Teodorescu 21 Method structure Example: optional public int CalculateAge(int y) { int birthYear = y; return 2010-y; } public int CalculateAge(int y) { int birthYear = y; return 2010-y; }
UNIVERSITY of South Asia 22 Md Rezaul Huda Reza UNIVERSITY of South Asia 22 Q: What is the purpose of declaring/creating methods? So you can use them at a later stage! They are “handy” pieces of code that do something Use a method by calling it from another part of the code. A method can be called as many times as you wish A method should be called at least once otherwise not much use to it! E. I. Teodorescu 22 Calling a method result = methodName ( [ParametersList] ); Calling a method result = methodName ( [ParametersList] ); optional
UNIVERSITY of South Asia 23 Md Rezaul Huda Reza UNIVERSITY of South Asia 23 public void Calculate ( ) { int x = 7; int y = 3; var result = 0; result = x * y; } E. I. Teodorescu 23 What is special about it? Is the Calculate() method returning anything?
UNIVERSITY of South Asia 24 Md Rezaul Huda Reza UNIVERSITY of South Asia 24 public double CalcSpeed(double m, int h) { double miles = m; int hours = h; return miles / hours; } E. I. Teodorescu 24 Return type Compatible value (double) returned
UNIVERSITY of South Asia 25 Md Rezaul Huda Reza UNIVERSITY of South Asia 25 private int AddValues(int x, int y) { return x + y ; } ……. E. I. Teodorescu 25 The return statement is usually positioned at the end of your method. Why? If you want a method to return information you must write a return statement inside the method! If the return type is “void”, the method doesn’t return a statement !
UNIVERSITY of South Asia 26 Md Rezaul Huda Reza UNIVERSITY of South Asia 26 Why do we want to use parameters? you may want a method to do something, but that method needs some external data The external data can be different every time you call that method. E. I. Teodorescu 26 public double CalcSpeed(double m, int h) { double miles = m; int hours = h; return miles / hours; } double porscheSpeed = CalcSpeed(260.0, 1) double tractorSpeed = CalcSpeed(80.0, 2)
UNIVERSITY of South Asia 27 Md Rezaul Huda Reza UNIVERSITY of South Asia 27 Declaring a method with parameters public int AddValues( int x, int y, int z) { return x+y+z; } Calling the method int r = AddValues(1,2,3) ; AddValues ( r, AddValues(3,3,3), 3*r ); E. I. Teodorescu 27 formal parameters When the method is declared. formal parameters When the method is declared. Actual arguments When the method is called. Actual arguments When the method is called. Exact match of the method’s name you are calling (case sensitive)
UNIVERSITY of South Asia 28 Md Rezaul Huda Reza UNIVERSITY of South Asia 28 public class AddingNumbers { double x; public int AddNumbers(int a, int b) { return a+b; } public int AddNumbers(int c, int d) { return d+c; } public int AddNumbers(int f, double g){ return d+c; } public int AddNumbers(double f, double g) { return f+g; } ……….. x = AddNumbers (3, 2)+ AddNumbers (4, 5) + AddNumbers (3.2, 2.0)+ AddNumbers (4, 5.0); ; string s = “Result= “ + x; } E. I. Teodorescu 28
UNIVERSITY of South Asia 29 Md Rezaul Huda Reza UNIVERSITY of South Asia 29 E. I. Teodorescu 29 What is the role of \n? What is the red “thing”? MessageBox.Show( What is this?
UNIVERSITY of South Asia 30 Md Rezaul Huda Reza UNIVERSITY of South Asia 30 E. I. Teodorescu 30 What is s and what is it set to? What is Parse(s) and what does it do? Switch Selection
UNIVERSITY of South Asia 31 Md Rezaul Huda Reza UNIVERSITY of South Asia 31 private void button1_Click(object sender, EventArgs e) { int y = 0; string s = " "; do // No semicolon on this line { s = s + y + " " + y++ + " " + ++y + "\n"; } while (y < 3); label1.Text = s; } E. I. Teodorescu 31 What is the difference between ++y and y++? Event created by double clicking button1 What would the output be if the condition was y<0 ? What will be the output in the label?
UNIVERSITY of South Asia 32 Md Rezaul Huda Reza UNIVERSITY of South Asia 32 E. I. Teodorescu 32
UNIVERSITY of South Asia 33 Md Rezaul Huda Reza UNIVERSITY of South Asia 33 E. I. Teodorescu 33 What is this?
UNIVERSITY of South Asia 34 Md Rezaul Huda Reza UNIVERSITY of South Asia 34 public double ConvertFahranheitToCelsius(int aNumber) { result = (5.0 / 9.0) * (aNumber - 32); return Math.Round(result,2); } E. I. Teodorescu 34 name What are they? What is this? What id Round(…)? What is this?
UNIVERSITY of South Asia 35 Md Rezaul Huda Reza UNIVERSITY of South Asia 35 When the button is clicked :, inputNumber = int.Parse(textBoxInput.Text); labelResult.Text = inputNumber + " Fahranheit is " + ConvertFahranheitToCelsius(inputN umber)+ " Celsius"; E. I. Teodorescu 35 Who is this?
UNIVERSITY of South Asia 36 Md Rezaul Huda Reza UNIVERSITY of South Asia 36 E. I. Teodorescu 36 What would happen if the formula is written : (5/9) * (aNumber – 32) What will the value of the result variable be ?
UNIVERSITY of South Asia 37 Md Rezaul Huda Reza UNIVERSITY of South Asia 37
UNIVERSITY of South Asia 38 Md Rezaul Huda Reza UNIVERSITY of South Asia 38