Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 INPUT/OUTPUT Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.

Similar presentations


Presentation on theme: "1 INPUT/OUTPUT Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand."— Presentation transcript:

1 1 INPUT/OUTPUT Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand

2 2 Variable Type Data type sizedescriptionbound 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 BytesTo keep short integer. -32,768 ถึง 32,767 int 4 BytesTo keep integer. ประมาณ -2.1 x 10 6 ถึง 2.1 x 10 6 long 8 Bytes To keep long integer. ประมาณ -9.2 x 10 18 ถึง 9.2 x 10 18 float 16 Bytes To keep real number. double 32 Bytes To keep real number. ประมาณ ± 5.0x10 -324 ถึง ± 1.7x10 308 String UnlimitTo keep several characters. unlimit

3 3 Special Character Special Character in C# symbolresult \nAdvance to a new line \tTab \\To type \ \'To type ' \"To type "

4 4 Number Conversion To convert a numberic data type = (type) 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

5 5 Input Statement คำสั่ง : Console.ReadLine( ); – This command is used to read string from the keyboard. Usually used with Parse. Example double num; string alpha; int unit; num = double.Parse(Console.ReadLine()); alpha = Console.ReadLine(); unit = int.Parse(Console.ReadLine());

6 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 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 8 Write Statement : Syntax I Write() / WriteLine() formats : Console.Write( ); – Example 1Console.Write("Hello world !!!\n "); – Example 2Console.Write("204111"); Console.Write( ); – Example 1 Console.Write(num); – Example 2 Console.Write(sum); Console.Write(,, …, ); – Value can be a variable or a constant and must be referenced in 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 9 Write Statement : Syntax II Variables in Console.Write() / Console.WriteLine() must be referenced by using its position symbol. {0} means the 0 th variable, {1} means the 1 st variable and …. Double quote must be used to enclose text and the position symbol of variable. Example Console.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 10 Write Statement : Example

11 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 12 Write Statement : Formatting Output Symbols which are used in Console.WriteLine() statement to define output format. Symbolmeaningexampleresult D or dDecimalConsole.WriteLine( “ {0:D} ",10};10 E or eExponentialConsole.WriteLine( “ {0:E} ",10};1.000000E +001 F or fFix PointConsole.WriteLine( “ {0:F} ",10};10.00 X or xHexadecimalConsole.WriteLine( “ {0:X} ",10};A P or pPercentConsole.WriteLine( “ {0:P} ",10};1,000.00 %

13 13 Operators symbol Number of variable meaningexample +, -1Symbol the represent positive or negative number. -5, -3, -a, +b +, -2Addition and subtraction operators of 2 operands. 5+3, a+10 *2Multiplication 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 %2Modulo operator of two operands.10%3, 7%5, a%b ++(x)1Increment x by 1 (this operator must be done before other operators). a=10; b = ++a; (=>b=11) --(x)1Decrement x by 1 (this operator must be done before other operators). a=10; b = --a; (=>b=9) (x)++1Increment x by 1 (this operator must be done after other operators). a=10; b = a++; (=>b=10) (x)--1Decrement x by 1 (this operator must be done after other operators). a=10; b = a--; (=>b=10) =2Assignment signa = 10+1 +2String concatination (if + is used with string.) st = “ Hello ” + “ World ”

14 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 : symbolExampleresult +=+=x += yx = x+ y -=x -= yx = x- y *=x *= yx = x* y /=x /= yx = x/ y %=x %= yx = x% y ++x++, ++xx = x+1 --x--, --xx = x-1

15 15 Operator Priority ( ) ++(x), --(x), +(x), -(x) * / % + - =, +=, -=, *=, /=, %= (x)++, (x)-- Highest precedence (perform first) Lowest precedence (perform later)

16 16 Thank you


Download ppt "1 INPUT/OUTPUT Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand."

Similar presentations


Ads by Google