95
Introduction to C# Programming ณณณณณ ณณณณณณ 1204452 Application Program Development

Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

Embed Size (px)

Citation preview

Page 1: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

Introduction to C# Programming

ณภั�ทร สั�กทอง1204452 Application Program Development

Page 2: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

2

Outline2

• Programming Languages• C# Language Overview

Page 3: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

3

Programming Languages3

• 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

Programming Languages

Page 4: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

4

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

Page 5: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

5

Language translator5

Interpreter/ Compiler

Hello World!

_

High-level language•static void Main( ){ Console.WriteLine("Hello World!");}

Assembly language•pushl %ebpmovl %esp, %ebpsubl $8, %espandl $-16, %esp

Machine language•000110001100011100011000111010111100011000110001110

Assembler

Machine

Programming Languages

Page 6: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

6

High-Level Languages• Procedural Language

– Fortran– Cobol– Basic– C– Pascal

• Object-Oriented Language– C++– Java– C#

Functional Language

Lisp Logic Language Prolog

6

Programming Languages

Page 7: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

7

Outline7

• Programming Languages• C# Language Overview

Page 8: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

8

A simple C# Program8

Grouping using { }

C# Language Overview

Page 9: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

9

A simple C# Program9

Statement ending with semicolon “;”

C# Language Overview

Page 10: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

10

A simple C# Program10

C# syntax is case-sensitive

namespace NAMEspace

Main() main()

C# Language Overview

Page 11: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

11

A simple C# Program11

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

Page 12: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

12

A simple C# Program12

Anything between /* */ or after // is considered a comment

static void Main(string[] args) //comment here { /* This is comment too. */ Console.WriteLine("Hello World!");}

Comments will not be translated

C# Language Overview

Page 13: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

13

Program Structure

• 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 ... }

static void Main () { ... starting point ... }

13

C# Language Overview

Page 14: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

14

Program Structure• 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

method1

method2

namespace

Class

Class

14

C# Language Overview

Page 15: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

15

Program Structure

• 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(); } }}

namespace HelloW { class HelloWClass { static void Main () { System.Console.WriteLine("Hello World!"); System.Console.ReadLine(); } }}

15

C# Language Overview

Page 16: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

16

Naming Rules

• 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

C# Language Overview

16

Page 17: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

17

Naming Rules

• Letters, digits and underscores(_)• First character letter or _• Up to 63 characters long• Must not be a reserved word

C# Language Overview

17

Example

name

Name

_data

9point

class class_A class_”A”

point9

Page 18: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

18

C# Reserved WordsC# Language Overview

18

Page 19: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

Any question?

Page 20: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

C# Basic Concept

Page 21: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

22

Outline22

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

Page 22: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

23

C# Beginning23

• 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 ... }

static void Main () { ... starting point ... }

C# Beginning

Page 23: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

24

24

Inside method Main

• 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); }

C# Beginning

Page 24: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

25

Outline25

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

Page 25: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

26

What is Variable?

• A variable is used to store “data.”

“It must be declared before used”

Variable & Constant

26

Page 26: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

27

Data Types

Type Size Description Range

bool 1 byte Store truth value true / false

char 1 byte Store one character character code 0 – 255

byte 1 byte Store positive integer 0 – 255

short 2 byte Store integer -32,768 -- 32,767

int 4 byte Store integer -2.1 x 109 -- 2.1 x 109

long 8 byte Store integer -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

N/A

Variable & Constant

27

Page 27: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

28

C# Variable Declaration

• 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;

Variable & Constant

28

Page 28: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

29

C# Variable Declaration29

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;

Page 29: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

30

Test I - Variable Declaration30

• 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

Page 30: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

31

C# Constant Declaration

• 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’;

Variable & Constant

31

Page 31: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

32

C# Constant Declaration

• 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;

Variable & Constant

32

Page 32: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

33

Test II - Constant Declaration33

• Declare Constant– Name : e– Type : double– Initial value : 2.71828

Page 33: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

34

Outline34

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

Page 34: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

35

C# Expression35

Expression

Arithmetic Expression

Boolean Expression

Expression

Page 35: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

36

Arithmetic Expression

• Operators– + - * /– % (remainder after division)

• Example– 11 + 5 – 39 / 5 – 39.0/5 – 39 % 5 – 5.0 % 2.2

Expression

167

4

0.6

36

7.8

Page 36: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

37

Piority of Arithmetic Operators

Priority operator

1 Parentheses ()

2 *, / , %

3 +, -

4 If equal precedence, left to right

int a, b;a = 2-10*3/5+(6%4);b = 5*(15%4+(2-3))/9;

a = -2b = 1

Answer

37

Expression

Page 37: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

38

Calculation Priority38

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;

}

d = 2e = 0f = 2.5g = 2.5

Answer

Expression

Page 38: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

39

Boolean Expression

• Operators– Comparison

• Equal ==• Not equal !=• Less <• Greater >• Less than or equal to <=• Greater than or equal to >=

– Boolean• And &&• Or ||• Not !

0 and 0 = 00 and 1 = 01 and 0 = 01 and 1 = 1

0 or 0 = 00 or 1 = 11 or 0 = 11 or 1 = 1

not 0 = 1not 1 = 0

Expression

39

Page 39: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

40

Example: Boolean Expression

• 10 > 50 • ’A’ < ’B’ • (3<2) || (2+5 > 6) • (’a’ != ’z’) && !(9==0)

falsetrue

Expression

40

truetrue

Page 40: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

41

Outline41

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

Page 41: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

42

Statements• A statement is a unit of command to instruct

your program• A method consists of one or more statementsclass Hello { static void Main () { Console.WriteLine("Hello World!"); Console.ReadLine(); }}

class Hello { static void Main () { Console.WriteLine("Hello World!"); Console.ReadLine(); }}

Statement#1

Statement#2

Statement

42

Page 42: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

43

C# Statement Types43

C# Statement TypesAssignm

ent Statement

Input Statement

Output Statement

Statement

Page 43: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

44

Assignment Statement

• Assigning value to variable• Use the equal sign (=) when making

assignments. • Syntax:

– <variable> = <expression>;

int Width,High;Width=10;High=20+Width;

Statement

44

Page 44: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

45

Input Statement

• 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

Examplestring st;st = Console.ReadLine();

Statement

45

Page 45: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

46

Example: Input Statement

• Ex1:• string myname;• myname = Console.ReadLine();

Ex2:int Width;

string temp1;temp1 = Console.ReadLine();Width = int.Parse(temp1);

Statement

46

Page 46: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

47

Output Statements

• 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(”Size {0}x{1}”, width, height);

double salary=12000;Console.WriteLine("My salary is {0:f2}.", salary);

Console.WriteLine("Hello");Console.WriteLine(area);

Statement

47

Page 47: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

48

Outline48

• C# Beginning• Variable and Constant• Expression• Statement• Math Class

Page 48: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

49

The Math Class

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

Math Class

49

Page 49: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

50

Test III50

• Write the program which– Input : Your name– Output : Your name is <your name>.

Page 50: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

51

Test IV51

• Write the program which– Input : 3 number– Output : average of 3 input number

Page 51: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

52

Test VI52

• Write the program which– Input : lenght of radius of circle– Output : Area of circle

Page 52: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

Any question?

Page 53: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

*

Selection Statement

Page 54: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

**

Outline*

•Boolean expression•if statement•nested if statement•Flowchart

Page 55: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

**

Boolean Expression

•Operators• Comparison

• Equal ==• Not equal !=• Less <• Greater >• Less than or equal to <=• Greater than or equal to >=

BooleanAnd &&

Or ||

Not !

*

Boolean Expression

0 and 0 = 00 and 1 = 01 and 0 = 01 and 1 = 1

0 or 0 = 00 or 1 = 11 or 0 = 11 or 1 = 1

not 0 = 1not 1 = 0

Page 56: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

**

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

*

Boolean Expression

Page 57: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

**

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) ___________

truefalsetruefalsetrue

*

Boolean Expression

falsetrue

Page 58: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

**

Outline*

•Boolean expression•if statement•nested if statement•switch case statement•Flowchart

Page 59: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

** *

if statement

10

ท�มาของภัาพ http://www.ryt9.com/s/prg/774090

Page 60: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

**

if statement

•Execute the specific statement when the ”condition” becomes true•Syntax:

if (condition) {

statement;

}

if (condition) {

statement1;

statement2;

}

*

if statement

true

true

Page 61: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

**

if statement

•Execute the specific statement when the ”condition” becomes true•Syntax:

if (condition)

statement;

if (condition) {

statement1;

statement2;

}if (condition) {

statement;

}

*

if statement

true

true

Page 62: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

**

if statement*

condition False

True

Statement

if (condition) {

statement;

}

if statement

Page 63: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

**

if statement*

condition False

True

Statement

if (condition)

statement;

if (condition) {

statement;

}

if statement

Page 64: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

**

if statement*

condition

FalseTrue

Statement1

Statement2

if (condition) {

statement1;

statement2;

}

if statement

Page 65: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

** *

if statement

•price = 40;

•if (height <= 140) {• Console.WriteLine (“Hello Children!”);• price = 0;•}

•Console.WriteLine(“price ={0}”,price);

12

height<=140

Console.WriteLine(“Hello children”);

true

price = 0;

false

Console.WriteLine(“price = {0}”, price)

price = 40;

Page 66: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

*

การควบค�มการไหลของโปรแกรม•โปรแกรมท��วไปจะท�างานเป�นเสั�นตรง

14

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)

Page 67: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

*

การควบค�มการไหลของโปรแกรม• โปรแกรมท�ใช้�คำ�าสั��ง if

15

price = 40;

if (height <= 140) {

Console.WriteLine("Hello kids!");

price = 0;

}

Console.WriteLine("price={0}",price);

เม �อ height = 120

height <= 140 True

False

เม �อ height = 160

Page 68: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

*

if – else statements

25

ที่��มาของภาพhttp://splinedoctors.com/2009/02/hurry-up-and-

choose/

Page 69: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

*

ค�าสั่��ง if-else• คำ�าสั��ง if • คำ�าสั��ง if-else

26

Page 70: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

**

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 statement

Page 71: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

**

if…else… statement*

condition FalseTrue

Statement2Statement1

if (condition) {

statement1; //true

} else {

statement2; //false

}

if statement

Page 72: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

**

if…else… statement*

if (condition) {

statement1; //true

} else {

statement2; //false

statement3; //false

}

condition FalseTrue

Statement2

Statement1

Statement3

if statement

Page 73: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

**

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 statement

Page 74: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

**

if…else… statement example

*

Value in N OutputEven Number It’s even number.

Odd Number It’s odd number.

if statement

if(n%2 == 0) {

Console.WriteLine(“It’s even number”);

} else {

Console.WriteLine(“It’s odd number”);}

Page 75: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

*

การควบค�มการไหลของโปรแกรม

30

n % 2 == 0 True

False

Console.WriteLine("It is an even number");

Console.WriteLine("It is an odd number");

Page 76: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

**

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

if statement

Page 77: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

**

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

if statement

Page 78: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

**

Thinking corner

•Write the program which decide type of integer input - positive, zero or negative integer.

*

x == 0

x > 0

x < 0

Page 79: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

**

Outline*

•Boolean expression•if statement•nested if statement•switch case statement•FlowChart

Page 80: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

*

ม�มนั�กค�ด

34

x == 0

x > 0

x < 0

x > 0 True

False

True

False

x < 0

Page 81: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

**

Nested if statement

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”); else Console.WriteLine(“N is negative number”);

if#1

if#2

*

Nested if statement

Page 82: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

**

Nested if statement

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”); }else Console.WriteLine(“N is negative number”);

if#1

*

Nested if statement

if#2

Page 83: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

**

Exercise 1: Separated IF (simple)

•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”);}•else• {Console.WriteLine(“I don’t like noodle”);}

1. Have you eaten lunch?2. Do you like noodle?

*

Page 84: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

**

•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”);}• else• {Console.WriteLine(“I don’t like fried rice”);}• }

1. Do you like noodle? 2. If you don’t like noodle, do you like fried

rice?

Exercise 2: Related two IF (full version)*

Page 85: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

**

•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”);}

1. Do you like noodle? 2. If you don’t like noodle, do you like fried

rice?

Exercise 2: Related two IF (short version)*

Page 86: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

**

•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”);}•else• {Console.WriteLine(“I don’t like fried rice”);}

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?

Exercise 3: Nested two IF (short version)*

Page 87: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

**

Nested if statement example*

•Write the program which show student’s grade

• input : score of student• output :

Score Grade

80 – 100 A70 – 79 B60 – 69 C50 – 59 D0 – 49 F

Nested if statement

Page 88: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

*

if statement

Test III

•Selfish Ratio

Ratio Output

More than 1 You are selfish

1 You break even

Less than 1 You are giver

Selfish Ratio =Recieving Giving

Page 89: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

**

Test III*

•Write the program which calculation value of following function

• input : value of x• output : function value of x according with ...

Nested if statement

f(x) =

2x+10, x ≤ 5

x2+10, 5 < x ≤ 20x3+10, x > 20

Page 90: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

**

Outline*

•Boolean expression•if statement•nested if statement•Flowchart

Page 91: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

**

Flowchart Symbols Overview

•Graphical representation

Terminator

Process

Input/output

Condition

Connector

Flow line

*

Flowchart

Page 92: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

**

Program Flowchart Example

Start

Statement1

Statement2

Statement3

Statement4

End

*

Flowchart

Page 93: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

**

if statement flowchart

statement1;

if (condition)

statement2; //true

else {

statement3; //false

}

statement4;

Start

Statement1

Condition

Statement2 Statement3

true false

Statement4

End

*

Flowchart

Page 94: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

**

if statement flowchart*

n= int.Parse(Console.ReadLine());

if (n>0)

n= 2*n+5;

else {

Console.Write(“Go”);

n = n%4;

}

Start

n>0

n=2*n+5;

true false

n=n%4;

End

n=int.Parse(Console.ReadLine());

Console.Write(“Go”)

;

Page 95: Introduction to C# Programming ณภัทร สักทอง 1204452 Application Program Development

*

Any question?