Chapter 2: Using Data

Similar documents
Visual C# Instructor s Manual Table of Contents

Introduction to C# Applications

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

CS313D: ADVANCED PROGRAMMING LANGUAGE

Lesson 02 Working with Data Types. MIT 31043: VISUAL PROGRAMMING By: S. Sabraz Nawaz Senior Lecturer in MIT

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Objectives. Introduce the core C# language features class Main types variables basic input and output operators arrays control constructs comments

Lesson 02 Working with Data Types MIT 31043, Visual Programming By: S. Sabraz Nawaz

Basics of Java Programming

Full file at

LECTURE 3 C++ Basics Part 2

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Fundamental of Programming (C)

Full file at

Programming in C++ 5. Integral data types

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. A Guide to this Instructor s Manual:

Reserved Words and Identifiers

Chapter 2: Using Data

.Net Technologies. Components of.net Framework

Values & Variables 1

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

ARG! Language Reference Manual

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

Operators. Java operators are classified into three categories:

Chapter 2 Working with Data Types and Operators

Fundamentals of Programming

3. Java - Language Constructs I

BASIC ELEMENTS OF A COMPUTER PROGRAM

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

COMP Primitive and Class Types. Yi Hong May 14, 2015

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Information Science 1

Chapter 02: Using Data

Declaration and Memory

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Information Science 1

Advanced Computer Programming

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

UNIT- 3 Introduction to C++

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

Objectives. In this chapter, you will:

1.3b Type Conversion

6.096 Introduction to C++ January (IAP) 2009

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

IT 374 C# and Applications/ IT695 C# Data Structures

Informatics Ingeniería en Electrónica y Automática Industrial

Lecture 3. More About C

C++ Programming: From Problem Analysis to Program Design, Third Edition

Java Notes. 10th ICSE. Saravanan Ganesh

A First Program - Greeting.cpp

CHAPTER 3 BASIC INSTRUCTION OF C++

Chapter 2: Introduction to C++

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Chapter 2. Lexical Elements & Operators

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Program Fundamentals

Introduction to C# Applications Pearson Education, Inc. All rights reserved.

DigiPen Institute of Technology

Understand Computer Storage and Data Types

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Lecture 3 Tao Wang 1

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

Chapter 2 Elementary Programming

Programming in C++ 6. Floating point data types

A flow chart is a graphical or symbolic representation of a process.

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

CHAPTER 3 Expressions, Functions, Output

The C++ Language. Arizona State University 1

JAVA Programming Fundamentals

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

LECTURE 02 INTRODUCTION TO C++

Operators and Expressions:

Chapter 2: Using Data

JAVA OPERATORS GENERAL

CSC Web Programming. Introduction to JavaScript

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

Operators & Expressions

Programming with Java

Java enum, casts, and others (Select portions of Chapters 4 & 5)

Creating a C++ Program

Java Primer 1: Types, Classes and Operators

Engineering Computing I

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

Data Types and Variables in C language

Unit 3. Operators. School of Science and Technology INTRODUCTION

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

Programming for Engineers Introduction to C

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

C/C++ Programming for Engineers: Working with Integer Variables

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

BITG 1233: Introduction to C++

Chapter 6. Simple C# Programs

Transcription:

Chapter 2: Using Data

Declaring Variables Constant Cannot be changed after a program is compiled Variable A named location in computer memory that can hold different values at different points in time 2

3

Declaring Variables (cont d.) Variable declaration A 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 4

Displaying Variable Values 5

Displaying Variable Values (cont d.) 6

Displaying Variable Values (cont d.) 7

Displaying Variable Values (cont d.) 8

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 9

Displaying Variable Values (cont d.) 10

Displaying Variable Values (cont d.) 11

Displaying Variable Values (cont d.) Formatting output Console.WriteLine("{0, 5}", 4); Console.WriteLine("{0, 5}", 56); Console.WriteLine("{0, 5}", 789); Console.WriteLine("{0, -8}{1, -8}", "Richard", "Lee"); Console.WriteLine("{0, -8}{1, -8}", "Marcia", "Parker"); Console.WriteLine("{0, -8}{1, -8}", "Ed", "Tompkins"); Concatenate strings using the plus (+) sign Console.WriteLine("Concatenated " + "string"); 12

Displaying Variable Values (cont d.) 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) 14

Using Floating-Point Data Types Floating-point number Contains decimal positions Floating-point data types float Can hold up to 7 significant digits of accuracy double Can hold 15 or 16 significant digits of accuracy decimal Can hold 29 significant digits but a smaller range Suitable for financial and monetary calculations 15

Using 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 (modulus) always take place prior to addition or subtraction in an expression You can override normal operator precedence with parentheses Expressions are evaluated left to right 16

Using Arithmetic Operators (cont d.) 17

Using Shortcut Arithmetic Operators Add and assign operator Example: bankbal += bankbal * interestrate; Variations: =, *=, /=, and %= Prefix increment operator Example: ++somevalue; Postfix increment operator Example: somevalue++; 18

Using Shortcut Arithmetic Operators (cont d.) Unary operator Uses only one value (e.g., 123) Decrement operator (--) Can be prefix or postfix 19 19

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 returns a Boolean value 20

Using the bool Data Type (cont d.) 21

Using the bool Data Type (cont d.) // Example using a bool data type and a comparison // operator bool employeeworkedovertime = hoursworked > 40; if (employeeworkedovertime) { // Code to calculate overtime goes here } 22

Understanding Numeric Type Conversion Implicit cast Automatic transformation that occurs when a value is assigned to a type with higher precedence Example: adouble = anint; Explicit cast Placing the desired result type in parentheses followed by the variable or constant to be cast Example: anint = (int)adouble; 23

Using the char Data Type char data type Holds any single Unicode character Place constant character values within single quotation marks (e.g., 'A') Escape sequence Stores a pair of characters Begins with a backslash A pair of symbols represents a single character 24

Using the char Data Type (cont d.) 25

Using the string Data Type string data type Holds a series of characters Values are expressed within double quotation marks Comparing strings Use == and!= Use the Equals() method, Compare() method, and CompareTo() method 26

Using the string Data Type (cont d.) 27

28

Using the string Data Type (cont d.) Use the Length property of a string to determine its length Example: string astring = "How long is this string?"; Console.WriteLine("{0} It is {1} characters long.", astring, astring.length); 29

Using the string Data Type (cont d.) Use the Substring() method to extract a portion of a string from a starting point for a specific length 30

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 31

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 32

Accepting Console Input (cont d.) 33

34

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 35