Numeric Types, Expressions, and Output ROBERT REAVES.

Slides:



Advertisements
Similar presentations
Types and Arithmetic Operators
Advertisements

IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Javascript Essentials How do I write it??  Start Homesite  Between the start and end BODY tags type: 
© 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.
1 Chapter 3 Arithmetic Expressions. 2 Chapter 3 Topics l Overview of Java Data Types l Numeric Data Types l Declarations for Numeric Expressions l Simple.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Data types and variables
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
1 Chapter 3 Topics Constants of Type int and float l Evaluating Arithmetic Expressions l Implicit Type Coercion and Explicit Type Conversion l Calling.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Expressions An expression is a sequence of operands and operators that reduces to a single value expression operator operand An operator is a language-specific.
Chapter 2 Data Types, Declarations, and Displays
JavaScript, Third Edition
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations System-oriented Programming, B. Hirsbrunner,
Data Types, Expressions and Functions (part I)
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.
Chapter 2 Data Types, Declarations, and Displays.
Objectives You should be able to describe: Data Types
Chapter 2 Getting Started in C Programming
A First Book of ANSI C Fourth Edition
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
***** SWTJC STEM ***** Chapter 2-3 cg 29 Java Operators Recall Java’s programming components: Packages - Collection of classes (Programs) Classes - Collections.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
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.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
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.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Operators in Python. Arithmetic operators Some operators in Python will look familiar (+, -, *, /) Others are new to you (%, //, **) All of these do work.
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
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.
CHAPTER 2 COMPONENTS OF A PROGRAMMING LANGUAGE I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
Computer Engineering 1 st Semester Dr. Rabie A. Ramadan 3.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Lesson 6 Getting Started in C Programming Hu Junfeng 2007/10/10.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Chapter 6 Mathematical Operations. 6.1 Mathematical Expressions In mathematics this expression is valid 0 = -4y + 5 It is invalid in programming Left.
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.
Data Types Declarations Expressions Data storage C++ Basics.
Copyright © – Curt Hill Types What they do.
CHAPTER 2 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Types of C Variables:  The following are some types of C variables on the basis of constants values it has. For example: ○ An integer variable can hold.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Variables Symbol representing a place to store information
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
Chapter 3 The New Math. C++ Data Types simple integral charshort intlong bool floating float double Long double enum address pointer reference structured.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Variables, Operators, and Expressions
Chap. 2. Types, Operators, and Expressions
ITEC113 Algorithms and Programming Techniques
Expressions Chapter 4 Copyright © 2008 W. W. Norton & Company.
Conversion Check your class notes and given examples at class.
Expressions and Assignment
Presentation transcript:

Numeric Types, Expressions, and Output ROBERT REAVES

Additional String Operations  Four functions that we will use to operate on strings:  length  size  find  substr

Length Function  When applied to a string variable, it returns an unsigned integer value that equals the number of characters currently in the string.  If myName is a string variable, a call looks like this:  myName.length();  Use parentheses to signify an empty argument list.  This is a value-returning function, so the call must appear within an expression.

Dot Notation  Why the call to this function so weird?  Certain programmer-defined data types, such as strings, have functions that are associated with them and the dot notation is required in the calls.  myName.length();  Dot notation is the dot (period) between the variable name and the function name.  What is we don’t use the “dot” and say:  length();

Size Function  Same as length function.  This was to accommodate both terms, since people say size and length interchangeably.  Example:  myName.size();  Returns unsigned-integer value, thus you must declare results to be of type int.  string firstName;  int len;  firstName = “Robert”;  len = firstName.length(); //len = firstName.size();

Find Function  Find function searches a string to find the first occurrence of a particular substring and returns an unsigned integer value.  Takes one argument, a literal string or a string expression.  Ex:  str1.find(“the”);  str1.find(str2);  str1.find(str2 + “abc”);  str1 is searched to see if substring can be found within it.  If so, returns the position in str1 where the match beings. (Position start at 0)  Else returns “npos”, meaning not a position within the string”  Evaluates to something like  Has to match exact.

Find Function  Find function can also be a char value, in this case it returns the position of the first occurrence of that character within the string else “npos”.  Only returns the FIRST matched position.

Substr Function  Substr function returns a particular substring of a string.  Ex:  String myString = “Hello, my name is Robert.”;  myString.substr(5, 20);  First argument is an unsigned integer that specifies a position within the string, and the second is an unsigned integer that specifies the length of the desired substring.  Returns the piece of the string that starts at the specified location and continues for the number of characters given by the second argument.  DOESN’T CHANGE YOUR ORIGINAL STRING

Numeric Data Types  Data types:  char, short, int, long  Are known as integral types (or integer types) because they refer to integer values, in other words WHOLE NUMBERS.  ex:  22  16  1  0  Commas are NOT ALLOWED  Minus sign preceding an integer will make it negative.

Numeric Data Types  Exception to this is when you add the reserved word unsigned to the data type name.  Ex: unsigned int  Assumed to only be positive or zero.  Used for specialized situations.  Meaning you won’t use these in here.  Each data type is intended to represent different sizes of integers, however this is machine dependent.

Numeric Data Types  Wait, why is a char an integer type?  C++ classifies char as an integral type so it can be used to store integer values with a very limited range  -128 to 127

Numeric Data Types  Float, or floating type are used to represent real numbers.  Have an integer part and a fractional part, with a decimal between.  Ex:  18.0  .8  Computer cannot always represent exactly.  Slight inaccuracies in the rightmost fractional digits are to be expected and are not the result of programmer error.  Ex:  4.8 might be

Named Constant Declartions  Just to show you can make constant variables of all data types.  const float PI = ;  const float E = ;  const int MAX_SCORE = 100;  const int MIN_SCORE = -100;  const char LETTER = ‘W’;  const string NAME = “Elizabeth”;

Arithmetic Operators  Operators allowed in an expression depend on the data types of the constants and variables in the expressions.  Arithmetic operators:  + Unary Plus  - Unary minus  + Addition  - Subtraction  * Multiplication  /  Floating-point division (floating-point result)  Integer division (no fractional part)  % Modulus (remainder from integer division.

Arithmetic Operators  Unary Operators an operator that has just one operand.  Ex:  -54   -rate (can use on variables)  Rarely use unary plus, numeric constant is assumed to be positive.  Binary Operators an operator that has two operands.

Arithmetic Operators  % (modulus) is only used with integers.  Gives only the remainder.  10 / 7 = 3  / (division) can be used with integers or float  Integer division yields ONLY the quotient  10 / 7 = 1  Floating point yields a floating point result  10.0 / 7.0 = 1.3

Increment and Decrement Operators  C++ provides increment and decrement operators  ++ increment  -- decrement  Unary operators take a single variable as an operand.  For int and float operands, the effect is to add 1 or subtract 1 from the operand.  If num currently contains 8, what would this statement yield?  num++;  and  num--;

Increment and Decrement Operators  The ++ and – operators can be either prefix operators:  ++num;  or postfix operators:  num++;

Library Functions  C++ system includes a standard library-a large collection of prewritten functions, data types, and other times that any C++ programmer may use.  The functions in library are divided into separate files called header files.  Ex:  #include  This library had this like sqrt() and abs()