Constants, Data Types and Variables

Slides:



Advertisements
Similar presentations
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
Advertisements

Object Oriented Programming in JAVA
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/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
CS150 Introduction to Computer Science 1
Chapter 2 Data Types, Declarations, and Displays
Elements of a C++ program 1. Review Algorithms describe how to solve a problem Structured English (pseudo-code) Programs form that can be translated into.
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.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Five Tips to Success. Work hard Try more exercises and more practice.
1 CSC103: Introduction to Computer and Programming Lecture No 6.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
CPS120: Introduction to Computer Science
Lecture #5 Introduction to C++
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
C++ Programming: Basic Elements of C++.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Primitive Variables.
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
 Character set is a set of valid characters that a language can recognise.  A character represents any letter, digit or any other sign  Java uses the.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
What is C? C is a programming language. It was developed in 1972 USA. It was designed and written by a man named dennis ritchie. C is the base for all.
Primitive Variables.
CONSTANTS Constants are also known as literals in C. Constants are quantities whose values do not change during program execution. There are two types.
VARIABLES AND DATA TYPES Chapter2:part1 1. Objectives: By the end of this section you should: Understand what the variables are and why they are used.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
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.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Constants, Variables and Data types in C The C character Set A character denotes any alphabet, digit or special symbol used to represent information.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
CPS120: Introduction to Computer Science Variables and Constants.
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Lecture 4 Monday Sept 9, Variables and Data Types ►A►A►A►A variable is simply a name given by the programmer that is used to refer to computer storage.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Chapter 3 Using Variables, Constants, Formatting Mrs. UlshaferSept
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
INTRODUCTION TO C LANGUAGE
TK1913 C++ Programming Basic Elements of C++.
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Variables Mr. Crone.
BASIC ELEMENTS OF A COMPUTER PROGRAM
Programming Fundamentals
Documentation Need to have documentation in all programs
Computing Fundamentals
ITEC113 Algorithms and Programming Techniques
Basic Elements of C++.
Chapter 2: Introduction to C++
Basic Elements of C++ Chapter 2.
DATA STRUCTURE : DAT JENIS DATA DAN JENIS DATA ABSTRAK (4JAM)
IDENTIFIERS CSC 111.
Tejalal Choudhary “Computer Programming” Fundamentals of “C”
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
2.1 Parts of a C++ Program.
CS111 Computer Programming
Chapter 2: Java Fundamentals
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Chapter # 2 Part 2 Programs And data
Chapter 2: Introduction to C++.
C++ Programming Lecture 3 C++ Basics – Part I
Fundamental Programming
C++ Programming Basics
Variables and Constants
Programming Fundamental-1
Presentation transcript:

Constants, Data Types and Variables Programming in C++ Constants, Data Types and Variables

Constants A constant is the quantity that does not change. This quantity can be stored at a locations in the memory of the computer. A variable can be consider as a name given to the location in memory where this constant is stored. Three Basic Types of Constant Integer Constants Real Constants String Constants

Integer Constant Rules for Constructing Integer Constant An integer constant must have at least one digit. It must not have a decimal point. It could be either negative or positive. If no sign precedes an integer constant it is assumed to be positive. No commas or blanks are allowed in integer constant. Example 426 -777 +20000

Floating Point Constant / Real Constants Rules for Constructing Real Constant A real constant must have at least one digit. It must have a decimal point. It could be either positive or negative Default sign is always positive. No commas or blanks are allowed in real constant. Example +300.25 221.005 -19845.0

Character Constant Rules for Constructing Character Constants A character constant is either a single alphabet, a single digit or a single special symbol enclosed within Single inverted commas. The maximum length of a character constant can be 1 character. Example ’a’ ’5’ ’=’ ’G’

Data Types Built-in Data Type A computer Program operates on data and produce an output. In C++, each data must be of specific data type. The data type determines how the data is represented in the computer and kind of processing the computer can perform on it. There are two kind of data types. Built-in data type User define data type Built-in Data Type The are already defined by C++. int ( For working with integer numbers) float (For working with real numbers having decimal points) char ( for working with character data)

Variables Variable is a location in memory, referenced by an identifier, that contain a data value that can be changed. Identifier: name given to a variable is known as identifier. Rules for writing Identifier The first character must be letter or underscore ( _ ). You can use upper and lowercase letters and digits from 1 to 9. Identifier can be as long as you like but only the first 250 character are recognizable in C++ Compiler.

Integer Variables Integer variables represent integer numbers like 1, 30,000 and -45. Integer variables do not store real numbers. Defining integer variables The amount of memory occupied by integer types is system dependent. On 32-bit system like windows 98/XP an integer occupies 4 bytes of memory. This allows an integer variable to hold numbers in the range from -2,147,483,648 to 2,147,483,647.

Integer Variables Example #include<iostream.h> void main ( ) { int var1; //define var1 int var2, var3; //define var2, var3 var1 = 20; //assign value to var1 var2 = var1 + 10; //assign value to var2 cout<<“Result =”; cout<<var2<< endl; //displaying the sum of var1 + 10 }

Character Variables Type char stores integer that range in value from -128 to 127. Variables of this type occupy only 1 byte of memory. Character variables are sometime use to store number but they are much more commonly used to store ASCII characters. Character Constants Character constants use single quotation marks around a character, like ‘a’ and ‘b’. When the C++ compiler encounters such a character constant, it translates it into the corresponding ASCII code.

Character Variable Example #include<iostream.h> void main ( ) { char charvar1 = ‘A’; //define char variable char charvar2 = ‘\t’; cout << charvar1; // display character cout << charvar2; charvar1 = ‘B’; //reassigning charvar1 to B cout << charvar1; }

Floating point Types Floating point variables represent number with a decimal place e.g. 3.14787 or 10.2 There are three kind of floating point variables in C++ float double long double

float Type float stores numbers in the range of about to . It occupy 4 bytes of memory. Double and Long Double: 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.

float #include<iostream.h> void main ( ) { float rad, area; float PI =3.14; cout << “Enter radius of circle”; cin >> rad; area = PI * rad * rad; cout <<“Area is” << area << endl; }

Variable Type Summary Bytes of Memory Digits of precision High range Low Range Keyword 1 n/a 127 -128 Char 2 32,767 -32,768 short 4 2,147,483,647 -2,147,483,648 int long 7 float 8 15 double