Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal 6.3333.

Slides:



Advertisements
Similar presentations
This Time Whitespace and Input/Output revisited The Programming cycle Boolean Operators The “if” control structure LAB –Write a program that takes an integer.
Advertisements

Chapter 3: Expressions and Interactivity. Outline cin object Mathematical expressions Type Conversion and Some coding styles.
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.
CS 1400 Chapter 2 sections 1, 2, 4 – 6, 8,
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Chapter 3 Assignment and Interactive Input. 2 Objectives You should be able to describe: Assignment Operators Mathematical Library Functions Interactive.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
Announcements Quiz 1 Next Week. int : Integer Range of Typically -32,768 to 32,767 (machine and compiler dependent) float : Real Number (i.e., integer.
1 CS 105 Lecture 3 Constants & Expressions Wed, Jan 26, 2011, 4:15 pm.
Chapter 2 Data Types, Declarations, and Displays
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Basic Elements of C++ Chapter 2.
Section 2 - More Basics. The char Data Type Data type of a single character Example char letter; letter = 'C';
COMPUTER SCIENCE I C++ INTRODUCTION
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
Input & Output: Console
1 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 2 Primitive Data Types and Operations.
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is a legal identifier? what.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 2: Using Data.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
C++ Programming: Basic Elements of C++.
CHAPTER 7 DATA INPUT OUTPUT Prepared by: Lec. Ghader R. Kurdi.
Chapter 2 Overview of C++. 2 Overview  2.1 Language Elements  2.2 Reserved Words & Identifiers  2.3 Data Types & Declarations  2.4 Input/Output 
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 2 Elementary Programming.
Control Structures (B) Topics to cover here: Sequencing in C++ language.
CSC 107 – Programming For Science. Announcements.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Chapter 2: Introduction to C++. Language Elements Keywords Programmer-defined symbols (identifiers) Operators Punctuation Syntax Lines and Statements.
Data Types Declarations Expressions Data storage C++ Basics.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
ITEC 109 Lecture 7 Operations. Review Variables / Methods Functions Assignments –Purpose? –What provides the data? –What stores the data? –What type of.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
Today’s Lecture  Literal  Constant  Precedence rules  More assignment rules  Program Style.
1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
COMP 110: Spring Announcements Lab 1 due Wednesday at Noon Assignment 1 available on website Online drop date is today.
A Sample Program public class Hello { public static void main(String[] args) { System.out.println( " Hello, world! " ); }
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
A Sample Program #include using namespace std; int main(void) { cout
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
arithmetic operator & cin I.Mona Alshehri The output formatting functions setw(width) setw(n) - output the value of the next expression in n columns.
Chapter 4 Strings and Screen I/O. Objectives Define strings and literals. Explain classes and objects. Use the string class to store strings. Perform.
Bill Tucker Austin Community College COSC 1315
Chapter 1.2 Introduction to C++ Programming
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Computing and Statistical Data Analysis Lecture 2
Chapter 3 Assignment and Interactive Input.
Completing the Problem-Solving Process
Programming Fundamentals
Basic Elements of C++.
Basic Elements of C++ Chapter 2.
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is legal identifier? what.
Introduction to C++ Programming
Programming Funamental slides
Introduction to C++ Programming
CS150 Introduction to Computer Science 1
Chapter 2: Introduction to C++.
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
What Actions Do We Have Part 1
Chapter 1 c++ structure C++ Input / Output
Presentation transcript:

Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal floating point literal 6.0 floating point literal 6E7 floating point literal 'a' character literal “Simon” String literal true bool literal

Constants It is often useful to name literals, that is, to use symbolic names rather than the literals directly. The const modifier is used to declare a data object whose value cannot be changed once it is initialized (and so it must be initialized to set the original value) const double PI = ;

String Literals String literals are sequences of characters enclosed in double quotes ( “ ). This is different from char literals which consist of a single character enclosed in single quotes ('). You can put a double quote inside of a string by using an escape sequence, that is, preceding the double quote by a backslash. Example: “This is a double quote \”.”

Assignment Compatibility A data object can only hold one type of object (int, float, double, char, string), but sometimes you can convert an object of one type to another type before storing it. For example, double a = 4; is perfectly fine. 4 is converted to a double, 4.0, before being stored in a. int b = 3.6; is not okay.

Shorthand Assignment Statements In C++ you can abbreviate an assignment statement which modifies the variable on the left hand side. For example, a += 5; is shorthand for a = a + 5; Similarly, *=, -=, /= are shorthand for their respective operations

Integer Division One thing to watch out for is integer division. If you divide two ints, the result is another int, not a floating point number. So, 19/4 is 4, not If you divide two floating point numbers, the result is a floating point number: 19.0/4.0 is If you divide an int by a floating point number, the int is converted to floating point first.

Increment and Decrement C++ provides shorthand operators for incrementing and decrementing values. n++; increments the value of n by 1 and the value is n before the increment ++n; increments the value of n by 1 and the value is n after the increment n-- and --n are similar, but decrement the value.

Output The variable cout represents the output stream, that is, normally the terminal. You can send stuff to the output stream by using the << operator. These can be chained together to print out different items. For example, cout << “The total of “ << a << “ and “ << b << “ is “ << a + b << endl; The constant endl prints out a new end of line character.

Input The variable cin represents standard input, that is, normally the keyboard. You can use the operator >> to read in and set variables. For example, int a, b; cin >> a >> b; will read in two int s and store them in a and b.

Exercise Write a C++ program that will prompt the user for a temperature (in Fahrenheit) and then convert that temperature to Celsius and print out the new temperature along with a text message. You will need: A double variable, f, to represent the Fahrenheit temperature A double variable, c, to represent the Celsius temperature

Exercise (cont'd) An output line to print out the prompt message An input line to read in the Fahrenheit temperature A line to calculate and store the Celsius temperature (c = (f – 32) * 5/9. A line to print out the result.