input/output statements

Post on 31-Dec-2015

15 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

INPUT/OUTPUT Statements. Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand. Variable Type. Special Character. Special Character in C#. Number Conversion. To convert a numberic data type = ( type ) example - PowerPoint PPT Presentation

TRANSCRIPT

1

INPUT/OUTPUT Statements

Dept. of Computer Engineering

Faculty of Engineering, Kasetsart University

Bangkok, Thailand

2

Variable TypeData type

size description bound

bool 1 Byte To keep truth or false value.

true / false

char 1 Byte To keep one character acsii0 ถึ�ง ascii255

byte 1 Byte To keep positive number.

0 ถึ�ง 255

short 2 Bytes To keep short integer. -32,768 ถึ�ง 32,767

int 4 Bytes To keep integer. ประมาณ -2.1 x 106 ถึ�ง 2.1 x 106

long 8 Bytes To keep long integer. ประมาณ -9.2 x 1018 ถึ�ง 9.2 x 1018

float 16 Bytes

To keep real number.

double 32 Bytes

To keep real number. ประมาณ ± 5.0x10-324 ถึ�ง ± 1.7x10308

String Unlimit To keep several characters.

unlimit

3

Special Character Special Character in C#

symbol result

\n Advance to a new line

\t Tab

\\ To type \

\' To type '

\" To type "

4

Number Conversion To convert a numberic data type

<var> = (type) <var>example

byte boy; int imobile; long limit; double donut;

imobile = (int) boy ; limit = (long) boy + (long)imobile;donut = (double) imobile / 10;

boy = (byte)long + 3; imobile = (int)donut – 10;

C# will change numeric data type to the bigger one automatically.

error error

5

Input Statement

คำ�าสั่��ง : Console.ReadLine( );– This command is used to read string from the keyboard.

Usually used with Parse.

Exampledouble num;string alpha;int unit;

num = double.Parse(Console.ReadLine());alpha = Console.ReadLine();unit = int.Parse(Console.ReadLine());

6

Read Statement : Remark ReadLine() is used to read input from keyboard and assign to the

variable and curser start a new line.

The input value is always string.

ReadLine() usually uses with Parse() command : byte.Parse, short.Parse, int.Parse long.Parse, char.Parse, bool.Parse, float.Parse, double.Parse

ReadLine() is used to read a value to a single variable at a time. For example :

double num;

num = double.Parse(Console.ReadLine());

7

Write Statement

Console.WriteLine(); This command is used to display the output on the screen. After the output is displayed, the curser is advanced to a new line.

Console.Write(); This command is used to display the output on the screen. After the output is displayed, the curser remains on the same line next to the last character. (The curser does not advance to a new line)

Example :

Console.WriteLine(“Hello World !!”);

Console.WriteLine(“What is your name? : ”);

8

Write Statement : Syntax I

Write() / WriteLine() formats :

Console.Write( <string> );– Example 1 Console.Write("Hello world !!!\n "); – Example 2 Console.Write("204111");

Console.Write( <var> );– Example 1 Console.Write(num); – Example 2 Console.Write(sum);

Console.Write( <text> , <value0>,…,<valuen> );

– Value can be a variable or a constant and must be referenced in <text> by using the position order such as {0} means value at position 0.

– Example Console.Write("Hi, {0} ", st);

Fix text

Format items

List of value

9

Write Statement : Syntax II

Variables in Console.Write() / Console.WriteLine() must be referenced by using its position symbol.

{0} means the 0th variable, {1} means the 1st variable and ….

Double quote must be used to enclose text and the position symbol of variable.

ExampleConsole.Write("X is {0}" , x);

Console.WriteLine("X+Y is {0}" , x+y);

Console.WriteLine("{0} + {1} = {2}",x,y,x+y);

Console.WriteLine(“Hello {0} how are you?”, stdName);

10

Write Statement : Example

11

Write Statement : Formatting Output

Syntax : {index [,alignment] [:formatString]}

The symbol {} is used to define starting and ending points of the text required to be formatted

index means the position of variables which start from 0, 1, 2,… respectively.

The same position can be referenced more than once.– Example: Console.WriteLine(“{0} - {0} = 0”,a);

alignment means printing justification. if position number , right alignment. If negative number, left alignment.

– Example: Console.WriteLine(“{0,-5} = {0,5}” , 10 , 5+5)

formatString is to define a format of text which is required to display on screen.

– Example: Console.WriteLine(“{0: dd mm yyyy}” , DateTime.Now);

12

Write Statement : Formatting Output

Symbols which are used in Console.WriteLine() statement to

define output format.

Symbol meaning example result

D or d Decimal Console.WriteLine(“ {0:D} ",10}; 10

E or e Exponential Console.WriteLine(“ {0:E} ",10}; 1.000000E+001

F or f Fix Point Console.WriteLine(“ {0:F} ",10}; 10.00

X or x Hexadecimal Console.WriteLine(“ {0:X} ",10}; A

P or p Percent Console.WriteLine(“ {0:P} ",10}; 1,000.00 %

13

Operators

symbol Number of variable meaning example

+ , - 1 Symbol the represent positive or negative number.

-5, -3, -a, +b

+, - 2 Addition and subtraction operators of 2 operands.

5+3 , a+10

* 2 Multiplication operator of two operands. 5*3, 10*0.1,

a*b , a+10*3

/ 2 Division operator of two operands 10/3 , 10.0/3,

b/a , a*b/3

% 2 Modulo operator of two operands. 10%3 , 7%5 , a%b

++(x) 1 Increment x by 1 (this operator must be done before other operators).

a=10; b = ++a; (=>b=11)

--(x) 1 Decrement x by 1 (this operator must be done before other operators).

a=10; b = --a; (=>b=9)

(x)++ 1 Increment x by 1 (this operator must be done after other operators).

a=10; b = a++; (=>b=10)

(x)-- 1 Decrement x by 1 (this operator must be done after other operators).

a=10; b = a--; (=>b=10)

= 2 Assignment sign a = 10+1

+ 2 String concatination (if + is used with string.) st = “Hello” + “World”

14

Assignment Operator

Normally, = is used to assign the right side value to the left side variable but the statement can be coded in shorten format :

symbol Example result+= x += y x = x+ y

-= x -= y x = x- y

*= x *= y x = x* y

/= x /= y x = x/ y

%= x %= y x = x% y

++ x++ , ++x x = x+1

-- x-- , --x x = x-1

15

Operator Priority

( )

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

* / %

+ -

= , += , -=, *=, /=, %=

(x)++ , (x)--

Highest precedence (perform first)

Lowest precedence (perform later)

16

Thank you

top related