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

Similar documents
Introduction to C# Applications

Chapter 2: Using Data

CS313D: ADVANCED PROGRAMMING LANGUAGE

Visual C# Instructor s Manual Table of Contents

.Net Technologies. Components of.net Framework

DigiPen Institute of Technology

Declaration and Memory

Fundamental of Programming (C)

Advanced Computer Programming

Fundamentals of Programming

Tools : The Java Compiler. The Java Interpreter. The Java Debugger

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Introduction to Programming Using Java (98-388)

C# Fundamentals. built in data types. built in data types. C# is a strongly typed language

Course Outline Introduction to C-Programming

UNIT- 3 Introduction to C++

Reserved Words and Identifiers

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Chapter 1 Getting Started

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

204111: Computer and Programming

CSC Web Programming. Introduction to JavaScript

DEPARTMENT OF MATHS, MJ COLLEGE

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Quick Reference Guide

Chapter 6. Simple C# Programs

Java Primer 1: Types, Classes and Operators

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

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

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

GridLang: Grid Based Game Development Language Language Reference Manual. Programming Language and Translators - Spring 2017 Prof.

C# Fundamentals. Hans-Wolfgang Loidl School of Mathematical and Computer Sciences, Heriot-Watt University, Edinburgh

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

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

Understand Computer Storage and Data Types

C++ Programming: From Problem Analysis to Program Design, Third Edition

Lesson 02 Working with Data Types. MIT 31043: VISUAL PROGRAMMING By: S. Sabraz Nawaz Senior Lecturer in MIT

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

The Warhol Language Reference Manual

LECTURE 3 C++ Basics Part 2

Chapter 2: Introduction to C++

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Objectives. In this chapter, you will:

COMP322 - Introduction to C++ Lecture 01 - Introduction

Language Fundamentals Summary

Question And Answer.

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Introduce C# as Object Oriented programming language. Explain, tokens,

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

CSI33 Data Structures

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Values and Variables 1 / 30

BASIC ELEMENTS OF A COMPUTER PROGRAM

Methods: A Deeper Look

Lecture 2 Tao Wang 1

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Introduction to C# Applications Pearson Education, Inc. All rights reserved.

CSCI 1061U Programming Workshop 2. C++ Basics

Java Notes. 10th ICSE. Saravanan Ganesh

Chapter 3: Operators, Expressions and Type Conversion

PROGRAMMING FUNDAMENTALS

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Lesson 02 Working with Data Types MIT 31043, Visual Programming By: S. Sabraz Nawaz

The Java language has a wide variety of modifiers, including the following:

Operators. Java operators are classified into three categories:

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Eng. Mohammed S. Abdualal

Computers and Programming Section 450. Lab #1 C# Basic. Student ID Name Signature

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Programming. C++ Basics

A Comparison of Visual Basic.NET and C#

Chapter 2: Using Data

Quick Reference Guide

PHPoC vs PHP > Overview. Overview

Simple Java Reference

Program Fundamentals

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

Java Basic Programming Constructs

Programming, numerics and optimization

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

Introduction To C#.NET

CS313D: ADVANCED PROGRAMMING LANGUAGE

Computer Programming : C++

Chapter 12 Variables and Operators

JAVA Programming Fundamentals

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Tokens, Expressions and Control Structures

PHPoC. PHPoC vs PHP. Version 1.1. Sollae Systems Co., Ttd. PHPoC Forum: Homepage:

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

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

The C++ Language. Arizona State University 1

C# Types. Industrial Programming. Value Types. Signed and Unsigned. Lecture 3: C# Fundamentals

C#: framework overview and in-the-small features

EMBEDDED SYSTEMS PROGRAMMING Language Basics

Transcription:

Basics

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

Class Keyword class used to define new type specify name enclose body in Most C# code placed inside a class no global variables allowed no global methods allowed class definition class MyApplication 3

Main Main method is the application entry point must be static method of some class any access level such as public or private is allowed Main can perform environment interaction can receive command line arguments as array of strings can return int to indicate success/failure entry point class MyApplication static void Main() 4

Simple types Comprehensive set of simple types available Type Description Special format for literals Boolean bool Boolean true false character char 16 bit Unicode character 'A' '\x0041' '\u0041' sbyte 8 bit signed integer none byte 8 bit unsigned integer none short 16 bit signed integer none integer ushort 16 bit unsigned integer none int 32 bit signed integer none uint 32 bit unsigned integer U suffix long 64 bit signed integer L or l suffix ulong 64 bit unsigned integer U/u and L/l suffix float 32 bit floating point F or f suffix floating point double 64 bit floating point no suffix decimal 128 bit high precision M or m suffix string string character sequence "hello" 5

Local variable declaration Declare variables by specifying type and name names are case sensitive comma separated list for multiple variables end with semicolon variables class MyApplication static void Main() double area; char grade; int x, y, z; string name; 6

Local variable initialization Can initialize local variables when declared use literal value or expression compiler error to use without initializing literals expression class MyApplication static void Main() int width = 2; int height = 4; int area = width * height; error, x not set int x; int y = x * 2; 7

Type conversion Some automatic type conversions available from smaller to larger types Conversions which may lose information require cast syntax is type name inside parentheses int i = 5; double d = 3.2; implicit conversion cast required d = i; i = (int)d; 8

Input Read input using ReadLine from Console class in System namespace returns entire input line as a string Typically then need to convert string to actual type conversion methods provided in Convert class read entire line string s = System.Console.ReadLine(); convert string to int int i = System.Convert.ToInt32 (s); convert string to double double d = System.Convert.ToDouble(s); 9

Output Write output using Write and WriteLine from Console class in System namespace WriteLine adds line terminator in output overloaded versions allow printing of all types some versions take format string and data variable argument list version allows printing multiple values int i = 3; double d = 5.2; int double multiple System.Console.WriteLine(i); System.Console.WriteLine(d); System.Console.WriteLine("first 0 second 1", i, d); format string placeholder value 10

Library namespace Core standard library is System namespace a using directive provides shorthand access using directive short names using System; class MyApplication static void Main() string s = Console.ReadLine(); int i = Convert.ToInt32(s); 11

Comments Three comment styles available XML documentation comment /// documentation delimited code comment /* */ single line code comment // delimited /// <summary> /// MyApplication is very simple.</summary> class MyApplication static void Main() /* delimited comment can extend across multiple lines */ single line int x; // comment goes to end of line 12

Operators Comprehensive set of operators available Operator Description + addition - subtraction * multiplication / division % remainder << left shift >> right shift & bitwise and bitwise or ^ bitwise xor ~ bitwise complement addition int x = 5; int y = 3; int z = x + y; 13

Combination assignment Several combination assignment operators available perform both operation and assignment Operator Description += add / assign -= subtract / assign *= multiply / assign /= divide / assign %= remainder / assign <<= left shift / assign >>= right shift / assign combination addition int x = 5; x += 2; &= bitwise and / assign = bitwise or / assign ^= bitwise xor / assign 14

Shorthand operators A few shorthand operators available Operator Description ++ pre/post increment -- pre/post decrement?: conditional post-increment y = 5, x = 6 pre-increment x = 7, z = 7 int x = 5; int y = x++; int z = ++x; max set to greater of x and y int x = 5; int y = 3; int max; max = x > y? x : y; 15

Array Arrays provide efficient storage of multiple data elements create using new specify element type and array size access elements using operator [ ] total number of elements recorded in Length property valid indices are 0 to Length-1 IndexOutOfRangeException generated if index invalid create element access number of elements int[] a = new int[5]; a[0] = 17; a[1] = 32; int x = a[1]; int l = a.length; 16

Array element values Array elements have default values 0 for numeric types false for bool '\x0000' for char null for references Programmer can supply initial values default to false default to 0 set to given values bool[] a = new bool[10]; int[] b = new int[5]; int[] c = new int[5] 48, 2, 55, 17, 7 ; 17

If statement if statement provides conditional execution Boolean expression is evaluated associated statement executed only if expression is true expression must be inside parentheses keywords such as "if" are case sensitive int temperature = 127; bool boiling = false; if statement if (temperature >= 100) boiling = true; 18

If/else statement if statement may provide optional else part executed if expression is false int x = 3; int y = 5; int min; else part if (x < y) min = x; else min = y; 19

Code block Block required for multiple statements in control construct use int x = 3; int y = 5; int min, max; block block if (x < y) min = x; max = y; else min = y; max = x; 20

Comparison and logical operators Comparison operators compare two values yield Boolean result Combine Boolean values with conditional logical operators yield Boolean result both must be true either can be true c can't be a letter int x; char c; if (x > 0 && x < 10) if (c == 'y' c == 'Y') if (!Char.IsLetter(c)) == equal!= not equal <, <= less >, >= greater && and or! not 21

Switch statement switch statement performs selection from a set of options expression evaluated and matching case executed case labels must be constant values end each case with keyword break optional default case executed if no label matches double total = 0.0; char grade = 'C'; switch cases switch (grade) case 'A': total += 4.0; break; case 'B': total += 3.0; break; case 'C': total += 2.0; break; case 'D': total += 1.0; break; case 'F': total += 0.0; break; default default: Console.WriteLine("Error"); break; 22

Switch type Types allowed in switch are limited integral types string string strings string color = "blue"; switch (color) case "red" : Console.WriteLine("red"); break; case "blue" : Console.WriteLine("blue"); break; case "green": Console.WriteLine("green"); break; 23

Multiple labels Can associate several different labels with same action use separate case for each label place action in last case pass switch (grade) case 'A': case 'B': case 'C': Console.WriteLine("pass"); break; no pass case 'D': case 'F': Console.WriteLine("no pass"); break; 24

Multiple actions Can perform multiple actions for match on single label must forward control to next desired case use goto case or goto default instead of break string level = "gold"; switch (level) case "silver": prioritycheckin = true; break; forward control to other case case "gold": priorityupgrade = true; goto case "silver"; case "platinum": useoflounge = true; goto case "gold"; 25

while loop while loop runs as long as condition is true statements repeatedly executed stops when condition becomes false int i = 0; loop while true while (i < 5) i++; 26

do-while loop do-while loop test condition located after body body executed at least once int x; do test done after body do string s = System.Console.ReadLine(); x = System.Convert.ToInt32(s); while (x < 0); 27

for loop for loop centralizes control information initialization, condition, update parts separated by semicolons done once at start loop runs while test is true for (int k = 0; k < 5; k++) done each time after body 28

foreach loop Specialized foreach loop provided for collections like array reduces risk of indexing error provides read only access int[] data = new int[5] 48, 2, 55, 17, 7 ; int sum = 0; foreach foreach (int x in data) sum += x; type value collection 29

Break break statement exits enclosing statement usable with switch, while, do, for, foreach int i = 0; bool error; while (i < 5) break if (error) break; 30

Continue continue statement skips to end of current loop iteration does not exit loop usable with while, do, for, foreach int i = 0; bool skip; while (i < 5) continue if (skip) continue; 31

Goto goto transfers control to the specified labeled statement label must be in current or outer scope label must be in same method int[,] data = new int[2,4]; goto label for (int i = 0; i < data.getlength(0); i++) for (int j = 0; j < data.getlength(1); j++) if (data[i,j] < 0) goto error; error: Console.WriteLine("Error"); 32

Summary Class is basic unit of C# coding Main defines application entry point Comprehensive set of types available Standard operators provided Arrays provide efficient way to store large amount of data Control constructs offer balance of convenience and safety 33