Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.

Slides:



Advertisements
Similar presentations
Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.
Advertisements

What Data Do We Have? Sections 2.2, 2.5 August 29, 2008.
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
CS150 Introduction to Computer Science 1
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
String Escape Sequences
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Basic Elements of C++ Chapter 2.
Variable & Constants. A variable is a name given to a storage area that our programs can manipulate. Each variable in C has a specific type, which determines.
Input and Output in Console Mode UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
Objectives You should be able to describe: Data Types
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
CSI 2372: Advanced Programming Concepts with C++
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
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Lecture #5 Introduction to C++
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Week 1 Algorithmization and Programming Languages.
Programming Fundamentals. Today’s Lecture Why do we need Object Oriented Language C++ and C Basics of a typical C++ Environment Basic Program Construction.
C++ Programming: Basic Elements of C++.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
CHAPTER 7 DATA INPUT OUTPUT Prepared by: Lec. Ghader R. Kurdi.
C++ How to Program, Late Objects Version, 7/e © by Pearson Education, Inc. All Rights Reserved.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
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 1: INTRODUCTION C++ Programming. CS 241 Course URL: Text Book: C++ How to Program, DETITEL & DEITEL, eighth Edition.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
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
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
1 A more complex example Write a program that sums a sequence of integers and displays the result. Assume that the first integer read specifies the number.
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.
Constants, Data Types and Variables
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Chapter 2 of C++ How to Program, 10/e © by Pearson Education, Inc. All Rights Reserved.
© by Pearson Education, Inc. All Rights Reserved.
Chapter 1.2 Introduction to C++ Programming
Chapter Topics The Basics of a C++ Program Data Types
What Actions Do We Have Part 1
Introduction to C++ Programming
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
BASIC ELEMENTS OF A COMPUTER PROGRAM
Programming Fundamentals
Computing Fundamentals
Basic Elements of C++.
Variables and Primative Types
Basic Elements of C++ Chapter 2.
Introduction to C++ Programming
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Chapter 2: Introduction to C++.
What Actions Do We Have Part 1
C++ Programming Basics
Chapter 1 c++ structure C++ Input / Output
Variables and Constants
Presentation transcript:

Programming Fundamentals

Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types

Today’s Lecture Manipulators Char type Floating-Point Types Input with cin

Manipulators Manipulators are instructions to the output stream that modify the output in various ways.

The endl Manipulator This causes a linefeed to be inserted into the stream, so that subsequent text is displayed on the next line. It has the same effect as sending the ‘\n’ character. Its an example of a manipulator.

Other Integer Types There are several numerical integer types besides type int. The two most common types are long and short. Type char is an integer type as well.

Character Variables Type char stores integers that range in value from –128 to 127. Variables of this type occupy only 1 byte (eight bits) of memory. Character variables are sometimes used to store numbers that confine themselves to this limited range, but they are much more commonly used to store ASCII characters.

ASCII characters

Character Variables The ASCII character set is a way of representing characters such as ‘a’, ‘B’, ‘$’, ‘3’, and so on, as numbers. These numbers range from 0 to 127.

Character Constants Character constants use single quotation marks around a character, like ‘a’ and ‘b’. Note: This differ from string constants, which use double quotation marks. When the C++ compiler encounters such a character constant, it translates it into the corresponding ASCII code. The constant ‘a’ appearing in a program, for example, will be translated into 97

Character variables can be assigned character constants as values. Character Constant Horizontal Tab Character Constant Escape Sequence for new line, alternative to endl manipulator

Output

Example ASCII code for ‘A’ ASCII code for horizontal tab ASCII code for ‘B’ No endl or ‘\n’ in source code so cursor blinking after last displayed charcter

Initialization Variables can be initialized at the same time they are defined. In this program two variables of type char—charvar1 and charvar2—are initialized to the character constants ‘A’ and ‘\t’.

Escape Sequences

Since the backslash, the single quotation marks, and the double quotation marks all have specialized meanings when used in constants, they must be represented by escape sequences when we want to display them as characters.

Example Here’s an example of a quoted phrase in a string constant:

Floating Point Types Floating-point variables represent numbers with a decimal place—like , ,and –10.2. They have both an integer part, to the left of the decimal point, and a fractional part, to the right.

Kinds of floating-point variables There are three kinds of floating-point variables in C++: type float, type double, and type long double.

Type float Type float stores numbers in the range of about 3.4x10^–38 to 3.4x10^38. It occupies 4 bytes (32 bits) in memory.

Example Constant variable Input with cin

Here’s a sample interaction with the program:

Type double and long double The larger floating point types, double and long double, are similar to float except that they require more memory space and provide a wider range of values and more precision. Type double requires 8 bytes of storage and handles numbers in the range from 1.7x10^–308 to 1.7x10^308. Type long double is compiler- dependent but is often the same as double.

Floating-Point Constants The number F is an example of a floating- point constant. The decimal point signals that it is a floating- point constant, and not an integer, and the F specifies that it’s type float, rather than double or long double. The number is written in normal decimal notation. You don’t need a suffix letter with constants of type double; it’s the default. With type long double, use the letter L.

Contdd.... You can also write floating-point constants using exponential notation. Exponential notation is a way of writing large numbers without having to write out a lot of zeros. For example,1,000,000,000 can be written as 1.0E9 in exponential notation.

The const Qualifier The keyword const (for constant) precedes the data type of a variable. It specifies that the value of a variable will not change throughout the program. Any attempt to alter the value of a variable defined with this qualifier will elicit an error message from the compiler.

Example Compile-Time Error

The #define Directive Constants can also be specified using the preprocessor directive #define. Syntax is # define identifier replacement-text For example, the line appearing at the beginning of your program specifies that the identifier PI will be replaced by the text throughout the program.

The statement cin >> rad causes the program to wait for the user to type in a number. The resulting number is placed in the variable rad. Input with cin

The keyword cin (pronounced “C in”) is an object, predefined in C++ to correspond to the standard input stream. This stream represents data coming from the keyboard. The >> is the extraction or get from operator. It takes the value from the stream object on its left and places it in the variable on its right.

Variables Defined at Point of Use Note that in this example variables are defined at point of use (e.g.; area). You can define variables throughout a program, not just at the beginning.

Cascading << The program first sends the phrase Area is: to cout, then it sends the value of area, and finally the endl manipulator. The extraction operator >> can be cascaded with cin in the same way, allowing the user to enter a series of values.

Example: Input with cin Example1

OUTPUT

Cascading Example

OUTPUT Two numbers separated by space

OUTPUT Two numbers separated by line

QUESTIONS????