36
C# Basics & Math Thanachat Thanomkulabut

C# Basics & Math Thanachat Thanomkulabut. Outline 2 Review Data Types Variable and Constant Declaration Arithmetic Expression Statement

Embed Size (px)

Citation preview

Page 1: C# Basics & Math Thanachat Thanomkulabut. Outline 2  Review  Data Types  Variable and Constant Declaration  Arithmetic Expression  Statement

C# Basics & MathThanachat Thanomkulabut

Page 2: C# Basics & Math Thanachat Thanomkulabut. Outline 2  Review  Data Types  Variable and Constant Declaration  Arithmetic Expression  Statement

Outline2

Review Data Types Variable and Constant Declaration Arithmetic Expression Statement

Page 3: C# Basics & Math Thanachat Thanomkulabut. Outline 2  Review  Data Types  Variable and Constant Declaration  Arithmetic Expression  Statement

Lecture 1: Program Structure

3

The starting point of the program is:

This is known as the Main method A method is put inside a class A class can be put inside a namespace

C# Beginning

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

Page 4: C# Basics & Math Thanachat Thanomkulabut. Outline 2  Review  Data Types  Variable and Constant Declaration  Arithmetic Expression  Statement

Lecture 1: Variable Naming Rules

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

4

Example

name Name

_data

9point

class class_A class_"A"

point9

C# Beginning

Page 5: C# Basics & Math Thanachat Thanomkulabut. Outline 2  Review  Data Types  Variable and Constant Declaration  Arithmetic Expression  Statement

Outline5

Review Data Types Variable and Constant Declaration Arithmetic Expression Statement

Page 6: C# Basics & Math Thanachat Thanomkulabut. Outline 2  Review  Data Types  Variable and Constant Declaration  Arithmetic Expression  Statement

6

What is in a program?

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 7: C# Basics & Math Thanachat Thanomkulabut. Outline 2  Review  Data Types  Variable and Constant Declaration  Arithmetic Expression  Statement

What is Variable?

A variable is used to store some “data.”

“It must be declared before it can be used”

Variable & Constant

7

Page 8: C# Basics & Math Thanachat Thanomkulabut. Outline 2  Review  Data Types  Variable and Constant Declaration  Arithmetic Expression  Statement

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

8

Page 9: C# Basics & Math Thanachat Thanomkulabut. Outline 2  Review  Data Types  Variable and Constant Declaration  Arithmetic Expression  Statement

Outline9

Review Data Types Variable and Constant Declaration Arithmetic Expression Statement

Page 10: C# Basics & Math Thanachat Thanomkulabut. Outline 2  Review  Data Types  Variable and Constant Declaration  Arithmetic Expression  Statement

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

10

Page 11: C# Basics & Math Thanachat Thanomkulabut. Outline 2  Review  Data Types  Variable and Constant Declaration  Arithmetic Expression  Statement

Multiple-Variable Declaration

11

Syntax:<data type> <name_1>, <name_2>,...,

<name_n>;

Example:

We can also assign their initial value. Example:

int width, length, height;double mean, sd, max, min;bool isokay, isright, check;

int width=5, length=2, height=4;

Variable & Constant

Page 12: C# Basics & Math Thanachat Thanomkulabut. Outline 2  Review  Data Types  Variable and Constant Declaration  Arithmetic Expression  Statement

Test : Variable Declaration12

Declare variable1 Name :

num_Student Type : integer Initial value :

nothing

Declare variable2 Name : gender Type : character Initial value : m

int num_Student; char gender = ‘m’;

Variable & Constant

Page 13: C# Basics & Math Thanachat Thanomkulabut. Outline 2  Review  Data Types  Variable and Constant Declaration  Arithmetic Expression  Statement

Test : Multiple-Variable Declaration

13

Declare variables 1,2,3 Name1 : u Name2 : t Name3 : a Type : double Initial value1 : 5.0 Initial value2 : nothing Initial value3 : 9.8

double u=5.0, t, a=9.8;

Variable & Constant

Page 14: C# Basics & Math Thanachat Thanomkulabut. Outline 2  Review  Data Types  Variable and Constant Declaration  Arithmetic Expression  Statement

Constant Declaration

Syntax:const <data type> <name> =

<value>;

Example:const int radius = 15;const double area=1.5;const bool is_done=true;const string movie=”StarWarIII”;const char my_initial=‘m’;

Variable & Constant

14

Page 15: C# Basics & Math Thanachat Thanomkulabut. Outline 2  Review  Data Types  Variable and Constant Declaration  Arithmetic Expression  Statement

Multiple-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, width=3.2, length = 4.1;

Variable & Constant

15

Page 16: C# Basics & Math Thanachat Thanomkulabut. Outline 2  Review  Data Types  Variable and Constant Declaration  Arithmetic Expression  Statement

Test : Constant Declaration16

Declare Constant Name : e Type : double Initial value :

2.71828const double

e=2.71828;

Page 17: C# Basics & Math Thanachat Thanomkulabut. Outline 2  Review  Data Types  Variable and Constant Declaration  Arithmetic Expression  Statement

Outline17

Review Data Types Variable and Constant Declaration Arithmetic Expression Statement

Page 18: C# Basics & Math Thanachat Thanomkulabut. Outline 2  Review  Data Types  Variable and Constant Declaration  Arithmetic Expression  Statement

C# Expression18

Expression

Arithmetic

Expression

Boolean Expressi

on

Expression

Page 19: C# Basics & Math Thanachat Thanomkulabut. Outline 2  Review  Data Types  Variable and Constant Declaration  Arithmetic Expression  Statement

Arithmetic Expression

Operators + - * / % (modulo: remainder after division)

Integer operates Integer IntegerInteger/Real operates Real/Integer

RealExample

11 + 5 39 / 5 39.0/5 39 % 5 5.0 % 2.2

Arithmetic

16

7

40.6

19

7.8

Page 20: C# Basics & Math Thanachat Thanomkulabut. Outline 2  Review  Data Types  Variable and Constant Declaration  Arithmetic Expression  Statement

Example20

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

Arithmetic

Page 21: C# Basics & Math Thanachat Thanomkulabut. Outline 2  Review  Data Types  Variable and Constant Declaration  Arithmetic Expression  Statement

Pre/Post Increment & Decrement

Operator Meaning example

++x pre increment int a = 5;int b = ++a; // a, b = 6

x++ post increment int a = 5;int b = a++; // a = 6, b = 5

--x pre decrement int a = 5;int b = --a; // a, b = 4

x-- post decrement int a = 5;int b = a- - ; // a = 4, b = 5

Pre in/de-crement: Increment/decremente the value first, then use the

value Post in/de-crement:

Use the value first, then increment/decrement the value

In/Decrement

21

Page 22: C# Basics & Math Thanachat Thanomkulabut. Outline 2  Review  Data Types  Variable and Constant Declaration  Arithmetic Expression  Statement

Pre & Post Increment22

Post:

int a=5;

int b=a++;

Console.WriteLine(“a={0}, b={1}”,a,b);

a b5 56 a=6, b=5

Monitor

Pre:

int a=5;

int b=++a;

Console.WriteLine(“a={0}, b={1}”,a,b);

a b5 66 a=6, b=6

Monitor

In/Decrement

Page 23: C# Basics & Math Thanachat Thanomkulabut. Outline 2  Review  Data Types  Variable and Constant Declaration  Arithmetic Expression  Statement

Priority of Arithmetic Operators

Priority

Operator

1 Parentheses ()

2 x++, x--

3 ++x, --x

4 *, / , %

5 +, -

6 If equal priority, work from left to right

23

Arithmetic

Page 24: C# Basics & Math Thanachat Thanomkulabut. Outline 2  Review  Data Types  Variable and Constant Declaration  Arithmetic Expression  Statement

Example

int a, b, c;

a = 2-10*3/5+(6%4);

b = 5*(15%4+(2-3))/9;

c = 5+9%2*4-14/4%3

a = -2b = 1c = 9

Answer

24

Arithmetic

Page 25: C# Basics & Math Thanachat Thanomkulabut. Outline 2  Review  Data Types  Variable and Constant Declaration  Arithmetic Expression  Statement

Comparison

x/y*b++

25

2

Given x = 3.0, y = 2.0, b =2, what is the output?

x/y*++b

3

Answer: 4.5 Answer: 3.0

Page 26: C# Basics & Math Thanachat Thanomkulabut. Outline 2  Review  Data Types  Variable and Constant Declaration  Arithmetic Expression  Statement

Modify-and-Assign Operations

Statement Description

var += expression Increment var by the value of expression

var -= expression Decrement var by the value of expression

var *= expression Multiply var by the value of expression, then store the result in var

var /= expression Divide var by the value of expression, then store the result in var

sum += x; // is equivalent to sum = sum + xprod *= 2.5; // is equivalent to prod = prod * 2.5y -= 3+a; // is equivalent to y = y – (3+a)

Modify-And-Assign

26

Page 27: C# Basics & Math Thanachat Thanomkulabut. Outline 2  Review  Data Types  Variable and Constant Declaration  Arithmetic Expression  Statement

Example

sum += x; // is equivalent to sum = sum + xprod *= 2.5; // is equivalent to prod = prod * 2.5y -= 3+a; // is equivalent to y = y – (3+a)

Console.WriteLine(sum);

Console.WriteLine(prod);

Console.WriteLine(y);

Modify-And-Assign

27

double x = 1, sum = 2, prod = 3;int y = 4, a = 5;

37.5-4

Monitor

Page 28: C# Basics & Math Thanachat Thanomkulabut. Outline 2  Review  Data Types  Variable and Constant Declaration  Arithmetic Expression  Statement

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 = ln x Math.Log(10) 2.302585

Floor(x) Greatest integer smaller than or equal to x Math.Floor(4.9) 4

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

28

Page 29: C# Basics & Math Thanachat Thanomkulabut. Outline 2  Review  Data Types  Variable and Constant Declaration  Arithmetic Expression  Statement

Example 29

What is the output?

Console.WriteLine(Math.Round(3.499));

Console.WriteLine(Math.Floor(3.499));

Console.WriteLine(Math.Ceiling(3.499));

334

Monitor

Page 30: C# Basics & Math Thanachat Thanomkulabut. Outline 2  Review  Data Types  Variable and Constant Declaration  Arithmetic Expression  Statement

Example 30

Write arithmetic expressions using Math class cos(π/3) | ln(4) |

Math.Cos(Math.PI/3)

Math.Pow(3,0.2)

Math.Abs(Math.Log(4))

5 3

Page 31: C# Basics & Math Thanachat Thanomkulabut. Outline 2  Review  Data Types  Variable and Constant Declaration  Arithmetic Expression  Statement

Outline31

Review Data Types Variable and Constant Declaration Arithmetic Expression Statement

Page 32: C# Basics & Math Thanachat Thanomkulabut. Outline 2  Review  Data Types  Variable and Constant Declaration  Arithmetic Expression  Statement

Statement

A statement is a command to computer

class 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

32

Page 33: C# Basics & Math Thanachat Thanomkulabut. Outline 2  Review  Data Types  Variable and Constant Declaration  Arithmetic Expression  Statement

C# Statement Types33

C# Statemen

t Types

Assignment

Statement

Input Statemen

t

Output Statemen

t

Statement

Page 34: C# Basics & Math Thanachat Thanomkulabut. Outline 2  Review  Data Types  Variable and Constant Declaration  Arithmetic Expression  Statement

Assignment Statement

To assign value to variable Use equal sign (=)

Syntax:<variable> = <expression>;

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

Statement

34

Page 35: C# Basics & Math Thanachat Thanomkulabut. Outline 2  Review  Data Types  Variable and Constant Declaration  Arithmetic Expression  Statement

Output Statement

Value

Math expression

More usages of output statement will be shown later in this course

Console.WriteLine(a);Console.WriteLine(a+b);Console.WriteLine(Math.Pow(a,b));

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

Statement

35

Page 36: C# Basics & Math Thanachat Thanomkulabut. Outline 2  Review  Data Types  Variable and Constant Declaration  Arithmetic Expression  Statement

Any question?