1 nd semester 2007 1 module2 c# basic concept thanawin rakthanmanon computer engineering...

Post on 19-Jan-2018

222 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

1 nd Semester C# Program  Consider all the programs you wrote in Lab#0  What C#'s programming rules can you derive? 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(); }

TRANSCRIPT

11nd Semester 2007

ModuleModule22 C# Basic ConceptC# Basic Concept

Thanawin RakthanmanonEmail: fengtwr@ku.ac.th

Computer Engineering DepartmentKasetsart University, Bangkok THAILAND

21nd Semester 2007

Outline

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

31nd Semester 2007

C# Program

Consider all the programs you wrote in Lab#0

What C#'s programming rules can you derive?

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

41nd Semester 2007

C# Program

C# syntax is case-sensitive Every statement ends with a semicolon

; White space means nothing Code block is inside braces { } Anything between /* */ or after // is

considered a comment Comments will not be translated

51nd Semester 2007

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

61nd Semester 2007

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

method1method1method2method2

nnamespaceamespaceClassClass

ClassClass

71nd Semester 2007

Program Structure (example)

nnamespaceamespace

method1method1method2method2 ClassClass

ClassClass

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

methodmethod33

method1method1method2method2 ClassClass

nnamespaceamespace

::

81nd Semester 2007

Program Structure

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

91nd Semester 2007

Naming Rules

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

* Case Sensitive * Case Sensitive ExampleExample

KU6KU666 ≠ kU6 ≠ kU666 ≠ ≠ ku6ku666

C# OverviewC# Overview

101nd Semester 2007

C# Reserved WordsC# OverviewC# Overview

111nd Semester 2007

Short break – 3 Minutes

121nd Semester 2007

Outline

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

131nd Semester 2007

What is Variable?

A variable is used to store “data.”

““It must be It must be declareddeclared before used” before used”

Variable & ConstantVariable & Constant

141nd Semester 2007

C# Variable Declaration

Syntax:<data type> <name>;

Example:

We can also assign its initial value. Example:

int radius;double area;int a,b,c;bool isokay;

int k = 200;bool done = false;

Variable & ConstantVariable & Constant

151nd Semester 2007

Data Types

Type Size Description Rangebool 1 byte Store truth value true / false char 1 byte Store one

charactercharacter code 0 – 255

byte 1 byte Store positive integer

0 – 255

short 2 byte Store integer -32,768 -- 32,767int 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

161nd Semester 2007

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

Variable & ConstantVariable & Constant

171nd Semester 2007

Outline

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

181nd Semester 2007

C# Expression

Arithmetic Expression Boolean Expression

ExpressionExpression

191nd Semester 2007

Arithmetic Expression

Operators+ - * /% (remainder after division)

Example 11 + 5 16 11 / 2 5 11 % 2 1 5.0 % 2.2 0.6

ExpressionExpression

201nd Semester 2007

Precedence rules for Arithmetic Operators1. ( ) parentheses2. *, / , % 3. + –4. If equal precedence, left to right

int Width,High;int Width,High;WidthWidth == 10*5+(16*12)/5;10*5+(16*12)/5;High= (16+5)+20%2;High= (16+5)+20%2;

211nd Semester 2007

Calculation Priority

public static void Main(){int a,b,c,d;a=1; b=2; c=3;d = c/b*a;Console.WriteLine("d={0}",d);d = a/b;Console.WriteLine("d={0}",d);

}

Console.WriteLine(3/4*8);= Console.WriteLine((3/4)*8);

= 0 (= )

Calculate from left to right!Calculate from left to right!

3344 xx 88? ???

221nd Semester 2007

Expression in C#

Arithmetic Expression Boolean Expression

ExpressionExpression

231nd Semester 2007

Boolean Expression Operators

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

Boolean And &&&& Or |||| Not ! !

ExpressionExpression

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

241nd Semester 2007

Example: Boolean Expression

50 > 10 true ’A’ < ’B’ true ’ANT’ < ’B’ true ’5’ > ’1’ true ’5’ > ’10’ true ’5’ > ’100’ true

251nd Semester 2007

Quiz

((1 and 0) or (1 and 1)) = ? What is I/O device ? What is RAM and ROM ? 1 byte = xxx bits 1 Mbyte = ? C# is xxx language. 10000102 = ?

261nd Semester 2007

Short break – 5 Minutes

271nd Semester 2007

Outline

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

281nd Semester 2007

Statements A statement is a unit of command to

instruct your program

A method consists of one or more statements

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

Statement#1Statement#1

Statement#2Statement#2

291nd Semester 2007

C# Statement Types Assignment Statement Input Statement Output Statement

StatementStatement

301nd Semester 2007

Assignment Statement

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

making assignments. Syntax:

<<variablevariable> = <> = <expressionexpression>;>;

int Width,High;int Width,High;Width=10;Width=10;High=5;High=5;

int Width = 10;int Width = 10;int High = 5;int High = 5;

StatementStatement

311nd Semester 2007

C# Statement Types Assignment Statement Input Statement Output Statement

StatementStatement

321nd Semester 2007

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

ExampleExamplestring st;string st;st = System.Console.ReadLine();st = System.Console.ReadLine();

StatementStatement

331nd Semester 2007

string st; int i; double d; 1. st = Console.ReadLine();

i = int.Parse(st);

2. st = Console.ReadLine();d = double.Parse(st);

3. i = int.Parse(Console.ReadLine());

4. d = double.Parse(Console.ReadLine());

Example: Input Statement

341nd Semester 2007

Example: Input Statement

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

Ex2:Ex2:int Width,High;int Width,High;string temp1;string temp1;temp1 = System.Console.ReadLine();temp1 = System.Console.ReadLine();Width = int.Parse(temp1);Width = int.Parse(temp1);temp1 = System.Console.ReadLine();temp1 = System.Console.ReadLine();HighHigh = int.Parse(temp1); = int.Parse(temp1);

StatementStatement

351nd Semester 2007

Example: Input Statement

StatementStatement

361nd Semester 2007

C# Statement Types Assignment Statement Input Statement Output Statement

StatementStatement

371nd Semester 2007

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

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

More information about formattingMore information about formattinghttphttp://://msdnmsdn..microsoftmicrosoft..comcom//librarylibrary//enen--usus//csrefcsref//htmlhtml//vclrfFormattingNumericResultsTablevclrfFormattingNumericResultsTable..aspasp

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

381nd Semester 2007

Outline

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

391nd Semester 2007

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 increment int a = 5;int b = a- - ; // a = 4, b = 5

Pre in/decrement: Use the value which has already been in-

decrement. Post in-decrement:

Use the value before in-decrement

401nd Semester 2007

Modify-And-Assign OperationsStatement 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)

int int yy==88; ; int int aa==22;;

ConsoleConsole..WriteLineWriteLine((y y -= -= 33++aa););

Try this !Try this !

411nd Semester 2007

Operator Priority

( )

++(x) , --(x), +(x), -(x)

* / %+ -= , += , -=, *=, /=, %=(x)++ , (x)--

Highest Priority (Do First)

Lowest Priority (Do Last)

421nd Semester 2007

The Math Class

Method/Constant Value returned Example Call Result

PI Value of Math.PI 3.1415927Max(x,y) Larger of the two Math.Max(1,2) 2Abs(x) Absolute value of x Math.Abs(-1.3) 1.3Sqrt(x) Square-root of x Math.Sqrt(4.0) 2.0Round(x) Nearest integer to x Math.Round(0.8) 1Pow(x,y) xy Math.Pow(3,2) 9.0Log(x) Natural log of x Math.Log(10) 2.302585Ceiling(x) Smallest integer greater

than or equal to xMath.Ceiling(4.1) 5

Cos(x) Cosine of x radians Math.Cos(Math.PI) -1

431nd Semester 2007

Summary

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

SummarySummary

top related