By: Md Rezaul Huda Reza UNIVERSITY of South Asia 2 Md Rezaul Huda Reza UNIVERSITY of South Asia 2 2 Program.

Slides:



Advertisements
Similar presentations
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Advertisements

CS1061 C Programming Lecture 2: A Few Simple Programs A. O’Riordan, 2004.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
Computer Science 101 Introduction to Programming.
1 Chapter 9 Scope, Lifetime, and More on Functions.
JavaScript – Part II Data Types and Operations George Mason University June 3, 2010.
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
By: Md Rezaul Huda Reza Decision and iteration statements. Methods. E. I. Teodorescu.
The Java Programming Language
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
CSE 1301 Lecture 2 Data Types Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
DiagrammaticRepresentation Iteration Construct False True Condition Exit from Statement (s) loop Sequence construct Selection construct Statement 1 Statement.
TUTORIAL 10: PROGRAMMING WITH JAVASCRIPT Session 2: What is JavaScript?
CPS120 Introduction to Computer Science Iteration (Looping)
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Chapter 2 Overview of C++. A Sample Program // This is my first program. It calculates and outputs // how many fingers I have. #include using namespace.
CPS120: Introduction to Computer Science
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.
CPS120: Introduction to Computer Science Decision Making in Programs.
JavaScript Syntax and Semantics. Slide 2 Lecture Overview Core JavaScript Syntax (I will not review every nuance of the language)
Controlling Execution Dong Shao, Nanjing Unviersity.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
CONTENTS Processing structures and commands Control structures – Sequence Sequence – Selection Selection – Iteration Iteration Naming conventions – File.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
JavaScript, Fourth Edition
CSC 212 Object-Oriented Programming and Java Part 2.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
 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.
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
Controlling Program Flow with Decision Structures.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Information and Computer Sciences University of Hawaii, Manoa
Chapter Topics The Basics of a C++ Program Data Types
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
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.
Basic Elements of C++.
Revision Lecture
Chapter 3 Loops Section 3.3 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Variables A piece of memory set aside to store data
JavaScript: Control Statements.
Basic Elements of C++ Chapter 2.
Arrays, For loop While loop Do while loop
Variables ICS2O.
T. Jumana Abu Shmais – AOU - Riyadh
CSC215 Lecture Control Flow.
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Tutorial 10: Programming with javascript
Introduction to Programming
Chap 2. Identifiers, Keywords, and Types
Programming Languages and Paradigms
CSC215 Lecture Control Flow.
CIS 136 Building Mobile Apps
Presentation transcript:

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