Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)

Slides:



Advertisements
Similar presentations
Data types and variables
Advertisements

CS150 Introduction to Computer Science 1
JavaScript, Fourth Edition
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
JavaScript, Third Edition
Basic Elements of C++ Chapter 2.
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.
Objectives You should be able to describe: Data Types
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Chapter 2: C Fundamentals Dr. Ameer Ali. Overview C Character set Identifiers and Keywords Data Types Constants Variables and Arrays Declarations Expressions.
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
C Tokens Identifiers Keywords Constants Operators Special symbols.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
CPS120: Introduction to Computer Science Variables and Constants Lecture 8 - B.
Sales person receive RM200/week plus 9% of their gross sales for that week. Write an algorithms to calculate the sales person’s earning from the input.
CSE 1301 Lecture 2 Data Types Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
1 CSC103: Introduction to Computer and Programming Lecture No 6.
Chapter 2: Using Data.
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.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
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.
Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
COMPUTER PROGRAMMING. variable What is variable? a portion of memory to store a determined value. Each variable needs an identifier that distinguishes.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
CHAPTER # 2 Part 2 PROGRAMS AND DATA 1 st semster King Saud University College of Applied studies and Community Service CSC1101 By: Asma Alosaimi.
CONSTANTS Constants are also known as literals in C. Constants are quantities whose values do not change during program execution. There are two types.
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.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Constants, Variables and Data types in C The C character Set A character denotes any alphabet, digit or special symbol used to represent information.
CPS120: Introduction to Computer Science Variables and Constants.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
C Building Block Chapter 2. Variables A variable is a space in the computer’s memory set aside for a certain kind of data and given a name for easy reference.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
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
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
Chapter # 2 Part 2 Programs And data
Chapter 2: Basic Elements of C++
Chapter Topics The Basics of a C++ Program Data Types
Chapter 2: Introduction to C++
BASIC ELEMENTS OF A COMPUTER PROGRAM
Computing Fundamentals
Documentation Need to have documentation in all programs
Basic Elements of C++.
Data Types, Identifiers, and Expressions
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2: Introduction to C++
Basic Elements of C++ Chapter 2.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
2.1 Parts of a C++ Program.
CS111 Computer Programming
C++ Data Types Data Type
Chapter 2: Java Fundamentals
Chapter # 2 Part 2 Programs And data
Chapter 2: Introduction to C++.
C Programming Lecture-3 Keywords, Datatypes, Constants & Variables
Variables and Constants
Programming Fundamental-1
Presentation transcript:

Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER) 1

VARIABLES A quantity whose value may change during program execution is called variable. It is represented by a symbol or a name. A variable represents a storage or memory location in the computer memory. Data is stored into the memory location. The name of the memory location i.e. the variable name remains fixed during execution of the program but the data stored in that location may change from time to time. 2

Conti…. A variable is also known as object in c++. In c++ a variable name consists of alphabets and digits. 3

Rules for writing variable name The following are the rules for writing a variable name in a program in c++. The first character of variable name must be an alphabetic character. underscore can be used as first character of variable name. blank space are not allowed in a variable name. 4

Conti…. Special characters such as arithmetic operators,#,^, cannot be used in a variable name. Reserved words cannot be used as variable names. The maximum length of a variable name depends upon the compiler of c++. A variable name declared for one data type cannot be used to declare another data type. 5

Conti….. C++ is a case sensitive language. Thus variable names with same spelling but different case are treated as different variable name. For example variable ‘PAY’ and ‘pay’ are two different variable. Following are some example of the valid and invalid variables names. 6

Variable nameValid/invalidremarks NadeemValid PerformValid doubleInvalidC++ reserved word Foxprovalid switchInvalidC++ reserved word MarriamValid IntInvalidC++ reserved word 3taqInvalidStarts with a number UnsignedinvalidC++ reserved word Taq ahdInvalidSpace is not allowed X-yInvalidSpecial character is not allowed 7

Declaration of variables Assigning the name and data type that a variable can hold is called declaration of the variable. All variables that are used in a program are declared using variable declaration statement. The syntax to declare a variable in c ++ is : type list of variable 8

Conti…. Type,specify data type of variable. For example, it may be int, float,etc. list of variables specify a list of variables separated by comma. In a single statement more than one variables, separated by commas, of same data type can be declared. For example, integer type. int a,b,c,d; float abc; char name; 9

Initialization of variable When a variable is declared, a memory location is assigned to it. The value in that memory location is also assigned to the variable. The value is assigned to the variable at the time of its declaration is called initializing of the variable. For example, to declare variable a,b,and c of integer type and assigning value a=10,b=20,c=30, the statement is written as: int a=10,b=20,c=30; 10

Write a program to assign values to the different variables at the time of declaration. print that values. # include main() { int a =4,b=5; float c=3.5; char name[15]=“marriam ahmed”; cout<<name<<endl; cout<<a<<b<<c; getch(); } 11

Data types in c ++ The variable types specify the type of data that can be stored in it. Each variable is declared by its type. C++ has five basic data types. These data types are: 1.intinteger 2.floatfloating point 3.doubledouble precision 4.char characters 5.boolboolean 12

TypeSize in bytes int2 bytes short int2 bytes long int4 bytes unsigned int2 bytes unsigned long int4 bytes float4 bytes long float8 bytes double8 bytes long double10 bytes char1 byte 13

CONSTANTS A quantity that cannot change its values during execution of program is called constants. There are four types of constants in c++. these are: 1.integer constants 2.floating constants 3.character constants 4.string constants 14

Integer constants A numerical value without a decimal part is called integer constant. The plus(+) and minus (-) sign can also be used with an integer constant. Integer constants are used in expression for calculations. To print an integer constant,it is given in the output statement without quotation marks. For example,to print integer constant 520 the output statement is cout<<520; 15

Floating –point constants Numeric values that have an integer as well as a decimal part are called floating –point values. For example CHARACTER CONSTANTS A single character enclosed in a single quotation marks is called character constants. For example ‘a’,’\’,’+’, represent character constants. 16

String constants A sequence of character consisting of alphabets, digits or special character enclosed in double quotation marks is called string constants For example “mihe Kabul", "Afghanistan” 17

The constant qualifier The data item that follows the keyword “const” cannot change its value during execution of the program. A value is assigned to a data item at the time of its declaration. If numeric type value is assigned then the data item can be used in expression for calculation. 18

PROGRAM # include main () { int r; const float p =3.14 ; float peri; r = 2; peri = 2*p*r; cout <<“result is =“<<peri; } 19

The “define” directive it is a preprocessor directives. It is used to define a constant quantity. It is used at the beginning of the program. # define identifier constant identifier : it specifies the character to which the constant value is to be assigned constant : it specifies the constant values that is to be assigned to the identifier. 20

The value assigned to the identifier remains the same throughout the execution of program. A variable of the name of the identifier cannot be declared in the program. Also, the identifier does not have any data type but any data can be assigned to it. 21

Program: ‘p’ has been assigned a constant value of 3.14 in the beginning of the program using the define directive. # include # define p 3.14 main () { int r; float peri; r=2; peri= 2*p*r; cout<<“result is =“<<peri; getch(); } 22

Arithmetic expression. An arithmetic expression is a combination of variables, constants and arithmetic operator. It is used to calculate the value of an arithmetic formula. It returns a single value after evaluating the expression. the value of the expression is assigned to a variable on the left side of “=“ operator. This variable is known as the receiving variable. 23

Conti……. The ‘=‘ operator is called assignment operator. It is used to assign the calculated value of the expression to the receiving variable. The receiving variable must be of numeric type. For example if m= 10,x=5 the expression may be written as m * x It is an arithmetic expression. This expression return a single value. This value is assigned to a single variable using the assignment operator. Thus, if “res” is the receiving variable the assignment statement is written as res=m * x + 100; 24

Relational Expressions A relational expression consists of constants, variables or arithmetic expression that are combined by a relational operator. A relational expression is written to find a relation between two expressions. It returns only one value, which is either true or false. For example 10>6 is a relational expression. It indicates a relation between two constants. Since the constants value is greater than 6 this relational expression returns a true value. 25

The operator ‘>’ used between the two values is called the relational operator. It specifies relation between two arithmetic expression or values. 26