1 / 16

INPUT/OUTPUT Statements

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

Télécharger la présentation

INPUT/OUTPUT Statements

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Variable Type

  3. Special Character • Special Character in C#

  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. Example double 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. 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);

  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. 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. 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 • 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);

  12. Write Statement : Formatting Output Symbols which are used in Console.WriteLine() statement to define output format.

  13. Operators

  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 :

  15. Operator Priority Highest precedence(perform first) Lowest precedence (perform later)

  16. Thank you

More Related