CS313D: ADVANCED PROGRAMMING LANGUAGE

Similar documents
2/3/2018 CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II. Lecture Contents. C# basics. Methods Arrays. Dr. Amal Khalifa, Spr17

CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II

CS111: PROGRAMMING LANGUAGE II

CS110D: PROGRAMMING LANGUAGE I

CS111: PROGRAMMING LANGUAGE II

CS313D: ADVANCED PROGRAMMING LANGUAGE

Introduction to Programming Using Java (98-388)

by Pearson Education, Inc. All Rights Reserved. 2

Le L c e t c ur u e e 3 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Control Statements

ECE 122. Engineering Problem Solving with Java

CSC Java Programming, Fall Java Data Types and Control Constructs

Instructor: SIR MUHAMMAD NAVEED Created by: ARSLAN AHMED SHAAD ( ) MUHAMMAD BILAL ( ) ISIT:

Exam 2. CSC 121 MW Class. Lecturer: Howard Rosenthal. April 26, 2017

1 Lexical Considerations

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

CS313D: ADVANCED PROGRAMMING LANGUAGE

Introduction to Programming (Java) 4/12

CS110D: PROGRAMMING LANGUAGE I

( &% class MyClass { }

Lecture 5: Methods CS2301

PROGRAMMING FUNDAMENTALS

CS1150 Principles of Computer Science Methods

Computational Expression

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Chapter 4 Defining Classes I

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

CS-201 Introduction to Programming with Java

The Basic Parts of Java

Lexical Considerations

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Object-Oriented Programming

Java Methods. Lecture 8 COP 3252 Summer May 23, 2017

Lexical Considerations

Principles of Computer Science

Objectives. Introduce the core C# language features class Main types variables basic input and output operators arrays control constructs comments

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs.

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

CS1150 Principles of Computer Science Methods

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

Java Primer 1: Types, Classes and Operators

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 6: User-Defined Functions I

Flow of Control. Chapter 3 Part 3 The Switch Statement

Chapter 7 Array. Array. C++, How to Program

Key Differences Between Python and Java

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

BRANCHING if-else statements

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

Introduction. C provides two styles of flow control:

Conditionals and Loops

Pace University. Fundamental Concepts of CS121 1

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

(Not Quite) Minijava

CS313D: ADVANCED PROGRAMMING LANGUAGE

Flow Control. CSC215 Lecture

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

CSCI 1061U Programming Workshop 2. C++ Basics

Review for Test 1 (Chapter 1-5)

Object Oriented Programming. Java-Lecture 6 - Arrays

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

CSCE 206: Structured Programming in C++

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes

Flow of Control. Chapter 3

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

10/30/2010. Introduction to Control Statements. The if and if-else Statements (cont.) Principal forms: JAVA CONTROL STATEMENTS SELECTION STATEMENTS

Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

CS 61B Data Structures and Programming Methodology. June David Sun

CS260 Intro to Java & Android 03.Java Language Basics

Lecture 2: C Programming Basic

Java Review. Java Program Structure // comments about the class public class MyProgram { Variables

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

Methods: A Deeper Look

EECS168 Exam 3 Review

Array. Prepared By - Rifat Shahriyar

Operators. Java operators are classified into three categories:

Programming with Java

x = 3 * y + 1; // x becomes 3 * y + 1 a = b = 0; // multiple assignment: a and b both get the value 0

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

Introduction to C Programming

Lecture Set 4: More About Methods and More About Operators

Computing Science 114 Solutions to Midterm Examination Tuesday October 19, In Questions 1 20, Circle EXACTLY ONE choice as the best answer

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

C Programming for Engineers Functions

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

IT 374 C# and Applications/ IT695 C# Data Structures

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Building Java Programs

Following is the general form of a typical decision making structure found in most of the programming languages:

Programming Constructs Overview. Method Call System.out.print( hello ); Method Parameters

STRUCTURED DATA TYPE ARRAYS IN C++ ONE-DIMENSIONAL ARRAY TWO-DIMENSIONAL ARRAY

CS-140 Fall 2017 Test 1 Version Practice Practie for Sept. 27, Name:

Admin. CS 112 Introduction to Programming. Recap: Exceptions. Summary: for loop. Recap: CaesarFile using Loop. Summary: Flow Control Statements

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

1/16/2013. Program Structure. Language Basics. Selection/Iteration Statements. Useful Java Classes. Text/File Input and Output.

CSC 1214: Object-Oriented Programming

Transcription:

CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science Department Lecture 3: C# language basics

Lecture Contents 2 C# basics Conditions Loops Methods Arrays Dr. Amal Khalifa, Spr 2015

3 Conditions and branching Dr. Amal Khalifa, Spr 2015

Relational operators 4 Dr. Amal Khalifa, Spr 2015

Logical Operators 5 Dr. Amal Khalifa, Spr 2015

Conditional Statements 6 A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic decisions Java's conditional statements: the if and if-else statements the conditional operator the switch statement Dr. Amal Khalifa, Spr 2015

condition evaluated true false Statement 1 Statement 2 7 Logic of an if-else statement Several statements can be grouped together into a block statement A block is delimited by braces ( { } ) Dr. Amal Khalifa, Spr 2015

The if Statement 8 The if statement has the following syntax: if is a Java reserved word The condition must be a boolean expression. e.g., a boolean variable, a == b, a <= b. It must evaluate to either true or false. if ( condition ) statement1; else statement2; If the condition is false, this statement is executed. If the condition is true, this statement is executed. Dr. Amal Khalifa, Spr 2015

Conditional Operator Also called "ternary operator" Allows embedded conditional in expression Essentially "shorthand if-else" operator Example: if (n1 > n2) max = n1; else max = n2; Can be written: max = (n1 > n2)? n1 : n2; "?" and ":" form this "ternary" operator dr. Amal Khalifa,Fall14

switch multipleselection statement a constant integral expression of type: byte, short, int or char. case labels break statement optional default case dr. Amal Khalifa,Fall14

11 loops Chapters 5 & 6 Dr. Amal Khalifa, Spr 2015

12 While loops while ( condition ) statement; Dr. Amal Khalifa, Spr 2015

Example 13 Dr. Amal Khalifa, Spr 2015

14 do loops do { statement; } while ( condition ); Dr. Amal Khalifa, Spr 2015

Example 15 Dr. Amal Khalifa, Spr 2015

16 for loops for ( initialization ; condition ; increment ) statement; Dr. Amal Khalifa, Spr 2015

Example 1992-2014 by Pearson Education, Inc. All Rights Reserved.

break and Continue break; Forces loop to exit immediately. Execution continues with the first statement after the control statement. continue; Skips rest of loop body In while and do while statements, the program evaluates the loop-continuation test immediately after the continue statement executes. In a for statement, the increment expression executes, then the program evaluates the loop-continuation test.

19 Methods Chapter 7 Dr. Amal Khalifa, Spr 2015

Packaging Code in C# The Framework Class Library provides many predefined classes that contain methods for performing common tasks.

1992-2014 by Pearson Education, Inc. All Rights Reserved.

Because these methods are static, you can access them via the class name Math and the member access (.) operator, just like class Math s methods. 1992-2014 by Pearson Education, Inc. All Rights Reserved.

Methods 23 A method: groups a sequence of statement takes input, performs actions, and produces output In Java, each method is defined within specific class Dr. Amal Khalifa, Spr 2015

Method Declaration: Header 24 A method declaration begins with a method header public class MyClass { static int min ( int num1, int num2 ) properties return type method name parameter list The parameter list specifies the type and name of each parameter The name of a parameter in the method declaration is called a formal argument Dr. Amal Khalifa, Spr 2015

Method Declaration: Body 25 The header is followed by the method body: class MyClass { static int min(int num1, int num2) { int minvalue = num1 < num2? num1 : num2; return minvalue; } } Dr. Amal Khalifa, Spr 2015

The return Statement 26 The return type of a method indicates the type of value that the method sends back to the calling location A method that does not return a value has a void return type The return statement specifies the value that will be returned Its expression must conform to the return type Dr. Amal Khalifa, Spr 2015

Calling a Method 27 Each time a method is called, the values of the actual arguments in the invocation are assigned to the formal arguments int num = min(2, 3); static int min (int num1, int num2) { } int minvalue = (num1 < num2? num1 : num2); return minvalue; Dr. Amal Khalifa, Spr 2015

Example

1992-2014 by Pearson Education, Inc. All Rights Reserved.

30 Method Call Stack A method can call another method, who can call another method, main min(num1, num2, num3) println() min(1, 2, 3); println( ) Dr. Amal Khalifa, Spr 2015

Method Overloading Methods of the same name declared in the same class Must have different sets of parameters (signatures). the compiler differentiates signatures by : the number of parameters, the types of the parameters and the order of the parameter types in each signature. Method calls cannot be distinguished by return type. Overloaded methods can have different return types if the methods have different parameter lists.

Example 1992-2014 by Pearson Education, Inc. All Rights Reserved.

1992-2014 by Pearson Education, Inc. All Rights Reserved.

Error!! 1992-2014 by Pearson Education, Inc. All Rights Reserved.

Optional Parameters Methods can have optional parameters that allow the calling method to vary the number of arguments to pass. An optional parameter specifies a default value that s assigned to the parameter if the optional argument is omitted. Example: public int Power( int basevalue, int exponentvalue = 2) You can create methods with one or more optional parameters. All optional parameters must be placed to the right of the method s non-optional parameters.

1992-2014 by Pearson Education, Inc. All Rights Reserved.

7.13 Optional Parameters (Cont.) Optionally, a second argument (for the exponentvalue parameter) can be passed to Power. Consider the following calls to Power: Power() Power(10) Power(10, 3) The first generates a compilation error because this method requires a minimum of one argument. The second is valid because one argument (10) is being passed the optional exponentvalue is not specified in the method call. 1992-2014 by Pearson Education, Inc. All Rights Reserved.

Example

Passing arguments to Methods Pass-by-value Pass-by-reference also called call-by-value A copy of the argument s value is passed to the called method. The called method works exclusively with the copy Changes to the called method s copy do not affect the original variable s value in the caller. also called call-byreference The called method can access the argument s value directly and modify that data, if necessary Improves performance by eliminating the need to copy

Passing arguments to Methods Applying the ref keyword to a parameter declaration allows you to pass a variable to a method by reference The ref keyword is used for variables that already have been initialized in the calling method. Preceding a parameter with keyword out creates an output parameter. This indicates to the compiler that the argument will be passed by reference and that the called method will assign a value to it. A method can return multiple output parameters.

Example

Be careful!! 1992-2014 by Pearson Education, Inc. All Rights Reserved.

44 Arrays & Strings Chapter 8 Dr. Amal Khalifa, Spr 2015

What is an array? 45 Array data structures Group of variables (called elements) containing values of the same type. related data items of the same type. fixed length once created. Elements referred to using index or subscript. In C#, Arrays are objects, so they re considered reference types. Every array object knows its own length and stores it in a Length instance variable. Elements can be either primitive types or reference types (strings). Dr. Amal Khalifa, Spr 2015

46 Array elements An index must be a nonnegative integer Can use an expression as an index Every array object knows its own length and stores it in a length instance variable

7.3 Arrays in Java 47 declare create initialize int[] a; int a[]; a = new int[5]; for(int i=0;i<5;i++) a[i] = i*i; Dr. Amal Khalifa, Spr 2015

Example 48 Dr. Amal Khalifa, Spr 2015

Example: 49 Dr. Amal Khalifa, Spr 2015

Example 1992-2014 by Pearson Education, Inc. All Rights Reserved.

Example 1992-2014 by Pearson Education, Inc. All Rights Reserved.

1992-2014 by Pearson Education, Inc. All Rights Reserved.

foreach Statement The foreach statement iterates through the elements of an entire array or collection. syntax foreach( type identifier in arrayname ) statement type and identifier are the type and name (e.g., int number) of the iteration variable. arrayname is the array through which to iterate. The type of the iteration variable must be consistent with the type of the elements in the array. The iteration variable represents successive values in the array on successive iterations of the foreach statement.

Example

55 That s all.. Dr. Amal Khalifa, Spr 2015