4. Statements and Methods. 2 Microsoft Objectives “With regards to programming statements and methods, C# offers what you would come to expect from a.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Language Fundamentals in brief C# - Introduction.
C#: Udtryk og metoder. Indhold “With regards to programming statements and methods, C# offers what you would come to expect from a modern OOPL…” Udtryk.
Chapter 7: User-Defined Functions II
Chapter 10 Introduction to Arrays
Objectives Using functions to organize PHP code
C#: Data Types Based on slides by Joe Hummel. 2 UCN Technology: Computer Science Content: “.NET is designed around the CTS, or Common Type System.
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
COMP1180 Review Date: 4 March, 2009 Time: 10:30am - 12:20pm Venue: –CS students -- FSC801C and FSC801D –IS and other students -- OEE1017 Remarks: – 1)
Class. 2 Objectives Discuss class basics –fields –methods –access levels Present object creation –new operator.
C# Programming: From Problem Analysis to Program Design1 Arrays C# Programming: From Problem Analysis to Program Design 3 rd Edition 7.
3. Data Types. 2 Microsoft Objectives “.NET is designed around the CTS, or Common Type System. The CTS is what allows assemblies, written in different.
COMP1170 Midterm Preparation (March 17 th 2009) Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.
5. OOP. 2 Microsoft Objectives “Classes, objects and object-oriented programming (OOP) play a fundamental role in.NET. C# features full support for the.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
C# vs. C++ What's Different & What's New. An example C# public sometype myfn { get; set; } C++ public: sometype myfn { sometype get (); void set (sometype.
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
C#: Statements and Methods Based on slides by Joe Hummel.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
1.  Collections are data structures that holds data in different ways for flexible operations  C# Collection classes are defined as part of the ◦ System.Collections.
Chapter Nine: Subprograms Lesson 09. What are they  Modularized code  Might return a value  Functions  Or not  Procedures  Subroutines  In object.
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
Advanced Programming LOOP.
FEN 2012 UCN Technology: Computer Science1 C# - Introduction Language Fundamentals in Brief.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
Spring 2007NOEA: Computer Science Programme 1 C# - Introduction Language Fundamentals: Data Types string Objects and Classes Methods Iteration and Selection.
C# Intro Programming languages and programs: Source code and object code Editors and compilers C# fundamentals: Program structure Classes and Objects Variables.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Iteration (Loop) partI Thanachat Thanomkulabut. Consider the following program! using System; Namespace SimPleExample { class SimPleC { class SimPleC.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Loops Repeating Code Multiple Times SoftUni Team Technical Trainers Software University
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays Part 4.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
1 Advanced Programming IF. 2 Control Structures Program of control –Program performs one statement then goes to next line Sequential execution –Different.
VB and C# Programming Basics. Overview Basic operations String processing Date processing Control structures Functions and subroutines.
1 C# A brief overview by Jack Senechal and Bryan Powell.
PHP Programming with MySQL Slide 4-1 CHAPTER 4 Functions and Control Structures.
C# Programming Fundamentals Control Flow Jim Warren, COMPSCI 280 S Enterprise Software Development.
Chapter 2 Functions and Control Structures PHP Programming with MySQL 2 nd Edition.
Chapter 05 (Part III) Control Statements: Part II.
CSIS 113A Lecture 8 Parameters.  Two methods of passing arguments as parameters  Call-by-value  ‘copy’ of value is passed  Call-by-reference  ‘address.
 2003 Prentice Hall, Inc. All rights reserved. Outline 1 fig02_07.cpp (1 of 2) 1 // Fig. 2.7: fig02_07.cpp 2 // Class average program with counter-controlled.
Methods SWE 344 Internet Protocols & Client Server Programming.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
Section 3 - Arrays and Methods. Arrays Array: collection of group of data variables of same type, sharing the same name for convenience - Easy to search.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Method Parameters and Overloading Version 1.0. Topics The run-time stack Pass-by-value Pass-by-reference Method overloading Stub and driver methods.
Session 1 C# Basics.
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.
C# Programming Methods.
Session 02 Module 3: Statements and Operators Module 4: Programming constructs Module 5: Arrays.
1 Statements © University of Linz, Institute for System Software, 2004 published under the Microsoft Curriculum License.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
Lecture 02 Dr. Eng. Ibrahim El-Nahry Methods. 2 Learning Objectives Class Definition includes both methods and data properties Method Definition and Declaration.
ITM 3521 ITM 352 Functions. ITM 3522 Functions  A function is a named block of code (i.e. within {}'s) that performs a specific set of statements  It.
Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 Pass-by-Value - default passing mechanism except.
Week 3-4 Control flow (review) Function definition Program Structures
Repetition (While-Loop) version]
11/10/2018.
Fundamental Programming
Lecture 11 Parameters CSE /26/2018.
Iteration (Loop) part II
5. OOP OOP © 2003 Microsoft.
Presentation transcript:

4. Statements and Methods

2 Microsoft Objectives “With regards to programming statements and methods, C# offers what you would come to expect from a modern OOPL…” Statements Methods

3 Microsoft Part 1 Statements…

4 Microsoft Statements in C# C# supports the standard assortment… Assignment Subroutine and function call Conditional –if, switch Iteration –for, while, do-while Control Flow –return, break, continue, goto

5 Microsoft Examples x = obj.foo(); if (x > 0 && x < 10) count++; else if (x == -1)... else {... } while (x > 0) {... x--; } for (int k = 0; k < 10; k++) {... }

6 Microsoft Other statements C# contains a couple surprises… –data structure iteration via foreach –namespace importing via using

7 Microsoft foreach Specialized foreach loop provided for collections like array –reduces risk of indexing error –provides read only access int[] data = { 1, 2, 3, 4, 5 }; int sum = 0; foreach (int x in data) { sum += x; } foreach typevaluecollection

8 Microsoft using using directive allows unqualified access to namespace –a convenience mechanism only… –still must ref underlying assembly // before Workshop.Customer c; c = new Workshop.Customer("joe hummel", 94652); // after using Workshop; Customer c; c = new Customer("joe hummel", 94652); namespace Workshop { public class Customer {. } public class Product {. }

9 Microsoft Complete example using directive(s) specified at top of file /* main.cs */ using System; using Workshop; public class App { public static void Main() { Customer c; c = new Customer("joe hummel", 94652); Console.WriteLine( c.ToString() ); } namespace Workshop { public class Customer {. } public class Product {. }

10 Microsoft Part 2 Methods…

11 Microsoft Types of methods Classes contain 2 types of methods: –those with no return value ( void ) –those with a return value ( int, string, etc.) Methods may be: –instance –static Instance methods require an object to call Static methods are global and thus require only class name

12 Microsoft Example Array class in FCL –fully-qualified name is System.Array namespace System { public class Array { public int GetLength(int dimension) {... } public static void Sort(Array a) {... }. } instance method (absence of static) static method (presence of static)

13 Microsoft Calling methods Here's an example of calling into the Array class: /* main.cs */ using System; public class App { public static void Main() { int[] data = { 11, 7, 38, 55, 3 }; Array.Sort(data); for (int i=0; i<data.GetLength(0); i++) Console.WriteLine(i + ": " + data[i]); }

14 Microsoft Other useful static methods using System; public class Calculator { public static void Main() { string input, output; int a, b, sum; Console.Write("Enter first integer: "); input = Console.ReadLine(); a = Convert.ToInt32(input); Console.Write("Enter second integer: "); input = Console.ReadLine(); b = Convert.ToInt32(input); sum = a + b; output = String.Format("{0} + {1} = {2}", a, b, sum); Console.WriteLine(output); } A program to add 2 integers and output the sum:

15 Microsoft Parameter passing C# offers three options: –pass-by-value (default) –pass-by-reference –pass-by-result ("copy-out")

16 Microsoft Pass-by-value Pass by value is default parameter passing mechanism –data copied into method –any changes to parameter inside method affect local copy only value parameter void F(int x) { x = 0; } int y = 9; F(y); y unchanged

17 Microsoft Pass-by-reference ref parameter passes data in and out –use keyword ref in definition and call –must use variable in call –must initialize passed variable before call ref parameter, initially 9 void G(ref int x) { x += 1; } int y = 9; G(ref y); y set to 10

18 Microsoft Pass-by-result ("copy-out") out parameter returns data through parameter –use keyword out in both definition and call –must use variable in call –must assign to parameter inside method or compiler error out parameter void H(out int x) { x = 0; } int y; H(out y); y set to 0 assignment required

19 Microsoft Summary Standard statements, a few non-standard ones –assignment, if, for, while, foreach, using Two types of methods –instance methods require an object to call –static methods are global and thus require only class name

20 Microsoft References Books: –I. Pohl, "C# by Dissection" –S. Lippman, "C# Primer" –J. Mayo, "C# Unleashed"

21 Microsoft Lab? Work on lab #2, "Types, Stmts and Methods"…