SlideShare a Scribd company logo
1 of 41
Download to read offline
Microsoft Visual C# 2010
       Fourth Edition



        Chapter 2
        Using Data
Objectives
•   Learn about declaring variables
•   Display variable values
•   Learn about the integral data types
•   Learn about floating-point data types
•   Use arithmetic operators




Microsoft Visual C# 2010, Fourth Edition         2
Objectives (cont'd.)
•   Learn about the bool data type
•   Learn about numeric type conversion
•   Learn about the char data type
•   Learn about the string data type
•   Define named constants and enumerations
•   Accept console input




Microsoft Visual C# 2010, Fourth Edition        3
Declaring Variables
• Constant
     – Cannot be changed after a program is compiled
• Literal constant
     – Its value is taken literally at each use
• Variable
     – A named location in computer memory that can hold
       different values at different points in time
• Data type
     – Describes the format and size of (amount of memory
       occupied by) a data item
Microsoft Visual C# 2010, Fourth Edition                   4
Microsoft Visual C# 2010, Fourth Edition   5
Declaring Variables (cont'd.)
• Variable declaration
     – Statement that names a variable and reserves
       storage
     – Example: int myAge = 25;
• You can declare multiple variables of the same
  type
     – In separate statements on different lines
• You can declare two variables of the same type in
  a single statement
     – By using the type once and separating the variable
       declarations with a comma
Microsoft Visual C# 2010, Fourth Edition                    6
Displaying Variable Values




Microsoft Visual C# 2010, Fourth Edition    7
Displaying Variable Values (cont'd.)




Microsoft Visual C# 2010, Fourth Edition   8
Displaying Variable Values (cont'd.)




Microsoft Visual C# 2010, Fourth Edition   9
Displaying Variable Values (cont'd.)




Microsoft Visual C# 2010, Fourth Edition   10
Displaying Variable Values (cont'd.)
• Format string
     – A string of characters that optionally contains fixed
       text
     – Contains one or more format items or placeholders
       for variable values
• Placeholder
     – Consists of a pair of curly braces containing a
       number that indicates the desired variable’s position
           • In a list that follows the string



Microsoft Visual C# 2010, Fourth Edition                       11
Displaying Variable Values (cont'd.)




Microsoft Visual C# 2010, Fourth Edition   12
Displaying Variable Values (cont'd.)
• Formatting output
           int num1 = 4, num2 = 56, num3 = 789;
           Console.WriteLine(“{0, 5}”, num1);
           Console.WriteLine(“{0, 5}”, num2);
           Console.WriteLine(“{0, 5}”, num3);
• Concatenate strings
     – Using the plus (+) sign




Microsoft Visual C# 2010, Fourth Edition          13
Using the Integral Data Types
• Integral data types
     – Types that store whole numbers
     – byte, sbyte, short, ushort, int, uint, long,
       ulong, and char
• Variables of type int
     – Store (or hold) integers, or whole numbers
• Shorter integer types
     – byte, sbyte (which stands for signed byte), short
       (short int), or ushort (unsigned short int)


Microsoft Visual C# 2010, Fourth Edition               14
Using Floating-Point Data Types
• Floating-point number
     – Contains decimal positions
• Floating-point data types
     – float
           • Can hold up to seven significant digits of accuracy
     – double
           • Can hold 15 or 16 significant digits of accuracy
     – decimal
           • Has a greater precision and a smaller range
           • Suitable for financial and monetary calculations

Microsoft Visual C# 2010, Fourth Edition                           15
Using Floating-Point Data Types
                    (cont'd.)
• Significant digits
     – Specifies the mathematical accuracy of the value
• Suffixes
     – Put an F after a number to make it a float
     – Put a D after it to make it a double
     – Put an M after it to make it a decimal
• Scientific notation
     – Includes an E (for exponent)



Microsoft Visual C# 2010, Fourth Edition                  16
Formatting Floating-Point Values
• C# displays floating-point numbers in the most
  concise way it can
     – While maintaining the correct value
• Standard numeric format strings
     – Strings of characters expressed within double
       quotation marks that indicate a format for output
     – Take the form X0
           • X is the format specifier; 0 is the precision specifier
• Format specifiers
     – Define the most commonly used numeric format
       types
Microsoft Visual C# 2010, Fourth Edition                               17
Formatting Floating-Point Values
                   (cont'd.)




Microsoft Visual C# 2010, Fourth Edition   18
Using the Standard Binary Arithmetic
               Operators
• Binary operators
     – Use two values (operands)
• Operator precedence
     – Rules that determine the order in which parts of a
       mathematical expression are evaluated
     – Multiplication, division, and remainder always take
       place prior to addition or subtraction in an expression
     – You can override normal operator precedence with
       parentheses


Microsoft Visual C# 2010, Fourth Edition                    19
Using the Standard Binary Arithmetic
           Operators (cont'd.)




Microsoft Visual C# 2010, Fourth Edition   20
Using Shortcut Arithmetic Operators

• Add and assign operator
     – Example: bankBal += bankBal * interestRate;
     – Variations: –=, *=, and /=
• Prefix increment operator
     – Example: ++someValue;
• Postfix increment operator
     – Example: someValue++;
• Unary operator
     – Use only one value
• Decrement operator (--)
Microsoft Visual C# 2010, Fourth Edition             21
Using the bool Data Type
• Boolean variable
     – Can hold only one of two values—true or false
     – Declare a Boolean variable with type bool
• Comparison operator
     – Compares two items
     – An expression containing a comparison operator has
       a Boolean value




Microsoft Visual C# 2010, Fourth Edition               22
Using the bool Data Type (cont'd.)




Microsoft Visual C# 2010, Fourth Edition   23
Understanding Numeric Type
                    Conversion
• Arithmetic with variables or constants of the same
  type
     – Result retains the same type
• Arithmetic with operands of dissimilar types
     – C# chooses a unifying type for the result
     – Implicitly (or automatically) converts nonconforming
       operands to the unifying type
           • Type with the higher type precedence




Microsoft Visual C# 2010, Fourth Edition                  24
Understanding Numeric Type
               Conversion (cont'd.)
• Implicit cast
     – Automatic transformation that occurs when a value
       is assigned to a type with higher precedence
• Explicit cast
     – Placing the desired result type in parentheses
           • Followed by the variable or constant to be cast




Microsoft Visual C# 2010, Fourth Edition                       25
Using the char Data Type
• char data type
     – Holds any single character
• Place constant character values within single
  quotation marks
• Escape sequence
     – Stores a pair of characters
     – Begins with a backslash
     – Pair of symbols represents a single character



Microsoft Visual C# 2010, Fourth Edition               26
Using the char Data Type (cont'd.)




Microsoft Visual C# 2010, Fourth Edition   27
Using the string Data Type

• string data type
     – Holds a series of characters
• Values are expressed within double quotation
  marks
• Comparing strings
     – Use == and !=
     – Methods Equals(), Compare(), CompareTo()




Microsoft Visual C# 2010, Fourth Edition          28
Using the string Data Type (cont'd.)




Microsoft Visual C# 2010, Fourth Edition   29
Using the string Data Type (cont'd.)




Microsoft Visual C# 2010, Fourth Edition   30
Using the string Data Type (cont'd.)
• Use the length property of a string to determine its
  length
     – The length of “water” is 5
• Use the Substring() method to extract a portion
  of a string from a starting point for a specific length




Microsoft Visual C# 2010, Fourth Edition                 31
Using the string Data Type (cont'd.)




Microsoft Visual C# 2010, Fourth Edition   32
Defining Named Constants
• Named constant
     – Often simply called a constant
     – An identifier whose contents cannot change
     – Created using the keyword const
• Programmers usually name constants using all
  uppercase letters
     – Inserting underscores for readability
• Self-documenting statement
     – Easy to understand even without program comments


Microsoft Visual C# 2010, Fourth Edition             33
Working with Enumerations
• An enumeration is a set of constants represented
  by identifiers
• The following is an enumeration called
  DayOfWeek:
     enum DayOfWeek
     {
       SUNDAY, MONDAY, TUESDAY, WEDNESDAY
       THURSDAY, FRIDAY, SATURDAY
     }


Microsoft Visual C# 2010, Fourth Edition         34
Working with Enumerations (cont'd.)
• By default, enumeration values are integers
     – Can specify otherwise by including a colon and a
       type name after the enumeration name
• The identifiers in an enumeration are often meant
  to hold consecutive values
     – When you don’t supply values, they start at 0 and
       increment by 1
     – In the DayOfWeek enumeration, SUNDAY is 0,
       MONDAY is 1, and so on


Microsoft Visual C# 2010, Fourth Edition                   35
Accepting Console Input
• Interactive program
     – A program that allows user input
• Console.ReadLine() method
     – Accepts user input from the keyboard
     – Accepts all of the characters entered by a user until
       the user presses Enter
     – Characters can be assigned to a string
     – Must use a conversion method to convert the input
       string to the proper type


Microsoft Visual C# 2010, Fourth Edition                       36
Accepting Console Input (cont'd.)




Microsoft Visual C# 2010, Fourth Edition   37
Accepting Console Input (cont'd.)




Microsoft Visual C# 2010, Fourth Edition   38
You Do It
• Activities to explore
     –   Declaring and Using variables
     –   Performing Arithmetic
     –   Working with Boolean Variables
     –   Using Escape Sequences
     –   Writing a Program that Accepts User Input




Microsoft Visual C# 2010, Fourth Edition             39
Summary
• Constant: cannot be changed after compilation
• Can display variable values with Write() or
  WriteLine()
• Nine integral data types: byte, sbyte, short,
  ushort, int, uint, long, ulong, and char
• Three floating-point data types: float, double,
  and decimal
• Use the binary arithmetic operators +, –, *, /, and
  % to manipulate values in your programs
• Shortcut arithmetic operators
Microsoft Visual C# 2010, Fourth Edition                40
Summary (cont'd.)
• A bool variable can be true or false
• Implicit cast versus explicit cast
• char data type holds any single character
• string data type holds a series of characters
• Named constants are program identifiers whose
  values cannot change.
• Console.ReadLine() method accepts user input




Microsoft Visual C# 2010, Fourth Edition      41

More Related Content

What's hot

9781285852744 ppt ch02
9781285852744 ppt ch029781285852744 ppt ch02
9781285852744 ppt ch02Terry Yoast
 
Solutions manual for c++ programming from problem analysis to program design ...
Solutions manual for c++ programming from problem analysis to program design ...Solutions manual for c++ programming from problem analysis to program design ...
Solutions manual for c++ programming from problem analysis to program design ...Warren0989
 
Operators and Expressions in C#
Operators and Expressions in C#Operators and Expressions in C#
Operators and Expressions in C#Prasanna Kumar SM
 
9781285852744 ppt ch12
9781285852744 ppt ch129781285852744 ppt ch12
9781285852744 ppt ch12Terry Yoast
 
Functions ppt ch06
Functions ppt ch06Functions ppt ch06
Functions ppt ch06Terry Yoast
 
Course Breakup Plan- C
Course Breakup Plan- CCourse Breakup Plan- C
Course Breakup Plan- Cswatisinghal
 
Computer Programming
Computer ProgrammingComputer Programming
Computer ProgrammingBurhan Fakhar
 
C notes by m v b reddy(gitam)imp notes all units notes 5 unit order
C notes by m v b  reddy(gitam)imp  notes  all units notes  5 unit orderC notes by m v b  reddy(gitam)imp  notes  all units notes  5 unit order
C notes by m v b reddy(gitam)imp notes all units notes 5 unit orderMalikireddy Bramhananda Reddy
 
Chapter 2 - Basic Elements of Java
Chapter 2 - Basic Elements of JavaChapter 2 - Basic Elements of Java
Chapter 2 - Basic Elements of JavaAdan Hubahib
 
Visualizing UML’s Sequence and Class Diagrams Using Graph-Based Clusters
Visualizing UML’s Sequence and   Class Diagrams Using Graph-Based Clusters  Visualizing UML’s Sequence and   Class Diagrams Using Graph-Based Clusters
Visualizing UML’s Sequence and Class Diagrams Using Graph-Based Clusters Nakul Sharma
 
Chapter 11: Object Oriented Programming Part 2
Chapter 11: Object Oriented Programming Part 2Chapter 11: Object Oriented Programming Part 2
Chapter 11: Object Oriented Programming Part 2Nicole Ryan
 

What's hot (20)

Lecture 5
Lecture 5Lecture 5
Lecture 5
 
9781285852744 ppt ch02
9781285852744 ppt ch029781285852744 ppt ch02
9781285852744 ppt ch02
 
Chapter 4 5
Chapter 4 5Chapter 4 5
Chapter 4 5
 
Solutions manual for c++ programming from problem analysis to program design ...
Solutions manual for c++ programming from problem analysis to program design ...Solutions manual for c++ programming from problem analysis to program design ...
Solutions manual for c++ programming from problem analysis to program design ...
 
Operators and Expressions in C#
Operators and Expressions in C#Operators and Expressions in C#
Operators and Expressions in C#
 
9781285852744 ppt ch12
9781285852744 ppt ch129781285852744 ppt ch12
9781285852744 ppt ch12
 
Functions ppt ch06
Functions ppt ch06Functions ppt ch06
Functions ppt ch06
 
JavaScript functions
JavaScript functionsJavaScript functions
JavaScript functions
 
Chap02
Chap02Chap02
Chap02
 
Intake 38_1
Intake 38_1Intake 38_1
Intake 38_1
 
Course Breakup Plan- C
Course Breakup Plan- CCourse Breakup Plan- C
Course Breakup Plan- C
 
Computer Programming
Computer ProgrammingComputer Programming
Computer Programming
 
C notes by m v b reddy(gitam)imp notes all units notes 5 unit order
C notes by m v b  reddy(gitam)imp  notes  all units notes  5 unit orderC notes by m v b  reddy(gitam)imp  notes  all units notes  5 unit order
C notes by m v b reddy(gitam)imp notes all units notes 5 unit order
 
Chapter 12
Chapter 12Chapter 12
Chapter 12
 
Chapter2
Chapter2Chapter2
Chapter2
 
Chapter 2 - Basic Elements of Java
Chapter 2 - Basic Elements of JavaChapter 2 - Basic Elements of Java
Chapter 2 - Basic Elements of Java
 
ListMyPolygons 0.6
ListMyPolygons 0.6ListMyPolygons 0.6
ListMyPolygons 0.6
 
Visualizing UML’s Sequence and Class Diagrams Using Graph-Based Clusters
Visualizing UML’s Sequence and   Class Diagrams Using Graph-Based Clusters  Visualizing UML’s Sequence and   Class Diagrams Using Graph-Based Clusters
Visualizing UML’s Sequence and Class Diagrams Using Graph-Based Clusters
 
Chapter 11: Object Oriented Programming Part 2
Chapter 11: Object Oriented Programming Part 2Chapter 11: Object Oriented Programming Part 2
Chapter 11: Object Oriented Programming Part 2
 
Programming in c notes
Programming in c notesProgramming in c notes
Programming in c notes
 

Viewers also liked

Viewers also liked (16)

Introduction To Silverlight and Prism
Introduction To Silverlight and PrismIntroduction To Silverlight and Prism
Introduction To Silverlight and Prism
 
Perl Development
Perl DevelopmentPerl Development
Perl Development
 
2310 b 11
2310 b 112310 b 11
2310 b 11
 
PyCologne
PyColognePyCologne
PyCologne
 
01 Ajax Intro
01 Ajax Intro01 Ajax Intro
01 Ajax Intro
 
2310 b 09
2310 b 092310 b 09
2310 b 09
 
Nosql availability & integrity
Nosql availability & integrityNosql availability & integrity
Nosql availability & integrity
 
Java swing
Java swingJava swing
Java swing
 
Forms authentication
Forms authenticationForms authentication
Forms authentication
 
Oid structure
Oid structureOid structure
Oid structure
 
5 Key Components of Genrocket
5 Key Components of Genrocket5 Key Components of Genrocket
5 Key Components of Genrocket
 
Ajax & ASP.NET 2
Ajax & ASP.NET 2Ajax & ASP.NET 2
Ajax & ASP.NET 2
 
Oracle 10g Application Server
Oracle 10g Application ServerOracle 10g Application Server
Oracle 10g Application Server
 
Itp 120 Chapt 19 2009 Binary Input & Output
Itp 120 Chapt 19 2009 Binary Input & OutputItp 120 Chapt 19 2009 Binary Input & Output
Itp 120 Chapt 19 2009 Binary Input & Output
 
Java/Swing
Java/SwingJava/Swing
Java/Swing
 
jQuery introduction
jQuery introductionjQuery introduction
jQuery introduction
 

Similar to Csc153 chapter 02

CS4443 - Modern Programming Language - I Lecture (2)
CS4443 - Modern Programming Language - I  Lecture (2)CS4443 - Modern Programming Language - I  Lecture (2)
CS4443 - Modern Programming Language - I Lecture (2)Dilawar Khan
 
C programming tutorial for Beginner
C programming tutorial for BeginnerC programming tutorial for Beginner
C programming tutorial for Beginnersophoeutsen2
 
Data structure & Algorithms - Programming in C
Data structure & Algorithms - Programming in CData structure & Algorithms - Programming in C
Data structure & Algorithms - Programming in Cbabuk110
 
CSharpCheatSheetV1.pdf
CSharpCheatSheetV1.pdfCSharpCheatSheetV1.pdf
CSharpCheatSheetV1.pdfssusera0bb35
 
Chap_________________1_Introduction.pptx
Chap_________________1_Introduction.pptxChap_________________1_Introduction.pptx
Chap_________________1_Introduction.pptxRonaldo Aditya
 
programming week 2.ppt
programming week 2.pptprogramming week 2.ppt
programming week 2.pptFatimaZafar68
 
0-Slot05-06-07-Basic-Logics.pdf
0-Slot05-06-07-Basic-Logics.pdf0-Slot05-06-07-Basic-Logics.pdf
0-Slot05-06-07-Basic-Logics.pdfssusere19c741
 
DISE - Windows Based Application Development in C#
DISE - Windows Based Application Development in C#DISE - Windows Based Application Development in C#
DISE - Windows Based Application Development in C#Rasan Samarasinghe
 

Similar to Csc153 chapter 02 (20)

CS4443 - Modern Programming Language - I Lecture (2)
CS4443 - Modern Programming Language - I  Lecture (2)CS4443 - Modern Programming Language - I  Lecture (2)
CS4443 - Modern Programming Language - I Lecture (2)
 
C programming tutorial for Beginner
C programming tutorial for BeginnerC programming tutorial for Beginner
C programming tutorial for Beginner
 
C++ ch2
C++ ch2C++ ch2
C++ ch2
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
Lesson 4.1 completing the problem solving process
Lesson 4.1 completing the problem solving processLesson 4.1 completing the problem solving process
Lesson 4.1 completing the problem solving process
 
lecture_3.ppt
lecture_3.pptlecture_3.ppt
lecture_3.ppt
 
Data structure & Algorithms - Programming in C
Data structure & Algorithms - Programming in CData structure & Algorithms - Programming in C
Data structure & Algorithms - Programming in C
 
CSharpCheatSheetV1.pdf
CSharpCheatSheetV1.pdfCSharpCheatSheetV1.pdf
CSharpCheatSheetV1.pdf
 
C# Basics
C# BasicsC# Basics
C# Basics
 
Csharp
CsharpCsharp
Csharp
 
C#
C#C#
C#
 
C_plus_plus
C_plus_plusC_plus_plus
C_plus_plus
 
Lesson 3.2 data types for memory location
Lesson 3.2 data types for memory locationLesson 3.2 data types for memory location
Lesson 3.2 data types for memory location
 
C++.ppt
C++.pptC++.ppt
C++.ppt
 
Chap_________________1_Introduction.pptx
Chap_________________1_Introduction.pptxChap_________________1_Introduction.pptx
Chap_________________1_Introduction.pptx
 
C language
C languageC language
C language
 
programming week 2.ppt
programming week 2.pptprogramming week 2.ppt
programming week 2.ppt
 
Intake 37 2
Intake 37 2Intake 37 2
Intake 37 2
 
0-Slot05-06-07-Basic-Logics.pdf
0-Slot05-06-07-Basic-Logics.pdf0-Slot05-06-07-Basic-Logics.pdf
0-Slot05-06-07-Basic-Logics.pdf
 
DISE - Windows Based Application Development in C#
DISE - Windows Based Application Development in C#DISE - Windows Based Application Development in C#
DISE - Windows Based Application Development in C#
 

Recently uploaded

Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Mark Goldstein
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesManik S Magar
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality AssuranceInflectra
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesBernd Ruecker
 
Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Kaya Weers
 
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...BookNet Canada
 
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...itnewsafrica
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkPixlogix Infotech
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...itnewsafrica
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI AgeCprime
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationKnoldus Inc.
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfNeo4j
 
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...Jeffrey Haguewood
 
Infrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platformsInfrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platformsYoss Cohen
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 

Recently uploaded (20)

Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architectures
 
Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)
 
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
 
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App Framework
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI Age
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog Presentation
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdf
 
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
 
Infrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platformsInfrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platforms
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 

Csc153 chapter 02

  • 1. Microsoft Visual C# 2010 Fourth Edition Chapter 2 Using Data
  • 2. Objectives • Learn about declaring variables • Display variable values • Learn about the integral data types • Learn about floating-point data types • Use arithmetic operators Microsoft Visual C# 2010, Fourth Edition 2
  • 3. Objectives (cont'd.) • Learn about the bool data type • Learn about numeric type conversion • Learn about the char data type • Learn about the string data type • Define named constants and enumerations • Accept console input Microsoft Visual C# 2010, Fourth Edition 3
  • 4. Declaring Variables • Constant – Cannot be changed after a program is compiled • Literal constant – Its value is taken literally at each use • Variable – A named location in computer memory that can hold different values at different points in time • Data type – Describes the format and size of (amount of memory occupied by) a data item Microsoft Visual C# 2010, Fourth Edition 4
  • 5. Microsoft Visual C# 2010, Fourth Edition 5
  • 6. Declaring Variables (cont'd.) • Variable declaration – Statement that names a variable and reserves storage – Example: int myAge = 25; • You can declare multiple variables of the same type – In separate statements on different lines • You can declare two variables of the same type in a single statement – By using the type once and separating the variable declarations with a comma Microsoft Visual C# 2010, Fourth Edition 6
  • 7. Displaying Variable Values Microsoft Visual C# 2010, Fourth Edition 7
  • 8. Displaying Variable Values (cont'd.) Microsoft Visual C# 2010, Fourth Edition 8
  • 9. Displaying Variable Values (cont'd.) Microsoft Visual C# 2010, Fourth Edition 9
  • 10. Displaying Variable Values (cont'd.) Microsoft Visual C# 2010, Fourth Edition 10
  • 11. Displaying Variable Values (cont'd.) • Format string – A string of characters that optionally contains fixed text – Contains one or more format items or placeholders for variable values • Placeholder – Consists of a pair of curly braces containing a number that indicates the desired variable’s position • In a list that follows the string Microsoft Visual C# 2010, Fourth Edition 11
  • 12. Displaying Variable Values (cont'd.) Microsoft Visual C# 2010, Fourth Edition 12
  • 13. Displaying Variable Values (cont'd.) • Formatting output int num1 = 4, num2 = 56, num3 = 789; Console.WriteLine(“{0, 5}”, num1); Console.WriteLine(“{0, 5}”, num2); Console.WriteLine(“{0, 5}”, num3); • Concatenate strings – Using the plus (+) sign Microsoft Visual C# 2010, Fourth Edition 13
  • 14. Using the Integral Data Types • Integral data types – Types that store whole numbers – byte, sbyte, short, ushort, int, uint, long, ulong, and char • Variables of type int – Store (or hold) integers, or whole numbers • Shorter integer types – byte, sbyte (which stands for signed byte), short (short int), or ushort (unsigned short int) Microsoft Visual C# 2010, Fourth Edition 14
  • 15. Using Floating-Point Data Types • Floating-point number – Contains decimal positions • Floating-point data types – float • Can hold up to seven significant digits of accuracy – double • Can hold 15 or 16 significant digits of accuracy – decimal • Has a greater precision and a smaller range • Suitable for financial and monetary calculations Microsoft Visual C# 2010, Fourth Edition 15
  • 16. Using Floating-Point Data Types (cont'd.) • Significant digits – Specifies the mathematical accuracy of the value • Suffixes – Put an F after a number to make it a float – Put a D after it to make it a double – Put an M after it to make it a decimal • Scientific notation – Includes an E (for exponent) Microsoft Visual C# 2010, Fourth Edition 16
  • 17. Formatting Floating-Point Values • C# displays floating-point numbers in the most concise way it can – While maintaining the correct value • Standard numeric format strings – Strings of characters expressed within double quotation marks that indicate a format for output – Take the form X0 • X is the format specifier; 0 is the precision specifier • Format specifiers – Define the most commonly used numeric format types Microsoft Visual C# 2010, Fourth Edition 17
  • 18. Formatting Floating-Point Values (cont'd.) Microsoft Visual C# 2010, Fourth Edition 18
  • 19. Using the Standard Binary Arithmetic Operators • Binary operators – Use two values (operands) • Operator precedence – Rules that determine the order in which parts of a mathematical expression are evaluated – Multiplication, division, and remainder always take place prior to addition or subtraction in an expression – You can override normal operator precedence with parentheses Microsoft Visual C# 2010, Fourth Edition 19
  • 20. Using the Standard Binary Arithmetic Operators (cont'd.) Microsoft Visual C# 2010, Fourth Edition 20
  • 21. Using Shortcut Arithmetic Operators • Add and assign operator – Example: bankBal += bankBal * interestRate; – Variations: –=, *=, and /= • Prefix increment operator – Example: ++someValue; • Postfix increment operator – Example: someValue++; • Unary operator – Use only one value • Decrement operator (--) Microsoft Visual C# 2010, Fourth Edition 21
  • 22. Using the bool Data Type • Boolean variable – Can hold only one of two values—true or false – Declare a Boolean variable with type bool • Comparison operator – Compares two items – An expression containing a comparison operator has a Boolean value Microsoft Visual C# 2010, Fourth Edition 22
  • 23. Using the bool Data Type (cont'd.) Microsoft Visual C# 2010, Fourth Edition 23
  • 24. Understanding Numeric Type Conversion • Arithmetic with variables or constants of the same type – Result retains the same type • Arithmetic with operands of dissimilar types – C# chooses a unifying type for the result – Implicitly (or automatically) converts nonconforming operands to the unifying type • Type with the higher type precedence Microsoft Visual C# 2010, Fourth Edition 24
  • 25. Understanding Numeric Type Conversion (cont'd.) • Implicit cast – Automatic transformation that occurs when a value is assigned to a type with higher precedence • Explicit cast – Placing the desired result type in parentheses • Followed by the variable or constant to be cast Microsoft Visual C# 2010, Fourth Edition 25
  • 26. Using the char Data Type • char data type – Holds any single character • Place constant character values within single quotation marks • Escape sequence – Stores a pair of characters – Begins with a backslash – Pair of symbols represents a single character Microsoft Visual C# 2010, Fourth Edition 26
  • 27. Using the char Data Type (cont'd.) Microsoft Visual C# 2010, Fourth Edition 27
  • 28. Using the string Data Type • string data type – Holds a series of characters • Values are expressed within double quotation marks • Comparing strings – Use == and != – Methods Equals(), Compare(), CompareTo() Microsoft Visual C# 2010, Fourth Edition 28
  • 29. Using the string Data Type (cont'd.) Microsoft Visual C# 2010, Fourth Edition 29
  • 30. Using the string Data Type (cont'd.) Microsoft Visual C# 2010, Fourth Edition 30
  • 31. Using the string Data Type (cont'd.) • Use the length property of a string to determine its length – The length of “water” is 5 • Use the Substring() method to extract a portion of a string from a starting point for a specific length Microsoft Visual C# 2010, Fourth Edition 31
  • 32. Using the string Data Type (cont'd.) Microsoft Visual C# 2010, Fourth Edition 32
  • 33. Defining Named Constants • Named constant – Often simply called a constant – An identifier whose contents cannot change – Created using the keyword const • Programmers usually name constants using all uppercase letters – Inserting underscores for readability • Self-documenting statement – Easy to understand even without program comments Microsoft Visual C# 2010, Fourth Edition 33
  • 34. Working with Enumerations • An enumeration is a set of constants represented by identifiers • The following is an enumeration called DayOfWeek: enum DayOfWeek { SUNDAY, MONDAY, TUESDAY, WEDNESDAY THURSDAY, FRIDAY, SATURDAY } Microsoft Visual C# 2010, Fourth Edition 34
  • 35. Working with Enumerations (cont'd.) • By default, enumeration values are integers – Can specify otherwise by including a colon and a type name after the enumeration name • The identifiers in an enumeration are often meant to hold consecutive values – When you don’t supply values, they start at 0 and increment by 1 – In the DayOfWeek enumeration, SUNDAY is 0, MONDAY is 1, and so on Microsoft Visual C# 2010, Fourth Edition 35
  • 36. Accepting Console Input • Interactive program – A program that allows user input • Console.ReadLine() method – Accepts user input from the keyboard – Accepts all of the characters entered by a user until the user presses Enter – Characters can be assigned to a string – Must use a conversion method to convert the input string to the proper type Microsoft Visual C# 2010, Fourth Edition 36
  • 37. Accepting Console Input (cont'd.) Microsoft Visual C# 2010, Fourth Edition 37
  • 38. Accepting Console Input (cont'd.) Microsoft Visual C# 2010, Fourth Edition 38
  • 39. You Do It • Activities to explore – Declaring and Using variables – Performing Arithmetic – Working with Boolean Variables – Using Escape Sequences – Writing a Program that Accepts User Input Microsoft Visual C# 2010, Fourth Edition 39
  • 40. Summary • Constant: cannot be changed after compilation • Can display variable values with Write() or WriteLine() • Nine integral data types: byte, sbyte, short, ushort, int, uint, long, ulong, and char • Three floating-point data types: float, double, and decimal • Use the binary arithmetic operators +, –, *, /, and % to manipulate values in your programs • Shortcut arithmetic operators Microsoft Visual C# 2010, Fourth Edition 40
  • 41. Summary (cont'd.) • A bool variable can be true or false • Implicit cast versus explicit cast • char data type holds any single character • string data type holds a series of characters • Named constants are program identifiers whose values cannot change. • Console.ReadLine() method accepts user input Microsoft Visual C# 2010, Fourth Edition 41