Input & Output Statement

Slides:



Advertisements
Similar presentations
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.
Advertisements

Introduction to Computers and Programming Lecture 7:
Working with the data type: char  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
1 Chapter 2: Elementary Programming Shahriar Hossain.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Basic Elements of C++ Chapter 2.
Thanachat Thanomkulabut
Performing Simple Calculations with C# Svetlin Nakov Telerik Corporation
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.
Expressions, Data Conversion, and Input
1. 2 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Decisions { class.
Chapter 2: Introducing Data Types and Operators.  Know Java’s primitive types  Use literals  Initialize variables  Know the scope rules of variables.
Reading and Writing to the Console Svetlin Nakov Telerik Corporation
Introduction to C# Programming ณภัทร สักทอง Application Program Development.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
1 Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
INPUT/OUTPUT STATEMENT ADDITION SLIDES. Console.ReadLine() – Use to get the input (String) from user Convert string to other data type – int.Parse() 
C# Programming: Expressions, conditions, and repetitions Computer and Programming.
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
Simple Data Types and Statements. Namespaces namespace MyNamespace { // …. { MyNamespace::func1() using namespace OtherNamespace; Comments: // /* xxxx.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
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.
Chapter 2: Using Data.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
CPS120: Introduction to Computer Science Operations Lecture 9.
C# Basic Concept Thanachat Thanomkulabut. Naming Rules  Letters, digits and underscores(_)  First character  letter or _  Up to 63 characters long.
Mathematical Calculations in Java Mrs. C. Furman.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
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.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Performing Simple Calculations with C# Telerik Corporation
C# Programming: Basic Concept Part 2 Computer and Programming (204111)
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
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.
1 INPUT/OUTPUT Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
Programming Fundamentals. Summary of Previous Lectures Phases of C++ Environment Data Types cin and cout.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
Data and Expressions. Let's explore some other fundamental programming concepts Chapter 2 focuses on: Character Strings Primitive Data The Declaration.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
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 Topics The Basics of a C++ Program Data Types
Expressions.
Chapter 2 Assignment and Interactive Input
Multiple variables can be created in one declaration
Operators and Expressions
Increment and Decrement
Lecture 3 Expressions Richard Gesick.
A First Book of ANSI C Fourth Edition
C# Programming.
elementary programming
Data Types and Expressions
Visual Programming COMP-315
Chapter 2 Primitive Data Types and Operations
Data Types and Expressions
Data Types and Expressions
Presentation transcript:

Input & Output Statement 01204111 Computer & Programming Group 350-352 Dept. of Computer Engineering Kasetsart University Adopted from Thanomkulabut’s experiences. Brushed up by MikeLab.Net Factory @KU. Version 2012

Review Data Type Arithmetic Operators Math Class

Data Types Type Size Description Range bool 1 byte Store truth value true / false char Store one character character code 0 – 255 byte Store positive integer 0 – 255 short 2 byte Store integer -32,768 -- 32,767 int 4 byte -2.1 x 109 -- 2.1 x 109 long 8 byte -9.2 x 1018 -- 9.2 x 1018 double 16 byte Store real number ± 5.0x10-324 -- ± 1.7x10308 string N/A Store sequence of characters

Priority of Arithmetic Operators high Operators Associativity Type () left to right parentheses unary postfix ++x --x + - (type) unary prefix * / % multiplicative + - additive = += -= *= /= %= x++, x-- right to left assignment low

Arithmetic Operators Example int a, b, c; a = 2-10*3/5+(6%4); b = 5*(15%4+(2-3))/9; c = 5+9%2*4-14/4%3 Answer a = -2 b = 1 c = 9 Given x = 3.0, y = 2.0, b =2, what is the output? x/y*++b x/y*b++ 3 2 Answer: 4.5 Answer: 3.0

Arithmetic Operators Example Answer int a=1, b=2, c=3; Console.WriteLine(a++ + ++b); Console.WriteLine(a++ - ++b); Console.WriteLine(++a + b++ + c++); Console.WriteLine(++a + b++ - c++); Console.WriteLine(++a - b++ - c++); 4 -2 7 1 -3 4, -2, 11, 6, -5

Arithmetic Operators Example(2) Statement Description var += expression Increment var by the value of expression var -= expression Decrement var by the value of expression var *= expression Multiply var by the value of expression, then store the result in var var /= expression Divide var by the value of expression, then store the result in var sum += x; // is equivalent to sum = sum + x prod *= 2.5; // is equivalent to prod = prod * 2.5 y -= 3+a; // is equivalent to y = y – (3+a)

The Math Class Math Class Method/ Constant Value returned Example Call Result PI Value of  Math.PI 3.1415927 Max(x,y) Larger of the two Math.Max(1,2) 2 Abs(x) Absolute value of x Math.Abs(-1.3) 1.3 Sqrt(x) Square-root of x Math.Sqrt(4.0) 2.0 Round(x) Nearest integer to x Math.Round(0.8) 1 Pow(x,y) xy Math.Pow(3,2) 9.0 Log(x) Natural log of x Math.Log(10) 2.302585 Floor(x) Greatest integer smaller than or equal to x Math.Floor(4.9) 4 Ceiling(x) Smallest integer greater than or equal to x Math.Ceiling(4.1) 5 Cos(x) Cosine of x radians Math.Cos(Math.PI) -1

Today’s Outline Input Statements Output Statements Data Conversions Console.ReadLine() / Console.Read() Basic Data Conversions Output Statements Console.Write() /Console.WriteLine() Output format Escape Sequences Characters Data Conversions Type of Conversions Convert class, Parse() methods

Input Statements

Input Statements Console.ReadLine(); Console.Read(); Read all characters (in form of string) Use with Convert or Parse commands frequently Console.Read(); Read ASCII code of the first character

Console.ReadLine() Console.ReadLine() Use to get the input (String) from user Convert string to other data type int.Parse() Convert string to integer double.Parse()  Convert string to double …. string st = Console.ReadLine(); Run-time error may be occurred if user’s input is incorrect

Console.ReadLine() Example string myname; myname = Console.ReadLine(); Console.WriteLine(“Your name is {0}”, myname); Kun Toto Your name is Kun Toto Ex2: int x; Console.Write(“Input x:”); string temp = Console.ReadLine(); x= int.Parse(temp); x = x*x; Console.WriteLine(“x^2 = {0}”, x); Input x:12 X^2 = 144

Console.Read() Console.Read returns an ACSII value (Integer) of the code of the first typed character. int i; Console.Write(“Input char:”); i = Console.Read(); Console.WriteLine(i); Input char: A 65

Output Statements

Output Statements Console.Write(); Console.WriteLine(); Show output Show output and shift to new line

Statement Output Statements Use the method Write or WriteLine in the Console class (which is in System namespace) Basic usage: Advanced usage with formatting Console.WriteLine("Hello"); Console.WriteLine(area); Console.WriteLine(”Size {0}x{1}”, width, height); double salary=12000; Console.WriteLine("My salary is {0:f2}.", salary);

Basic Usage string integer Console.Write(“Happy”); Output : Happy2009 string Console.WriteLine(“Happy”); Console.WriteLine(2009); Output : integer Happy 2009

Advanced Usage int a=7, b=2; Console.WriteLine(“a={0} and b={1}”,a,b); Console.WriteLine(“{1}+{0}={2}”,b,a,a+b); a=7 and b=2 {0} {1} 7 + 2 = 9 {1} {0} {2}

Advanced Usage (2) 20

Advanced Usage (3) int a = 10; Symbol Meaning Example Output D , d Decimal Console.WriteLine(“ {0:D} ",a}; 10 E , e Exponential Console.WriteLine(“ {0:E} ",a}; 1.000000E+001 F , f Fix Point Console.WriteLine(“ {0:F} ",a}; 10.00 P , p Percent Console.WriteLine(“ {0:P} ",a}; 1,000.00 % For a complete list of format specifiers, see http://msdn.microsoft.com/library/en-us/csref/html/vclrfFormattingNumericResultsTable.asp

Escape Sequences Characters What should you do if you would like to write “C:\mydocument” Console.WriteLine(““C:\mydocument””); Symbols Output \’ Single Quote \” Double Quote \\ Backslash \n New line \t Horizontal tab \uxxxx Unicode escape sequence for character with hex value xxxx. Console.WriteLine(“\“C:\mydocument\””); Console.WriteLine(“\“C:\\mydocument\””); 

Unicode Characters เกม 0e01 0e21 0e40 Console.Write(“\u0e40\u0e01\u0e21”); เกม Reference: http://unicode.org/charts/PDF/U0E00.pdf

Data Conversions

Losing some information!!! Data Conversions In some situation we have to convert data from one type to another. We may want to convert width and height of rectangle from Console.ReadLine() to int and calculate the rectangle area from the converted data. Type of conversions Widening conversions Convert from a smaller data type to a larger one Ex; int  double Narrowing conversions Convert from a larger data type to smaller one Ex; double  int Losing some information!!!

Data Conversions (2) Assignment conversion Arithmetic promotion Occurs automatically when a value of one type is assigned to a variable of another. Ex: aFloatVar = anIntVar; Arithmetic promotion Occurs automatically Ex: aFloatVar/anIntVar Casting Value of anIntVar is converted to float data type automatically Value of aFloatVar/anIntVar is converted to float (Larger data type)

Casting Both widening and narrowing conversions can be accomplished by explicitly casting a value. To cast, the type is put in parentheses in front of the value being converted. Example byte b, int i, long l i = (int)b; l = (long)b + (long)i; int byte int long long

Casting (2) 100% Example byte b; int i; long l; double d; d = (double) i /10; b = (byte) l +3; i = (int) d – 10; int double double int 100% OK, but may yield unpredictable result in some case byte long OK, but may yield unpredictable result in some case

Convert.To<type>(); byte boy=100; int imob=100; double dawn=100.0; char alpha=‘d’; string st=“1”; boy = Convert.ToByte(imob); dawn = Convert.ToDouble(st); imob= Convert.ToInt32(alpha); alpha = Convert.ToChar(boy); st = Convert.ToString(imob); boy = 100 i = 100 byte int int char dawn = 1 char alpha = ‘d’ byte double string st = 100

<type>.Parse(); Convert string data to others Example byte b=100; int i=100; double d=100.0; char c=‘d’; string st=“1”; d = double.Parse(st); i = int.Parse (st); c = char.Parse(st); b = byte.Parse(st); d = 1 i = 1 double string int string c = 1 b = 1

Test I Write a program which Input : Your name Output : Your name is <your name>.

Test II Write a program which Input : 3 numbers Output : average of the 3 input numbers

Test III Write a program which Input : Length of radius of circle Output : Area of the circle