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

Slides:



Advertisements
Similar presentations
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Advertisements

Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
1 9/13/06CS150 Introduction to Computer Science 1 Type Casting.
Expressions ► An expression can be a single variable, or can include a series of variables. If an expression includes multiple variables they are combined.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
© 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5/e Starting Out with C++: Early Objects 5 th Edition Chapter 2 Introduction.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
Data types and variables
Chapter 2 Data Types, Declarations, and Displays
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Reading and Writing to the Console Svetlin Nakov Telerik Corporation
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
INPUT/OUTPUT STATEMENT ADDITION SLIDES. Console.ReadLine() – Use to get the input (String) from user Convert string to other data type – int.Parse() 
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
Input, Output, and Processing
1 Conditional statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
Chapter 2: Using Data.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
C++ Programming: Basic Elements of C++.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
Reading and Writing to the Console Svetlin Nakov Telerik Corporation
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
C# Basic Concept Thanachat Thanomkulabut. Naming Rules  Letters, digits and underscores(_)  First character  letter or _  Up to 63 characters long.
1 st semester Basic Pascal Elements อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
C# C1 CSC 298 Elements of C# code (part 1). C# C2 Style for identifiers  Identifier: class, method, property (defined shortly) or variable names  class,
Input & Output Statement
CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;
Chapter 2 Variables.
GUJARAT KNOWLEDGE VILLAGE Faculty Name:- Prof H.M.Patel Students Name:- SONI VISHAL THAKKER BHAVIN ZALA JAYDIP ZALA.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
Console Input / Output Reading and Writing to the Console SoftUni Team Technical Trainers Software University
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Fundamental Programming Fundamental Programming More Expressions and Data Types.
C# Programming: Basic Concept Part 2 Computer and Programming (204111)
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
INPUT/OUTPUT STATEMENT ADDITION SLIDES. Console.ReadLine() – Use to get the input (String) from user Convert string to other data type – int.Parse() 
1 nd Semester Module2 C# Basic Concept Thanawin Rakthanmanon Computer Engineering Department Kasetsart University, Bangkok.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
Computer Programs CS 1400 Dennis A. Fairclough Version 1.1 CS 1400 Dennis A. Fairclough Version 1.1.
Lecture 2 Review of 1301 and C# Richard Gesick. Common data types int, long, double, float, char, bool, string.
Dr. Sajib Datta Jan 21,  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
arithmetic operator & cin I.Mona Alshehri The output formatting functions setw(width) setw(n) - output the value of the next expression in n columns.
Input/Output Statement Thanachat Thanomkulabut. Input Statement  Console.ReadLine()  Return string  Use to get the input from user  Convert string.
1 nd Semester Module6 Review Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department Kasetsart.
Chapter 2 Basic Computation
Multiple variables can be created in one declaration
Computing with C# and the .NET Framework
Type Conversion, Constants, and the String Object
Introduction to C++ Programming
DATA TYPES There are four basic data types associated with variables:
DATA TYPES AND OPERATIONS
Presentation transcript:

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

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 ถึง 9.2 x float 16 Bytes To keep real number. double 32 Bytes To keep real number. ประมาณ ± 5.0x ถึง ± 1.7x String UnlimitTo keep several characters. unlimit

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

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 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 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 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 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. Symbolmeaningexampleresult D or dDecimalConsole.WriteLine( “ {0:D} ",10};10 E or eExponentialConsole.WriteLine( “ {0:E} ",10}; E +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, %

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 = 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 : 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 Operator Priority ( ) ++(x), --(x), +(x), -(x) * / % + - =, +=, -=, *=, /=, %= (x)++, (x)-- Highest precedence (perform first) Lowest precedence (perform later)

16 Thank you