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.

Slides:



Advertisements
Similar presentations
Expressions ► An expression can be a single variable, or can include a series of variables. If an expression includes multiple variables they are combined.
Advertisements

Chapter 2: Using Data.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Data types and variables
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
JavaScript, Third Edition
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.
Chapter 2 Data Types, Declarations, and Displays.
Objectives You should be able to describe: Data Types
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
A Review of Programming and C++
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
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.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
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.
Computer Programming TCP1224 Chapter 4 Variables, Constants, and Arithmetic Operators.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: Basic Elements of C++.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Java Programming: From Problem Analysis to Program Design, 5e Chapter 2 Basic Elements of Java.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Introduction to C++ Basic Elements of C++. C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 The Basics of a C++ Program Function:
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Computer Engineering 1 st Semester Dr. Rabie A. Ramadan 3.
Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Chapter 2 Variables.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
Module B - Computation1/61 Module-B-Computation Variables Basic Memory Operations Expressions.
Programming Fundamentals. The setw Manipulator setw changes the field width of output. The setw manipulator causes the number (or string) that follows.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Chapter 2 Variables.
Chapter Topics The Basics of a C++ Program Data Types
BASIC ELEMENTS OF A COMPUTER PROGRAM
Basic Elements of C++.
Computing with C# and the .NET Framework
Java Programming: From Problem Analysis to Program Design, 4e
Basic Elements of C++ Chapter 2.
2.1 Parts of a C++ Program.
Chapter 2: Basic Elements of Java
Chapter 2 Variables.
C++ Data Types Data Type
Chapter 2: Introduction to C++.
Data Types and Expressions
Chapter 2 Variables.
Variables and Constants
Review of Java Fundamentals
Presentation transcript:

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 data types Learn how to use standard binary arithmetic operators Learn how to use shortcut arithmetic operators

3 Objectives Learn about the Boolean data type Learn about floating-point data types Learn how to format floating-point values Learn about numeric type conversion Learn about the char data type

4 Objectives Learn about the string data type Learn how to define named constants Learn how to accept console input

5 Declaring Variables A data item is classified as either constant or variable Constant data items cannot vary Variable data items can hold different values at different points of time All data items in a C# program have a data type Data type describes the format and size of a piece of data

6 Declaring Variables C# provides 14 basic or intrinsic types The most commonly used data types are: int, double, char, string, and bool Each C# intrinsic type is an alias, or other name for, a class in the System namespace

7 Declaring Variables A variable declaration includes: –The data type that the variable will store –An identifier that is the variable’s name –An optional assignment operator and assigned value when you want a variable to contain an initial value –An ending semicolon

8 Declaring Variables Examples of variable declarations: int myAge = 25; The equal sign (=) is an assignment operator The assignment operator works from right to left An assignment made when a variable is declared is an initialization It is not necessary to initialize a variable during a declaration. For example int myAge; is a valid declaration

9 Declaring Variables Examples of multiple variable declarations: –int myAge = 25; int yourAge = 19; –int myAge = 25, your Age = 19; Declarations of variables of the same type can be separated by commas. However, separate statements are used when declaring variables of different types –int myAge, yourAge; double mySalary, yourSalary;

10 Displaying Variable Values You can display variable values by using the variable name within a WriteLine() method call

11 Displaying Variable Values Output of DisplaySomeMoney Program

12 Displaying Variable Values Program that displays a string and a variable value

13 Displaying Variable Values Output of DisplayMoney2 program

14 Displaying Variable Values A format string is a string of characters that contains one or more placeholders. For example: –Console.WriteLine(“The money is {0} exactly”, someMoney);

15 Displaying Variable Values The number within the curly braces in the format string must be less than the number of values you list after the format string You do not have to use the positions in order: –Console.WriteLine (“The money is {0}. ${0} is a lot for my age which is {1}”, someMoney, myAge);

16 Displaying Variable Values If you use a second number within the curly braces in a number format, you can specify alignment and field size

17 Displaying Variable Values Unaligned output

18 Using the Integral Data Types In C#, there are nine integral data types: byte, sbyte, short, ushort, int, uint, long, ulong, and char char is used for characters like ‘A’ or ‘b’, while the other eight are used to store whole numbers You use variables of type int to store integers When you assign a value to a numeric variable, you do not type any commas: –int cost = 1000; // NOT int cost = 1,000;

19 Using the Standard Binary Arithmetic Operators The five most commonly used binary arithmetic operators

20 Using the Standard Binary Arithmetic Operators When you divide two integers (using the division operator or the modulus operator), the result is ALWAYS an integer Operator precedence refers to the order in which parts of a mathematical expression are evaluated

21 Using Shortcut Arithmetic Operators C# provides you with several shortcut ways to count and accumulate –Examples of Shortcuts: +=, -+, *=, /= The prefix and postfix operators can also provide a shorthand way of writing operators –Examples of the unary operators: ++val, val++ Besides the prefix and postfix increment operators, you can also use a decrement operator (--)

22 Using the Boolean Data Type Boolean logic is based on true or false comparisons Boolean variables can hold only one of two values: true or false You can assign values based on the result of comparisons to Boolean variables

23 Using Floating-Point Data Types A floating-point number is one that contains decimal positions C# supports three floating-point data types: float, double, and decimal A double can hold 15 to 16 significant digits of accuracy Floating-point numbers are double by default

24 Formatting Floating-Point Values Standard numeric format strings are strings of characters expressed within double quotation marks that indicate a format for output. For example, “F3”, where F is the format specifier and 3 is the precision specifier, indicates a fixed point with three significant digits. The format specifier can be one of nine built-in format characters The precision specifier controls the number of significant digits or zeros to the right of the decimal point

25 Formatting Floating-Point Values Format Specifiers

26 Understanding Numeric Type Conversion In arithmetic operations with variables or constants of the same type, the result of the operation usually retains the same type - For example, an integer type added to another integer type will result in an integer type When an arithmetic operation is performed with dissimilar types, C# chooses a unifying type for the result and implicitly converts the nonconforming operands to the unifying type There are rules followed by C# for implicit numeric conversions

27 Understanding Numeric Type Conversion When you perform a cast, you explicitly override the unifying type imposed by C# For example: double balance = ; int dollars = (int)balance; A cast involves placing the desired resulting type in parentheses followed by the variable or constant to be cast

28 Using the char Data Type The char data type is used to hold a single character (‘A’,’b’,’9’) A number can be a character only if it is enclosed in a single quotation mark (char aChar = 9; is illegal) You can store any character in a char variable, including escape sequences Characters in C# are represented in Unicode

29 Using the char Data Type Common Escape Sequences

30 Using the string Data Type In C# the string data type holds a series of characters Although you can use the == comparison operator with strings that are assigned literal values, you CANNOT use it with strings created through other methods C# recommends that you use the Equals() and Compare() methods to compare strings A string is considered greater than another string when it is greater lexically

31 Using the string Data Type Program that compares two strings using ==

32 Using the string Data Type Output of CompareNames1 program that uses ==

33 Using the string Data Type Program that compares two strings using Equals() and Compares()

34 Using the string Data Type Output of CompareNames2 program that uses Equals() and Compare()

35 Defining Named Constants A named constant is an identifier whose contents cannot change You create a named constant using the keyword const Programmers usually name constants using all uppercase letters, inserting underscores for readability Often times, constant statements are self-documenting

36 Defining Named Constants SalesTax program

37 Defining Named Constants Output of SalesTax program

38 Accepting Console Input A program that allows user input is an interactive program The Console.ReadLine() method accepts user input from the keyboard This method accepts a user’s input as a string You must use a Convert() method to convert the input string to the type required

39 Accepting Console Input InteractiveSalesTax program

40 Accepting Console Input Output of InteractiveSalesTax program

41 Accepting Console Input Typical run of InteractiveAddition program

42 Chapter Summary Data is constant when it cannot be changed after a program is compiled You can display variable values by using the variable name within a WriteLine() or Write() method call In C#, nine data types are considered integral data types Use the binary arithmetic operators to manipulate values in your program Because increasing the value of a variable is a common task, C# provides you with several shortcut arithmetic operators

43 Chapter Summary A Boolean variable can hold only one of two values: true or false C# supports three floating-point data types When you perform arithmetic with variables or constants of the same type, the result of the arithmetic retains the same type. When you perform arithmetic operations with operands of different types, C# chooses a unifying type for the result and implicitly converts nonconforming operands to the unifying type

44 Chapter Summary Use the char data type to hold any single character Use the string data type to hold a series of characters Named constants are program identifiers you cannot change Use the Console.ReadLine() method to accept user input