C# Programming.

Slides:



Advertisements
Similar presentations
Selection Statement – Selection Statement Modified from Aj. Thanachat Thanomkulabut’s slide 1 st semester, 2012.
Advertisements

Basic Elements of C++ Chapter 2.
Implementing Control Logic in C# Svetlin Nakov Telerik Corporation
Thanachat Thanomkulabut
Introduction to C# Programming ณภัทร สักทอง Application Program Development.
Variable Declaration  It is possible to declare multiple variables of the same data type on the same line.  Ex. double hours, rate, total;  Variables.
1 nd Semester Module3 Selection Statement Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
1 Conditional statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
CPS120: Introduction to Computer Science
Introduction to Computer and Programing Thanachat Thanomkulabut.
C++ Programming: Basic Elements of C++.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
Introduction to Computer and Programing Thanachat Thanomkulabut.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
1 st Semester Module3 Condition Statement อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
C# Basic Concept Thanachat Thanomkulabut. Naming Rules  Letters, digits and underscores(_)  First character  letter or _  Up to 63 characters long.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX.
Input & Output Statement
Selection Statement – Selection Statement Modified from Aj. Thanachat Thanomkulabut’s slide 1 st semester, 2012.
Selection Statement Thanachat Thanomkulabut. Outline 2  Boolean expression  if  if statement  nested if  nested if statement  switch case  switch.
Introduction to Computer and Programing Thanachat Thanomkulabut.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 – Introduction to C# Programming Outline 3.1 Introduction 3.2 Simple Program: Printing a Line.
CPS120: Introduction to Computer Science Variables and Constants.
C# Programming: Basic Concept Part 2 Computer and Programming (204111)
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
1 nd Semester Module2 C# Basic Concept Thanawin Rakthanmanon Computer Engineering Department Kasetsart University, Bangkok.
Introduction to Computer and Programing Thanachat Thanomkulabut.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Input/Output Statement Thanachat Thanomkulabut. Input Statement  Console.ReadLine()  Return string  Use to get the input from user  Convert string.
1 nd Semester Module6 Review Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department Kasetsart.
Bill Tucker Austin Community College COSC 1315
Chapter # 2 Part 2 Programs And data
Information and Computer Sciences University of Hawaii, Manoa
Today Variable declaration Mathematical Operators Input and Output Lab
Basic concepts of C++ Presented by Prof. Satyajit De
Chapter 1.2 Introduction to C++ Programming
Egyptian Language School Computer Department
Chapter Topics The Basics of a C++ Program Data Types
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
BASIC ELEMENTS OF A COMPUTER PROGRAM
Basic Elements of C++.
Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Thanachat Thanomkulabut
Chapter 4 – Control Structures Part 1
Chapter 3: Understanding C# Language Fundamentals
SELECTION STATEMENTS (1)
Basic Elements of C++ Chapter 2.
Control Statement Examples
IDENTIFIERS CSC 111.
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Chapter 2 - Introduction to C Programming
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2 - Introduction to C Programming
Module 2: Understanding C# Language Fundamentals
Chapter 2 - Introduction to C Programming
Chapter 3 – Introduction to C# Programming
Chapter # 2 Part 2 Programs And data
elementary programming
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Chapter 2 - Introduction to C Programming
Just Enough Java 17-May-19.
Presentation transcript:

C# Programming

Outline 2 Programming Languages C# Language Overview

Programming Languages 3 Program A set of instructions for a computer to follow, written in specific programming language Types of programming language High-Level Language Assembly Language Machine Language

High-level VS Assembly VS Machine Language Programming Languages High-level VS Assembly VS Machine Language 4 High-level Language Nearly like human word SUM := A * 2 + ALPHA/3; Assembly Language Some key words are understandable MULL3 A, #2, R ADDL3 R6, R7, SUM Machine Language Only “0” and “1” 00011000011 00011001111 10011000111 Computer itself understands only Machine language

Language translator Hello World! _ Machine language Programming Languages Language translator 5 Interpreter / Compiler High-level language static void Main( ) { Console.WriteLine("Hello World!"); } Assembly language pushl %ebp movl %esp, %ebp subl $8, %esp andl $-16, %esp Machine language 00011000110001110 00110001110101111 00011000110001110 Assembler Machine Hello World! _

High-Level Languages Procedural Language Functional Language Programming Languages High-Level Languages 6 Procedural Language Fortran Cobol Basic C Pascal Object-Oriented Language C++ Java C# Functional Language Lisp Logic Language Prolog

Outline 7 Programming Languages C# Language Overview

C# Language Overview A simple C# Program 8 Grouping using { }

A simple C# Program Statement ending with semicolon “;” C# Language Overview A simple C# Program 9 Statement ending with semicolon “;”

A simple C# Program C# syntax is case-sensitive namespace NAMEspace C# Language Overview A simple C# Program 10 C# syntax is case-sensitive namespace NAMEspace Main() main()

A simple C# Program White space means nothing C# Language Overview A simple C# Program 11 White space means nothing static void Main(string[] args) { Console.WriteLine("Hello World!"); } static void Main(string[] args){ Console.WriteLine("Hello World!");}

C# Language Overview A simple C# Program 12 Anything between /* */ or after // is considered a comment Comments will not be translated static void Main(string[] args) //comment here { /* This is comment too. */ Console.WriteLine("Hello World!"); }

Program Structure The starting point of the program is: C# Language Overview Program Structure 13 The starting point of the program is: This is known as the method Main A method is put inside a class A class may be put inside a namespace static void Main () { ... starting point ... }

Program Structure In C# In other words C# Language Overview Program Structure 14 In C# A program can contain several namespaces A namespace can contain several classes A class can contain several methods In other words Think of a namespace as a container of classes Think of a class as a container of methods namespace Class method1 method2 Class

Program Structure For this 1204452 course C# Language Overview Program Structure 15 For this 1204452 course Program with only one class and at most one namespace For now until sometime before midterm Program with one method (i.e., Main) namespace HelloW { class HelloWClass { static void Main () { System.Console.WriteLine("Hello World!"); System.Console.ReadLine(); }

Naming Rules MSU53 ≠ msU53 ≠ msu53 Letters, digits and underscores(_) C# Language Overview Naming Rules 16 Letters, digits and underscores(_) First character letter or _ Up to 63 characters long Must not be a reserved word * Case Sensitive Example MSU53 ≠ msU53 ≠ msu53

Naming Rules         Letters, digits and underscores(_) C# Language Overview Naming Rules 17 Letters, digits and underscores(_) First character letter or _ Up to 63 characters long Must not be a reserved word Example   name  Name point9 9point   _data class  class_A  class_”A” 

C# Language Overview C# Reserved Words 18

Any question?

C# Basic Concept

Outline C# Beginning Variable and Constant Expression Statement 22 C# Beginning Variable and Constant Expression Statement Modify-And-Assign Math Class

C# Beginning The starting point of the program is: 23 The starting point of the program is: This is known as the method Main A method is put inside a class A class may be put inside a namespace static void Main () { ... starting point ... }

Inside method Main Variable declarations Statements C# Beginning Inside method Main 24 Variable declarations Statements static void Main(string[] args) { const double pi = 3.1416; int radius; double area; radius = int.Parse(Console.ReadLine()); area = pi*radius*radius; Console.WriteLine(area); }

Outline C# Beginning Variable and Constant Expression Statement 25 C# Beginning Variable and Constant Expression Statement Modify-And-Assign Math Class

A variable is used to store “data.” Variable & Constant What is Variable? 26 A variable is used to store “data.” “It must be declared before used”

Data Types Type Size Description Range bool 1 byte Store truth value Variable & Constant Data Types 27 Type Size Description Range bool 1 byte Store truth value true / false char Store one character character code 0 – 255 byte Store positive integer 0 – 255 short 2 byte Store integer -32,768 -- 32,767 int 4 byte -2.1 x 109 -- 2.1 x 109 long 8 byte -9.2 x 1018 -- 9.2 x 1018 double 16 byte Store real number ± 5.0x10-324 -- ± 1.7x10308 string N/A Store sequence of characters

C# Variable Declaration Variable & Constant C# Variable Declaration 28 Syntax: <data type> <name>; Example: We can also assign its initial value. Example: int radius; double area; bool isokay; int k = 200; bool done = false;

C# Variable Declaration 29 Syntax: <data type> <name_1>, <name_2>,..., <name_n>; Example: We can also assign its initial value. Example: int width, length, height; double mean, sd, max, min; bool isokay, isright, check; int width=5, length, height=4;

Test I - Variable Declaration 30 Declare variable1 Name : num_Student Type : interger Initial value : nothing Declare variable2 Name : gender Type : character Initial value : m Declare variable3,4,5 Name3 : u Name4 : t Name5 : a Type : double Initial value3 : 5.0 Initial value4 : nothing Initial value5 : 9.8

C# Constant Declaration Variable & Constant C# Constant Declaration 31 Syntax: const <data type> <name> = <value>; Example: const int radius = 15; const double area=1.5; const bool isokay=true; const string movie=”StarWarIII”; const char mckazine=‘m’;

C# Constant Declaration Variable & Constant C# Constant Declaration 32 Syntax: const <data type> <name_1> = <value_1>, <name_2> = <value_2>, ... , <name_n> = <value_n>; Example: const int radius = 15, height = 5; const double area=1.5, wide=3.2, lenght = 4.1;

Test II - Constant Declaration 33 Declare Constant Name : e Type : double Initial value : 2.71828

Outline C# Beginning Variable and Constant Expression Statement 34 C# Beginning Variable and Constant Expression Statement Modify-And-Assign Math Class

C# Expression Expression Arithmetic Expression Boolean Expression 35 Expression Arithmetic Expression Boolean Expression

Arithmetic Expression 36 Operators + - * / % (remainder after division) Example 11 + 5  39 / 5  39.0/5  39 % 5  5.0 % 2.2  16 7 7.8 4 0.6

Piority of Arithmetic Operators Expression Piority of Arithmetic Operators 37 Priority operator 1 Parentheses () 2 *, / , % 3 +, - 4 If equal precedence, left to right Answer int a, b; a = 2-10*3/5+(6%4); b = 5*(15%4+(2-3))/9; a = -2 b = 1

Calculation Priority static void Main(){ int a,b,c,d; double e,f,g; Expression Calculation Priority 38 static void Main(){ int a,b,c,d; double e,f,g; a=2; b=7; c=5; d=c/a; e=5/b; f=5.0/2; g=5/2.0; } Answer d = 2 e = 0 f = 2.5 g = 2.5

Boolean Expression Operators Comparison Boolean Equal == Not equal != 39 Operators Comparison Equal == Not equal != Less < Greater > Less than or equal to <= Greater than or equal to >= Boolean And && Or || Not ! 0 and 0 = 0 0 and 1 = 0 1 and 0 = 0 1 and 1 = 1 0 or 0 = 0 0 or 1 = 1 1 or 0 = 1 1 or 1 = 1 not 0 = 1 not 1 = 0

Example: Boolean Expression 40 10 > 50  ’A’ < ’B’  (3<2) || (2+5 > 6)  (’a’ != ’z’) && !(9==0)  false true true true

Outline C# Beginning Variable and Constant Expression Statement 41 C# Beginning Variable and Constant Expression Statement Modify-And-Assign Math Class

Statements A statement is a unit of command to instruct your program 42 A statement is a unit of command to instruct your program A method consists of one or more statements Statement#1 class Hello { static void Main () { Console.WriteLine("Hello World!"); Console.ReadLine(); } Statement#2

C# Statement Types C# Statement Types Assignment Statement 43 C# Statement Types Assignment Statement Input Statement Output Statement

Assignment Statement Assigning value to variable 44 Assigning value to variable Use the equal sign (=) when making assignments. Syntax: <variable> = <expression>; int Width,High; Width=10; High=20+Width;

Input Statement Console.ReadLine() Return string 45 Console.ReadLine() Return string Use to get the input from user Convert string to other data type int.Parse() Convert string to integer double.Parse() Convert string to double Example string st; st = Console.ReadLine();

Example: Input Statement 46 Ex1: string myname; myname = Console.ReadLine(); Ex2: int Width; string temp1; temp1 = Console.ReadLine(); Width = int.Parse(temp1);

Statement Output Statements 47 Use the method Write or WriteLine in the Console class (which is in System namespace) Basic usage: Advanced usage: Even more advanced usage: Console.WriteLine("Hello"); Console.WriteLine(area); Console.WriteLine(”Size {0}x{1}”, width, height); double salary=12000; Console.WriteLine("My salary is {0:f2}.", salary);

Outline C# Beginning Variable and Constant Expression Statement 48 C# Beginning Variable and Constant Expression Statement Math Class

The Math Class Value returned Example Call Result PI Value of π 49 Method/ Constant Value returned Example Call Result PI Value of π Math.PI 3.1415927 Max(x,y) Larger of the two Math.Max(1,2) 2 Abs(x) Absolute value of x Math.Abs(-1.3) 1.3 Sqrt(x) Square-root of x Math.Sqrt(4.0) 2.0 Round(x) Nearest integer to x Math.Round(0.8) 1 Pow(x,y) xy Math.Pow(3,2) 9.0 Log(x) Natural log of x Math.Log(10) 2.302585 Ceiling(x) Smallest integer greater than or equal to x Math.Ceiling(4.1) 5 Cos(x) Cosine of x radians Math.Cos(Math.PI) -1

Test III Write the program which Input : Your name 50 Write the program which Input : Your name Output : Your name is <your name>.

Test IV Write the program which Input : 3 number 51 Write the program which Input : 3 number Output : average of 3 input number

Test VI Write the program which Input : lenght of radius of circle 52 Write the program which Input : lenght of radius of circle Output : Area of circle

Any question?

Selection Statement

Outline Boolean expression if statement nested if statement Flowchart * Boolean expression if statement nested if statement Flowchart

Boolean Expression Boolean Operators Comparison Equal == Not equal != * Operators Comparison Equal == Not equal != Less < Greater > Less than or equal to <= Greater than or equal to >= Boolean And && Or || Not ! ! Exclamation mark 0 and 0 = 0 0 and 1 = 0 1 and 0 = 0 1 and 1 = 1 0 or 0 = 0 0 or 1 = 1 1 or 0 = 1 1 or 1 = 1 not 0 = 1 not 1 = 0

Boolean Expression Example * From the equation: X2+9X+10 = 0 How can we check that value of X is the answer for above equation? Condition: Is value Y even number? ((X*X +9*X +10) == 0) //true if X is the answer (Y%2 == 0) //true if Y is even OR (Y%2 != 1) //true if Y is even

Example: Boolean Expressions * double x = 4.0; Expression Value x < 5.0 ___________ x > 5.0 ___________ x <= 5.0 ___________ 5.0 == x ___________ x != 5.0 ___________ (3!=4)&&(7<5) ___________ (4>4)||(5<=10) ___________ true false true false true false true

Outline Boolean expression if statement nested if statement * Boolean expression if statement nested if statement switch case statement Flowchart

ที่มาของภาพ http://www.ryt9.com/s/prg/774090 if statement * ที่มาของภาพ http://www.ryt9.com/s/prg/774090 10

if statement if statement * Execute the specific statement when the ”condition” becomes true Syntax: true true if (condition) { statement; } if (condition) { statement1; statement2; }

if statement if statement * Execute the specific statement when the ”condition” becomes true Syntax: true if (condition) statement; if (condition) { statement1; statement2; } true if (condition) { statement; }

if statement condition if (condition) { statement; } False True * condition False True Statement if (condition) { statement; }

if statement if (condition) statement; condition if (condition) { * if (condition) statement; condition False True Statement if (condition) { statement; }

if statement if (condition) { statement1; statement2; condition } True * if (condition) { statement1; statement2; } condition False True Statement1 Statement2

if statement price = 40; price = 40; false true * price = 40; height<=140 price = 40; if (height <= 140) { Console.WriteLine (“Hello Children!”); price = 0; } Console.WriteLine (“price ={0}”,price); false true Console.WriteLine (“Hello children”); price = 0; Console.WriteLine (“price = {0}”, price) 12

การควบคุมการไหลของโปรแกรม โปรแกรมทั่วไปจะทำงานเป็นเส้นตรง x = int.Parse(Console.ReadLine()); y = int.Parse(Console.ReadLine()); Console.WriteLine(x+y) Console.WriteLine("Hello {0}",x) z = x * y + 10; Console.WriteLine(z) 14

การควบคุมการไหลของโปรแกรม โปรแกรมที่ใช้คำสั่ง if height <= 140 True False price = 40; if (height <= 140) { Console.WriteLine("Hello kids!"); price = 0; } Console.WriteLine("price={0}",price); เมื่อ height = 160 เมื่อ height = 120 15

ที่มาของภาพ http://splinedoctors.com/2009/02/hurry-up-and- choose/ if – else statements ที่มาของภาพ http://splinedoctors.com/2009/02/hurry-up-and- choose/ 25

คำสั่ง if-else คำสั่ง if คำสั่ง if-else 26

if…else… statement If condition is true → execute statement1 if statement if…else… statement * If condition is true → execute statement1 If condition is false → execute statement2 Syntax: if (condition) statement1; //true else statement2; //false if (condition) statement1; //true else { statement2; //false statement3; //false }

if…else… statement if (condition) { statement1; //true } else { if statement if…else… statement * condition False True Statement2 Statement1 if (condition) { statement1; //true } else { statement2; //false }

if…else… statement if (condition) { statement1; //true } else { if statement if…else… statement * condition False True Statement2 Statement1 Statement3 if (condition) { statement1; //true } else { statement2; //false statement3; //false }

if…else… statement example if statement if…else… statement example * Write the program which check input number. input : integer number output : message to inform that number is odd or even. Value in N Output Even Number It’s even number. Odd Number It’s odd number.

if…else… statement example if statement if…else… statement example * Value in N Output Even Number It’s even number. Odd Number It’s odd number. if(n%2 == 0) { Console.WriteLine(“It’s even number”); } else { Console.WriteLine(“It’s odd number”); }

การควบคุมการไหลของโปรแกรม Console.WriteLine ("It is an even number"); n % 2 == 0 True False Console.WriteLine ("It is an odd number"); 30

if statement Test I * Write the program which decide result of the examination from student’s score input : number output : score output less than 50 fail otherwise pass

Test II Write the program which find the value of function if statement Test II * Write the program which find the value of function input : number output : f(x) x x2+1 x<0 x+2 x≥0

Thinking corner x > 0 x == 0 x < 0 * Write the program which decide type of integer input - positive, zero or negative integer. x > 0 x == 0 x < 0

Outline Boolean expression if statement nested if statement * Boolean expression if statement nested if statement switch case statement FlowChart

มุมนักคิด x > 0 x < 0 x == 0 x > 0 x < 0 True False True 34

Nested if statement int N; N = int.Parse(Console.ReadLine()); * int N; N = int.Parse(Console.ReadLine()); if (N > 0) { Console.WriteLine(“N is positive number”); else if (N==0) Console.WriteLine(“N is zero number”); Console.WriteLine(“N is negative number”); if#1 if#2

Nested if statement int N; N = int.Parse(Console.ReadLine()); * int N; N = int.Parse(Console.ReadLine()); if (N >= 0) { if (N==0) Console.WriteLine(“N is zero number”); else Console.WriteLine(“N is positive number”); } Console.WriteLine(“N is negative number”); if#1 if#2

Exercise 1: Separated IF (simple) * 1. Have you eaten lunch? 2. Do you like noodle? if (eaten==true) {Console.WriteLine(“ I’ve already eaten”);} else {Console.WriteLine(“ I’ve not eat yet”);} if (like_noodle==true) {Console.WriteLine(“I like noodle”);} {Console.WriteLine(“I don’t like noodle”);}

Exercise 2: Related two IF (full version) * 1. Do you like noodle? 2. If you don’t like noodle, do you like fried rice? if (like_noodle==true) {Console.WriteLine(“I like noodle”);} else { Console.WriteLine(“I don’t like noodle”); if (like_friedrice==true) {Console.WriteLine(“I like fried rice”);} {Console.WriteLine(“I don’t like fried rice”);} }

Exercise 2: Related two IF (short version) * 1. Do you like noodle? 2. If you don’t like noodle, do you like fried rice? if (like_noodle==true) {Console.WriteLine(“I like noodle”);} else if (like_friedrice==true) {Console.WriteLine(“I like friedrice”);} else {Console.WriteLine(“I don’t like friedrice”);}

Exercise 3: Nested two IF (short version) * 1. Do you like noodle? 1.1 If you like noodle, do you love Sen-Yai? 1.2 If you don’t like noodle, do you like fried rice? if (like_noodle==true) {Console.WriteLine(“I like noodle”); if (love_SenYai==true) {Console.WriteLine(“I love Sen-Yai”);} else {Console.WriteLine(“I don’t love Sen-Yai”);} } else if (like_friedrice==true) {Console.WriteLine(“I like fried rice”);} {Console.WriteLine(“I don’t like fried rice”);}

Nested if statement example * Write the program which show student’s grade input : score of student output : Score Grade 80 – 100 A 70 – 79 B 60 – 69 C 50 – 59 D 0 – 49 F

Test III Selfish Ratio Recieving Selfish Ratio = Giving Ratio Output if statement Selfish Ratio Selfish Ratio = Recieving Giving Ratio Output More than 1 You are selfish 1 You break even Less than 1 You are giver

Nested if statement Test III * Write the program which calculation value of following function input : value of x output : function value of x according with ... f(x) = 2x+10, x ≤ 5 x2+10, 5 < x ≤ 20 x3+10, x > 20

Outline Boolean expression if statement nested if statement Flowchart * Boolean expression if statement nested if statement Flowchart

Flowchart Symbols Overview * Graphical representation Terminator Process Input/output ข้าวหลามตัด = diamond Condition Connector Flow line

Program Flowchart Example * Start Statement1 Statement2 Statement3 Statement4 End

if statement flowchart Start * statement1; if (condition) statement2; //true else { statement3; //false } statement4; Statement1 Condition true false Statement2 Statement3 Statement4 End

if statement flowchart Start * n=int.Parse(Console.ReadLine()); n= int.Parse(Console.ReadLine()); if (n>0) n= 2*n+5; else { Console.Write(“Go”); n = n%4; } n>0 false true Console. Write(“Go”); n=2*n+5; n=n%4; End

Any question?