160 likes | 259 Vues
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 <var> = ( type ) <var> example
E N D
INPUT/OUTPUT Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand
Special Character • Special Character in C#
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
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());
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());
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? : ”);
Fix text Format items List of value Write Statement : Syntax I • Write() / WriteLine()formats : Console.Write( <string> ); • Example 1Console.Write("Hello world !!!\n "); • Example 2Console.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);
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. 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);
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 • indexmeans 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); • alignmentmeans 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);
Write Statement : Formatting Output Symbols which are used in Console.WriteLine() statement to define output format.
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 :
Operator Priority Highest precedence(perform first) Lowest precedence (perform later)