SlideShare a Scribd company logo
UNIT-2
DATA TYPES, OPERATORS, AND
CONTROL STRUCTURES
FACULTY NAME: Er. Kamaldeep Kaur
CHAPTER CONTENTS
 BASIC DATA TYPES, DERIVED DATA TYPES,
 KEYWORDS, IDENTIFIERS, CONSTANTS AND VARIABLES,
 TYPE CASTING,
 OPERATORS, AND OPERATOR PRECEDENCE.
 CONTROL STRUCTURES: IF STATEMENT, SWITCH-CASE, FOR,
WHILE AND DO-WHILE LOOPS, BREAK AND CONTINUE
STATEMENT
INTRODUCTION
 C++ was developed by Bjarne Stroustrup, in
early 1980s.
 C++ is an extension of C with a major addition
of the class construct feature.
 Stroustrup initially called the new language as
‘C with classes’.
 The name was later changed to C++, from the
increment operator ++, suggesting that C++ is
an incremented version of C.
C++ BASIC ELEMENTS
 Programming language is a set of rules, symbols,
and special words used to construct programs.
 Character Set: a set of valid characters that a
language can recognize.
 Letters: A-Z, a-z
 Digits: 0-9
 Special Characters
Space + * / ^  () [] {} = != <> ‘ “ $ , ; : % ! & ?
_ # <= >= @
 Formatting characters backspace, horizontal tab, vertical tab,
and carriage return
TOKENS
 Tokens are the basic building blocks in C++
language.
 The smallest individual units in a program are known
as tokens.
 A program is constructed using a combination of
these tokens.
TYPES OF TOKENS
 KEYWORDS
 IDENTIFIERS
 CONSTANTS
 STRING
 SPECIAL SYMBOLS (PUNCTUATORS)
 OPERATORS
KEYWORDS
 Explicitly reserved identifiers
 Cannot be used as names for the program variables
or other user-defined program elements
 There are 32 keywords, that were present in C
language and have been carried over into C++ :
 auto const double float int short struct
unsigned break continue else for long signed
switch void case default enum goto register
sizeof typedef volatile char do extern if return
static union while
KEYWORDS
 There are another 30 reserved words that were not
in C, are therefore new to C++, and here they are:
 asm dynamic_cast namespace
reinterpret_cast try bool explicit new
static_cast typeid catch false operator template
typename class friend private this using const_cast
inline public throw virtual delete mutable protected
true wchar_t
IDENTIFIERS
 Identifiers refers to the name of variables,
functions, arrays, classes, etc. created by the user.
Identifiers are the fundamental requirement of any
language.
 Identifier naming conventions
 Only alphabetic characters, digits and underscores are
permitted.
 First letter must be an alphabet or underscore (_).
 Identifiers are case sensitive.
 Reserved keywords can not be used as an identifier's name.
IDENTIFIERS
IDENTIFIER NAME VALID OR INVALID
5th_element Invalid (begins with a digit)
_delete Valid
School.fee Invalid (dot not allowed)
register[5] Invalid (keyword)
Student[10] Valid
Employee name Invalid (white space)
Perimeter() Valid
IDENTIFIERS
 A major difference between C and C++ is the limit
on the length of a name.
 C recognizes only the first 32 characters in a name
 C++ places no limit on its length, and, therefore, all
the characters in a name are significant.
CONSTANTS
 Refer to fixed values that do not change during the
execution of a program.
 Also known as literals.
 Types of literals in C++:
 Integer constant
 Character constant
 Floating (real) constant
 String constant
CONSTANTS
 EXAMPLES
 123 //integer constant
 12.34 //floating point constant
 ‘A’ //character constant
 “C++” //string constant
DATA TYPES
BASIC DATA TYPES
 ** void is used:
 To specify the return type of a function when it is
not returning any value
 To indicate an empty argument list to a function.
STRING
 used to store letters and digits.
 referred to as an array of characters as well as an individual
data type.
 enclosed within double quotes, unlike characters which are
stored within single quotes.
 The termination of a string in C++ is represented by the null
character, that is, ‘0’
 Examples:
 char name[30] = ‘’Hello!”;
 char name[] = “Hello!”;
 char name[30] = { ‘H’ , ’e’ , ’l’ , ’l’ , ’o’};;
 string name = “Hello”
SPECIAL SYMBOLS (PUNCTUATORS)
Special
Character
Name Function
[ ]
Square
brackets
The opening and closing brackets of an array symbolize
single and multidimensional subscripts.
()
Simple
brackets
The opening and closing brackets represent function
declaration and calls, used in print statements.
{ } Curly braces
The opening and closing curly brackets to denote the start
and end of a particular fragment of code which may be
functions, loops or conditional statements
, Comma
We use commas to separate more than one statements, like
in the declaration of different variable names
#
Hash / Pound /
Preprocessor
The hash symbol represents a preprocessor directive used
for denoting the use of a header file
* Asterisk
We use the asterisk symbol in various respects such as to
declare pointers, used as an operand for multiplication
~ Tilde We use the tilde symbol as a destructor to free memory
. Period / dot The use the dot operator to access a member of a structure
OPERATORS
 Operators are tools or symbols which are used to
perform a specific operation on data.
 Operations are performed on operands.
 Operators can be classified into three broad
categories according to the number of operands
used:
 Unary operators
 Binary operators
 Ternary operators
UNARY OPERATORS
 The operators that operate a single operand to
form an expression are known as unary operators.
 Examples:
 + + (increment) operator,
 -- (decrement) operator,
 unary minus operator.
BINARY OPERATORS
 The operators that operate on two or more
operands are known as binary operators.
 Examples:
 Arithmetic Operators
 Assignment Operator
 Relational Operators
 Logical Operators
 E.g. a+b, a-b, a*b, a/b etc.
TERNARY OPERATORS
 The operator that operates minimum or maximum
three operands is known as ternary operator.
 There is only one ternary operator available in
C++.
 The operator ?: is the only available ternary
operator i.e. used as a substitute of if-else
statement.• E.g. a>b ? a:b
OPERATORS BASED ON TYPE OF OPERATION
 Arithmetic Operators
 Assignment Operators
 Relational Operators
 Logical Operators
 Increment Decrement Operators
 Conditional Operator
ARITHMETIC OPERATORS
 An operator that performs an arithmetic operation
is known as arithmetic operator
 It is a binary operator
ARITHMETIC OPERATORS
ASSIGNMENT OPERATORS
RELATIONAL OPERATORS
 An operator that is used to test the relation between
two values or operands.
 Relational Operators are BINARY operators.
 These operators evaluate to true or false.
 Return zero for false and a non zero for true.
 Also known as Comparison operators.
RELATIONAL OPERATORS
LOGICAL OPERATORS
 The operators that are used to combine the result of
one or more relation.
 These operators evaluate to true or false.
 Are Unary and Binary Operators.
LOGICAL OPERATORS
INCREMENT & DECREMENT OPERATORS
 ++ Increments value by 1
 x++ / ++x Equivalent to X=X+1
 -- Decrement value by 1
 x-- / --x Equivalent to X=X-1
 ++x and --y are called prefix operator, that means the increment or
decrement operators modify their operand before it is used or assigned.
 Example a = 10; b = ++a; a is incremented by 1 and then assign to b.
Therefore both a and b have the value of 11.
 x++ and y-- are called postfix operator, the increment or decrement
operators modify their operand after it is used. a = 10; b = a++; value of
a is assigned to b then a is incremented by 1. Therefore here a=11 and
b=10.
INCREMENT & DECREMENT OPERATORS
CONDITIONAL OPERATOR
OPERATOR PRECEDENCE & ASSOCIATIVITY
 Operator precedence determines the grouping of
terms in an expression.
 The associativity of an operator is a property that
determines how operators of the same precedence
are grouped in the absence of parentheses. This
affects how an expression is evaluated.
Data types, operators and control structures unit-2.pdf
TYPE CONVERSION
 It is the process of converting one data type to
another i.e. converting an expression of given type
into another
 Two types:
 Implicit (automatic)
 Explicit (type casting)
AUTOMATIC TYPE CONVERSION
 It is automatically performed when:
1. Constants and variables of different types are mixed in an
expression
 For a binary operator, if the operands type differ, the
compiler converts one of them to match with the other, using
the rule that smaller type is converted to the wider type.
 For example, if one of the operand is int and other is float,
then int is converted to float because a float is wider than int.
 For example, m = 5 + 2.75
 ‘5’ would be converted to float as 5.00
AUTOMATIC TYPE CONVERSION
2. An assignment operator also causes automatic type conversion.
 The type of data to the right of an assignment operator is
automatically converted to the type of the variable on the left.
 For example, int m;
float x=3.14459;
m=x;
 The fractional part is truncated.
 The type conversions are automatic with built in data types.
TYPE CASTING
 The explicit type conversion is done using type cast operator.
 Syntax: datatype(expression)
 datatype is the type which we want the expression to get
changed as
 For example,
int a;
float b,c;
cin>>a>>b;
c= float(a)+b;
cout<<c;
 O/P: 10 //a
12.5 //b
22.5 //c
CONTROL STRUCTURES
 Programs are written using four basic structures
 Sequence: a sequence is a series of statements that execute
one after another
 Selection(branching): selection (branch) is used to execute
different statements depending on certain conditions
 Repetition(loop or iteration): repetition (looping) is used to
repeat statements while certain conditions are met
 Jumping: jump from one point of program to another
 Called control structures or logic structures
SEQUENCE CONTROL STRUCTURE
 The sequence structure directs the computer to
process the program instructions, one after another,
in the order listed in the program.
SELECTION CONTROL STRUCTURE (BRANCHING)
 makes a decision and then takes an appropriate
action based on that decision
 Also called the decision structure
SELECTION (BRANCHING OR CONDITIONAL)
 If
 if-else
 nested if (if- elseif- elseif- else)
 switch
Simple If STATEMENT
 if(condition)statement
 If the condition is true, statement is executed.
 If the condition is false, statement is not executed.
 EXAMPLE:
if (x==100)
cout<<“x is 100”;
Simple If STATEMENT
If else STATEMENT
 if (condition)statement1
else statement2
 If condition is true, statement1 is executed and if the
condition is false, statement2 is executed
 EXAMPLE:
if (x==100)
cout<<“x is 100”;
else
cout<<“x is not 100”;
If else STATEMENT
Nested if STATEMENT
 When if statement occurs with in another if
statement, then such type of if statement is called
nested if statement.
 if (condition1)
if (condition2)
statement-1;
else
statement-2;
else
statement-3;
Nested if STATEMENT
Nested if STATEMENT
 EXAMPLE:
if(a>=b && a>=c)
cout<<“a is biggest”;
elseif(b>=a &&b>=c)
cout<<“b is biggest”;
else
cout<<“c is biggest”;
Switch STATEMENT
Switch STATEMENT
Switch STATEMENT
 EXAMPLE:
int main(){
int a; cin>>a;
switch(a) {
case 1: cout<<“the value is 1”; break;
case 2: cout<<“the value is 2”; break;
case 3: cout<<“the value is 3”; break;
default: cout<<“out of range”;
}
return 0;
}
DIFFERENCE BETWEEN SWITCH & IF-ELSE
REPETITION (LOOP OR ITERATION)
 directs computer to repeat one or more instructions
until some condition is met
 Also called a loop or iteration
REPETITION (LOOP OR ITERATION)
 While
 do-while
 For
 Nesting
LOOPS (ITERATION)
 Loops have an objective to repeat a certain number
of times or while a condition is fulfilled. When a
single statement or a group of statements will be
executed again and again in the program then such
type of processing is called loop. Loop is divided
into two parts
 Body of loop
 Control of loop
CONTROL OF LOOP
 Control of loop is divided into two parts:
 Entry control loop- in this first of all condition is
checked if it is true then body of the loop is executed.
Otherwise we can exit from the loop when the
condition becomes false. Entry control loop is also
called base loop. Example- While loop and for loop
 Exit control loop- in this first body is executed and
then condition is checked. If condition is true, then
again body of loop is executed. If condition is false,
then control will move out from the loop. Exit control
loop is also called Derived loop. Example- Do-while
LOOP STATEMENTS
 The loop statements in C++ language are-
 While loop
 Do loop/Do-while loop
 For loop
 Nested for loop
WHILE LOOP
WHILE LOOP
WHILE LOOP EXAMPLE
 #include<iostream.h>
int main()
{
int n;
cout<<“Enter the starting number”;
cin>>n;
while(n>0){
cout<<n<<“,”;
n--;
}
cout << “FIRE!“;
return 0;
}
DO WHILE LOOP
DO WHILE LOOP
DO WHILE EXAMPLE
WHILE VS DO WHILE
ENTRY CONTROLLED EXIT CONTROLLED
FOR LOOP
 For loop is an Entry control loop when action is to
be repeated for a predetermined number of times.
 Four parts
 Initialise expression
 Test expression (condition)
 Body (loop statements)
 Increment/ decrement (modification)
FOR LOOP
FOR LOOP EXAMPLE
NESTING OF FOR LOOPS
 A loop can be inside another loop.
 C++ can have 256 levels of nesting.
 for(init;condition;increment)
{
for(init;condition;increment)
}
statement(s);
}
statement(s);
}
JUMPING STATEMENTS
 Jump statements are used to alter the flow of
control unconditionally.
 That is, jump statements transfer the program
control within a function unconditionally.
 The jump statements defined in C++ are break,
continue, goto.
Goto STATEMENT
 It allows making an absolute jump to another point
in the program.
Goto STATEMENT
 #include<iostream.h>
int main()
{
int n=10;
loop:
cout<<n<<“,”;
n--;
if(n>0) goto loop;
cout << “FIRE!“;
return 0;
}
BREAK STATEMENT
 Goes straight to the end of a do, while or for loop or a switch
statement block.
 EXAMPLE:
#include<iostream.h>
int main(){
int n;
for(n=10;n>0;n--){
cout<<n<<“,”;
if(n==5){
cout<<“count down aborted!”;
break;}}
return 0;}
O/P: 10,9,8,7,6,5,count down aborted!
CONTINUE STATEMENT
 Goes straight back to the start of a do, while or for loop
 EXAMPLE:
#include<iostream.h>
int main(){
int n;
for(n=10;n>0;n--){
if(n==5)continue;
cout<<n<<“,”;}
cout << “FIRE!“;
return 0;}
O/P: 10,9,8,7,6,4,3,2,1,FIRE!
Ad

More Related Content

Similar to Data types, operators and control structures unit-2.pdf (20)

Fundamentals of C Programming Language
Fundamentals of C Programming LanguageFundamentals of C Programming Language
Fundamentals of C Programming Language
RamaBoya2
 
2nd PUC Computer science chapter 5 review of c++
2nd PUC Computer science chapter 5   review of c++2nd PUC Computer science chapter 5   review of c++
2nd PUC Computer science chapter 5 review of c++
Aahwini Esware gowda
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++
Bussines man badhrinadh
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++
K Durga Prasad
 
12 computer science_notes_ch01_overview_of_cpp
12 computer science_notes_ch01_overview_of_cpp12 computer science_notes_ch01_overview_of_cpp
12 computer science_notes_ch01_overview_of_cpp
sharvivek
 
component of c language.pptx
component of c language.pptxcomponent of c language.pptx
component of c language.pptx
AnisZahirahAzman
 
C intro
C introC intro
C intro
SHIKHA GAUTAM
 
Getting started with c++.pptx
Getting started with c++.pptxGetting started with c++.pptx
Getting started with c++.pptx
Akash Baruah
 
C presentation! BATRA COMPUTER CENTRE
C presentation! BATRA  COMPUTER  CENTRE C presentation! BATRA  COMPUTER  CENTRE
C presentation! BATRA COMPUTER CENTRE
jatin batra
 
java or oops class not in kerala polytechnic 4rth semester nots j
java or oops class not in kerala polytechnic  4rth semester nots jjava or oops class not in kerala polytechnic  4rth semester nots j
java or oops class not in kerala polytechnic 4rth semester nots j
ishorishore
 
Getting Started with C++
Getting Started with C++Getting Started with C++
Getting Started with C++
Praveen M Jigajinni
 
C program
C programC program
C program
AJAL A J
 
Introduction to C++.pptx learn c++ and basic concepts of OOP
Introduction to C++.pptx learn c++ and basic concepts of OOPIntroduction to C++.pptx learn c++ and basic concepts of OOP
Introduction to C++.pptx learn c++ and basic concepts of OOP
UbaidKhan930128
 
INTRODUCTION TO C++.pptx
INTRODUCTION TO C++.pptxINTRODUCTION TO C++.pptx
INTRODUCTION TO C++.pptx
MamataAnilgod
 
Ch02
Ch02Ch02
Ch02
Arriz San Juan
 
Basics of C.ppt
Basics of C.pptBasics of C.ppt
Basics of C.ppt
Ashwini Rao
 
Basics of c
Basics of cBasics of c
Basics of c
vinothini1996
 
C_Programming_Language_tutorial__Autosaved_.pptx
C_Programming_Language_tutorial__Autosaved_.pptxC_Programming_Language_tutorial__Autosaved_.pptx
C_Programming_Language_tutorial__Autosaved_.pptx
Likhil181
 
C introduction
C introductionC introduction
C introduction
AswathyBAnil
 
Fundamentals of c language
Fundamentals of c languageFundamentals of c language
Fundamentals of c language
AkshhayPatel
 
Fundamentals of C Programming Language
Fundamentals of C Programming LanguageFundamentals of C Programming Language
Fundamentals of C Programming Language
RamaBoya2
 
2nd PUC Computer science chapter 5 review of c++
2nd PUC Computer science chapter 5   review of c++2nd PUC Computer science chapter 5   review of c++
2nd PUC Computer science chapter 5 review of c++
Aahwini Esware gowda
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++
K Durga Prasad
 
12 computer science_notes_ch01_overview_of_cpp
12 computer science_notes_ch01_overview_of_cpp12 computer science_notes_ch01_overview_of_cpp
12 computer science_notes_ch01_overview_of_cpp
sharvivek
 
component of c language.pptx
component of c language.pptxcomponent of c language.pptx
component of c language.pptx
AnisZahirahAzman
 
Getting started with c++.pptx
Getting started with c++.pptxGetting started with c++.pptx
Getting started with c++.pptx
Akash Baruah
 
C presentation! BATRA COMPUTER CENTRE
C presentation! BATRA  COMPUTER  CENTRE C presentation! BATRA  COMPUTER  CENTRE
C presentation! BATRA COMPUTER CENTRE
jatin batra
 
java or oops class not in kerala polytechnic 4rth semester nots j
java or oops class not in kerala polytechnic  4rth semester nots jjava or oops class not in kerala polytechnic  4rth semester nots j
java or oops class not in kerala polytechnic 4rth semester nots j
ishorishore
 
Introduction to C++.pptx learn c++ and basic concepts of OOP
Introduction to C++.pptx learn c++ and basic concepts of OOPIntroduction to C++.pptx learn c++ and basic concepts of OOP
Introduction to C++.pptx learn c++ and basic concepts of OOP
UbaidKhan930128
 
INTRODUCTION TO C++.pptx
INTRODUCTION TO C++.pptxINTRODUCTION TO C++.pptx
INTRODUCTION TO C++.pptx
MamataAnilgod
 
C_Programming_Language_tutorial__Autosaved_.pptx
C_Programming_Language_tutorial__Autosaved_.pptxC_Programming_Language_tutorial__Autosaved_.pptx
C_Programming_Language_tutorial__Autosaved_.pptx
Likhil181
 
Fundamentals of c language
Fundamentals of c languageFundamentals of c language
Fundamentals of c language
AkshhayPatel
 

More from gurpreetk8199 (7)

ch11 Data Link Control.ppttttttt data link
ch11 Data Link Control.ppttttttt data linkch11 Data Link Control.ppttttttt data link
ch11 Data Link Control.ppttttttt data link
gurpreetk8199
 
ch10 Error Detection and Correction.pptttt
ch10 Error Detection and Correction.ppttttch10 Error Detection and Correction.pptttt
ch10 Error Detection and Correction.pptttt
gurpreetk8199
 
ch07 transmission media.pptxxxxxxxxxxxxxx
ch07 transmission media.pptxxxxxxxxxxxxxxch07 transmission media.pptxxxxxxxxxxxxxx
ch07 transmission media.pptxxxxxxxxxxxxxx
gurpreetk8199
 
ch08 switching.pptxxxxxxxxxxxxxxxxxxxxxxxx
ch08 switching.pptxxxxxxxxxxxxxxxxxxxxxxxxch08 switching.pptxxxxxxxxxxxxxxxxxxxxxxxx
ch08 switching.pptxxxxxxxxxxxxxxxxxxxxxxxx
gurpreetk8199
 
classes and objects.pdfggggggggffffffffgggf
classes and objects.pdfggggggggffffffffgggfclasses and objects.pdfggggggggffffffffgggf
classes and objects.pdfggggggggffffffffgggf
gurpreetk8199
 
lecture 3 HVPE.pptxhbjhhhhhhhhhhhhhhhhhhhh
lecture 3 HVPE.pptxhbjhhhhhhhhhhhhhhhhhhhhlecture 3 HVPE.pptxhbjhhhhhhhhhhhhhhhhhhhh
lecture 3 HVPE.pptxhbjhhhhhhhhhhhhhhhhhhhh
gurpreetk8199
 
Project_monitoring&control.pptxbbbbbbbbbbb
Project_monitoring&control.pptxbbbbbbbbbbbProject_monitoring&control.pptxbbbbbbbbbbb
Project_monitoring&control.pptxbbbbbbbbbbb
gurpreetk8199
 
ch11 Data Link Control.ppttttttt data link
ch11 Data Link Control.ppttttttt data linkch11 Data Link Control.ppttttttt data link
ch11 Data Link Control.ppttttttt data link
gurpreetk8199
 
ch10 Error Detection and Correction.pptttt
ch10 Error Detection and Correction.ppttttch10 Error Detection and Correction.pptttt
ch10 Error Detection and Correction.pptttt
gurpreetk8199
 
ch07 transmission media.pptxxxxxxxxxxxxxx
ch07 transmission media.pptxxxxxxxxxxxxxxch07 transmission media.pptxxxxxxxxxxxxxx
ch07 transmission media.pptxxxxxxxxxxxxxx
gurpreetk8199
 
ch08 switching.pptxxxxxxxxxxxxxxxxxxxxxxxx
ch08 switching.pptxxxxxxxxxxxxxxxxxxxxxxxxch08 switching.pptxxxxxxxxxxxxxxxxxxxxxxxx
ch08 switching.pptxxxxxxxxxxxxxxxxxxxxxxxx
gurpreetk8199
 
classes and objects.pdfggggggggffffffffgggf
classes and objects.pdfggggggggffffffffgggfclasses and objects.pdfggggggggffffffffgggf
classes and objects.pdfggggggggffffffffgggf
gurpreetk8199
 
lecture 3 HVPE.pptxhbjhhhhhhhhhhhhhhhhhhhh
lecture 3 HVPE.pptxhbjhhhhhhhhhhhhhhhhhhhhlecture 3 HVPE.pptxhbjhhhhhhhhhhhhhhhhhhhh
lecture 3 HVPE.pptxhbjhhhhhhhhhhhhhhhhhhhh
gurpreetk8199
 
Project_monitoring&control.pptxbbbbbbbbbbb
Project_monitoring&control.pptxbbbbbbbbbbbProject_monitoring&control.pptxbbbbbbbbbbb
Project_monitoring&control.pptxbbbbbbbbbbb
gurpreetk8199
 
Ad

Recently uploaded (20)

YSPH VMOC Special Report - Measles Outbreak Southwest US 5-14-2025 .pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 5-14-2025  .pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 5-14-2025  .pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-14-2025 .pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
COPA Apprentice exam Questions and answers PDF
COPA Apprentice exam Questions and answers PDFCOPA Apprentice exam Questions and answers PDF
COPA Apprentice exam Questions and answers PDF
SONU HEETSON
 
Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)
Mohamed Rizk Khodair
 
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Leonel Morgado
 
Botany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic ExcellenceBotany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic Excellence
online college homework help
 
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
Dr. Nasir Mustafa
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
Nguyen Thanh Tu Collection
 
puzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tensepuzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tense
OlgaLeonorTorresSnch
 
How to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo SlidesHow to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo Slides
Celine George
 
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptxTERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
PoojaSen20
 
Pope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptxPope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptx
Martin M Flynn
 
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptxANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
Mayuri Chavan
 
Look Up, Look Down: Spotting Local History Everywhere
Look Up, Look Down: Spotting Local History EverywhereLook Up, Look Down: Spotting Local History Everywhere
Look Up, Look Down: Spotting Local History Everywhere
History of Stoke Newington
 
Final Evaluation.docx...........................
Final Evaluation.docx...........................Final Evaluation.docx...........................
Final Evaluation.docx...........................
l1bbyburrell
 
Chemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptxChemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptx
Mayuri Chavan
 
2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx
mansk2
 
IPL QUIZ | THE QUIZ CLUB OF PSGCAS | 2025.pdf
IPL QUIZ | THE QUIZ CLUB OF PSGCAS | 2025.pdfIPL QUIZ | THE QUIZ CLUB OF PSGCAS | 2025.pdf
IPL QUIZ | THE QUIZ CLUB OF PSGCAS | 2025.pdf
Quiz Club of PSG College of Arts & Science
 
How to Configure Extra Steps During Checkout in Odoo 18 Website
How to Configure Extra Steps During Checkout in Odoo 18 WebsiteHow to Configure Extra Steps During Checkout in Odoo 18 Website
How to Configure Extra Steps During Checkout in Odoo 18 Website
Celine George
 
How to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 PurchaseHow to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 Purchase
Celine George
 
How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18
Celine George
 
COPA Apprentice exam Questions and answers PDF
COPA Apprentice exam Questions and answers PDFCOPA Apprentice exam Questions and answers PDF
COPA Apprentice exam Questions and answers PDF
SONU HEETSON
 
Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)
Mohamed Rizk Khodair
 
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Leonel Morgado
 
Botany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic ExcellenceBotany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic Excellence
online college homework help
 
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
Dr. Nasir Mustafa
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
Nguyen Thanh Tu Collection
 
puzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tensepuzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tense
OlgaLeonorTorresSnch
 
How to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo SlidesHow to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo Slides
Celine George
 
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptxTERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
PoojaSen20
 
Pope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptxPope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptx
Martin M Flynn
 
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptxANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
Mayuri Chavan
 
Look Up, Look Down: Spotting Local History Everywhere
Look Up, Look Down: Spotting Local History EverywhereLook Up, Look Down: Spotting Local History Everywhere
Look Up, Look Down: Spotting Local History Everywhere
History of Stoke Newington
 
Final Evaluation.docx...........................
Final Evaluation.docx...........................Final Evaluation.docx...........................
Final Evaluation.docx...........................
l1bbyburrell
 
Chemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptxChemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptx
Mayuri Chavan
 
2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx
mansk2
 
How to Configure Extra Steps During Checkout in Odoo 18 Website
How to Configure Extra Steps During Checkout in Odoo 18 WebsiteHow to Configure Extra Steps During Checkout in Odoo 18 Website
How to Configure Extra Steps During Checkout in Odoo 18 Website
Celine George
 
How to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 PurchaseHow to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 Purchase
Celine George
 
How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18
Celine George
 
Ad

Data types, operators and control structures unit-2.pdf

  • 1. UNIT-2 DATA TYPES, OPERATORS, AND CONTROL STRUCTURES FACULTY NAME: Er. Kamaldeep Kaur
  • 2. CHAPTER CONTENTS  BASIC DATA TYPES, DERIVED DATA TYPES,  KEYWORDS, IDENTIFIERS, CONSTANTS AND VARIABLES,  TYPE CASTING,  OPERATORS, AND OPERATOR PRECEDENCE.  CONTROL STRUCTURES: IF STATEMENT, SWITCH-CASE, FOR, WHILE AND DO-WHILE LOOPS, BREAK AND CONTINUE STATEMENT
  • 3. INTRODUCTION  C++ was developed by Bjarne Stroustrup, in early 1980s.  C++ is an extension of C with a major addition of the class construct feature.  Stroustrup initially called the new language as ‘C with classes’.  The name was later changed to C++, from the increment operator ++, suggesting that C++ is an incremented version of C.
  • 4. C++ BASIC ELEMENTS  Programming language is a set of rules, symbols, and special words used to construct programs.  Character Set: a set of valid characters that a language can recognize.  Letters: A-Z, a-z  Digits: 0-9  Special Characters Space + * / ^ () [] {} = != <> ‘ “ $ , ; : % ! & ? _ # <= >= @  Formatting characters backspace, horizontal tab, vertical tab, and carriage return
  • 5. TOKENS  Tokens are the basic building blocks in C++ language.  The smallest individual units in a program are known as tokens.  A program is constructed using a combination of these tokens.
  • 6. TYPES OF TOKENS  KEYWORDS  IDENTIFIERS  CONSTANTS  STRING  SPECIAL SYMBOLS (PUNCTUATORS)  OPERATORS
  • 7. KEYWORDS  Explicitly reserved identifiers  Cannot be used as names for the program variables or other user-defined program elements  There are 32 keywords, that were present in C language and have been carried over into C++ :  auto const double float int short struct unsigned break continue else for long signed switch void case default enum goto register sizeof typedef volatile char do extern if return static union while
  • 8. KEYWORDS  There are another 30 reserved words that were not in C, are therefore new to C++, and here they are:  asm dynamic_cast namespace reinterpret_cast try bool explicit new static_cast typeid catch false operator template typename class friend private this using const_cast inline public throw virtual delete mutable protected true wchar_t
  • 9. IDENTIFIERS  Identifiers refers to the name of variables, functions, arrays, classes, etc. created by the user. Identifiers are the fundamental requirement of any language.  Identifier naming conventions  Only alphabetic characters, digits and underscores are permitted.  First letter must be an alphabet or underscore (_).  Identifiers are case sensitive.  Reserved keywords can not be used as an identifier's name.
  • 10. IDENTIFIERS IDENTIFIER NAME VALID OR INVALID 5th_element Invalid (begins with a digit) _delete Valid School.fee Invalid (dot not allowed) register[5] Invalid (keyword) Student[10] Valid Employee name Invalid (white space) Perimeter() Valid
  • 11. IDENTIFIERS  A major difference between C and C++ is the limit on the length of a name.  C recognizes only the first 32 characters in a name  C++ places no limit on its length, and, therefore, all the characters in a name are significant.
  • 12. CONSTANTS  Refer to fixed values that do not change during the execution of a program.  Also known as literals.  Types of literals in C++:  Integer constant  Character constant  Floating (real) constant  String constant
  • 13. CONSTANTS  EXAMPLES  123 //integer constant  12.34 //floating point constant  ‘A’ //character constant  “C++” //string constant
  • 16.  ** void is used:  To specify the return type of a function when it is not returning any value  To indicate an empty argument list to a function.
  • 17. STRING  used to store letters and digits.  referred to as an array of characters as well as an individual data type.  enclosed within double quotes, unlike characters which are stored within single quotes.  The termination of a string in C++ is represented by the null character, that is, ‘0’  Examples:  char name[30] = ‘’Hello!”;  char name[] = “Hello!”;  char name[30] = { ‘H’ , ’e’ , ’l’ , ’l’ , ’o’};;  string name = “Hello”
  • 18. SPECIAL SYMBOLS (PUNCTUATORS) Special Character Name Function [ ] Square brackets The opening and closing brackets of an array symbolize single and multidimensional subscripts. () Simple brackets The opening and closing brackets represent function declaration and calls, used in print statements. { } Curly braces The opening and closing curly brackets to denote the start and end of a particular fragment of code which may be functions, loops or conditional statements , Comma We use commas to separate more than one statements, like in the declaration of different variable names # Hash / Pound / Preprocessor The hash symbol represents a preprocessor directive used for denoting the use of a header file * Asterisk We use the asterisk symbol in various respects such as to declare pointers, used as an operand for multiplication ~ Tilde We use the tilde symbol as a destructor to free memory . Period / dot The use the dot operator to access a member of a structure
  • 19. OPERATORS  Operators are tools or symbols which are used to perform a specific operation on data.  Operations are performed on operands.  Operators can be classified into three broad categories according to the number of operands used:  Unary operators  Binary operators  Ternary operators
  • 20. UNARY OPERATORS  The operators that operate a single operand to form an expression are known as unary operators.  Examples:  + + (increment) operator,  -- (decrement) operator,  unary minus operator.
  • 21. BINARY OPERATORS  The operators that operate on two or more operands are known as binary operators.  Examples:  Arithmetic Operators  Assignment Operator  Relational Operators  Logical Operators  E.g. a+b, a-b, a*b, a/b etc.
  • 22. TERNARY OPERATORS  The operator that operates minimum or maximum three operands is known as ternary operator.  There is only one ternary operator available in C++.  The operator ?: is the only available ternary operator i.e. used as a substitute of if-else statement.• E.g. a>b ? a:b
  • 23. OPERATORS BASED ON TYPE OF OPERATION  Arithmetic Operators  Assignment Operators  Relational Operators  Logical Operators  Increment Decrement Operators  Conditional Operator
  • 24. ARITHMETIC OPERATORS  An operator that performs an arithmetic operation is known as arithmetic operator  It is a binary operator
  • 27. RELATIONAL OPERATORS  An operator that is used to test the relation between two values or operands.  Relational Operators are BINARY operators.  These operators evaluate to true or false.  Return zero for false and a non zero for true.  Also known as Comparison operators.
  • 29. LOGICAL OPERATORS  The operators that are used to combine the result of one or more relation.  These operators evaluate to true or false.  Are Unary and Binary Operators.
  • 31. INCREMENT & DECREMENT OPERATORS  ++ Increments value by 1  x++ / ++x Equivalent to X=X+1  -- Decrement value by 1  x-- / --x Equivalent to X=X-1  ++x and --y are called prefix operator, that means the increment or decrement operators modify their operand before it is used or assigned.  Example a = 10; b = ++a; a is incremented by 1 and then assign to b. Therefore both a and b have the value of 11.  x++ and y-- are called postfix operator, the increment or decrement operators modify their operand after it is used. a = 10; b = a++; value of a is assigned to b then a is incremented by 1. Therefore here a=11 and b=10.
  • 34. OPERATOR PRECEDENCE & ASSOCIATIVITY  Operator precedence determines the grouping of terms in an expression.  The associativity of an operator is a property that determines how operators of the same precedence are grouped in the absence of parentheses. This affects how an expression is evaluated.
  • 36. TYPE CONVERSION  It is the process of converting one data type to another i.e. converting an expression of given type into another  Two types:  Implicit (automatic)  Explicit (type casting)
  • 37. AUTOMATIC TYPE CONVERSION  It is automatically performed when: 1. Constants and variables of different types are mixed in an expression  For a binary operator, if the operands type differ, the compiler converts one of them to match with the other, using the rule that smaller type is converted to the wider type.  For example, if one of the operand is int and other is float, then int is converted to float because a float is wider than int.  For example, m = 5 + 2.75  ‘5’ would be converted to float as 5.00
  • 38. AUTOMATIC TYPE CONVERSION 2. An assignment operator also causes automatic type conversion.  The type of data to the right of an assignment operator is automatically converted to the type of the variable on the left.  For example, int m; float x=3.14459; m=x;  The fractional part is truncated.  The type conversions are automatic with built in data types.
  • 39. TYPE CASTING  The explicit type conversion is done using type cast operator.  Syntax: datatype(expression)  datatype is the type which we want the expression to get changed as  For example, int a; float b,c; cin>>a>>b; c= float(a)+b; cout<<c;  O/P: 10 //a 12.5 //b 22.5 //c
  • 40. CONTROL STRUCTURES  Programs are written using four basic structures  Sequence: a sequence is a series of statements that execute one after another  Selection(branching): selection (branch) is used to execute different statements depending on certain conditions  Repetition(loop or iteration): repetition (looping) is used to repeat statements while certain conditions are met  Jumping: jump from one point of program to another  Called control structures or logic structures
  • 41. SEQUENCE CONTROL STRUCTURE  The sequence structure directs the computer to process the program instructions, one after another, in the order listed in the program.
  • 42. SELECTION CONTROL STRUCTURE (BRANCHING)  makes a decision and then takes an appropriate action based on that decision  Also called the decision structure
  • 43. SELECTION (BRANCHING OR CONDITIONAL)  If  if-else  nested if (if- elseif- elseif- else)  switch
  • 44. Simple If STATEMENT  if(condition)statement  If the condition is true, statement is executed.  If the condition is false, statement is not executed.  EXAMPLE: if (x==100) cout<<“x is 100”;
  • 46. If else STATEMENT  if (condition)statement1 else statement2  If condition is true, statement1 is executed and if the condition is false, statement2 is executed  EXAMPLE: if (x==100) cout<<“x is 100”; else cout<<“x is not 100”;
  • 48. Nested if STATEMENT  When if statement occurs with in another if statement, then such type of if statement is called nested if statement.  if (condition1) if (condition2) statement-1; else statement-2; else statement-3;
  • 50. Nested if STATEMENT  EXAMPLE: if(a>=b && a>=c) cout<<“a is biggest”; elseif(b>=a &&b>=c) cout<<“b is biggest”; else cout<<“c is biggest”;
  • 53. Switch STATEMENT  EXAMPLE: int main(){ int a; cin>>a; switch(a) { case 1: cout<<“the value is 1”; break; case 2: cout<<“the value is 2”; break; case 3: cout<<“the value is 3”; break; default: cout<<“out of range”; } return 0; }
  • 55. REPETITION (LOOP OR ITERATION)  directs computer to repeat one or more instructions until some condition is met  Also called a loop or iteration
  • 56. REPETITION (LOOP OR ITERATION)  While  do-while  For  Nesting
  • 57. LOOPS (ITERATION)  Loops have an objective to repeat a certain number of times or while a condition is fulfilled. When a single statement or a group of statements will be executed again and again in the program then such type of processing is called loop. Loop is divided into two parts  Body of loop  Control of loop
  • 58. CONTROL OF LOOP  Control of loop is divided into two parts:  Entry control loop- in this first of all condition is checked if it is true then body of the loop is executed. Otherwise we can exit from the loop when the condition becomes false. Entry control loop is also called base loop. Example- While loop and for loop  Exit control loop- in this first body is executed and then condition is checked. If condition is true, then again body of loop is executed. If condition is false, then control will move out from the loop. Exit control loop is also called Derived loop. Example- Do-while
  • 59. LOOP STATEMENTS  The loop statements in C++ language are-  While loop  Do loop/Do-while loop  For loop  Nested for loop
  • 62. WHILE LOOP EXAMPLE  #include<iostream.h> int main() { int n; cout<<“Enter the starting number”; cin>>n; while(n>0){ cout<<n<<“,”; n--; } cout << “FIRE!“; return 0; }
  • 66. WHILE VS DO WHILE ENTRY CONTROLLED EXIT CONTROLLED
  • 67. FOR LOOP  For loop is an Entry control loop when action is to be repeated for a predetermined number of times.  Four parts  Initialise expression  Test expression (condition)  Body (loop statements)  Increment/ decrement (modification)
  • 70. NESTING OF FOR LOOPS  A loop can be inside another loop.  C++ can have 256 levels of nesting.  for(init;condition;increment) { for(init;condition;increment) } statement(s); } statement(s); }
  • 71. JUMPING STATEMENTS  Jump statements are used to alter the flow of control unconditionally.  That is, jump statements transfer the program control within a function unconditionally.  The jump statements defined in C++ are break, continue, goto.
  • 72. Goto STATEMENT  It allows making an absolute jump to another point in the program.
  • 73. Goto STATEMENT  #include<iostream.h> int main() { int n=10; loop: cout<<n<<“,”; n--; if(n>0) goto loop; cout << “FIRE!“; return 0; }
  • 74. BREAK STATEMENT  Goes straight to the end of a do, while or for loop or a switch statement block.  EXAMPLE: #include<iostream.h> int main(){ int n; for(n=10;n>0;n--){ cout<<n<<“,”; if(n==5){ cout<<“count down aborted!”; break;}} return 0;} O/P: 10,9,8,7,6,5,count down aborted!
  • 75. CONTINUE STATEMENT  Goes straight back to the start of a do, while or for loop  EXAMPLE: #include<iostream.h> int main(){ int n; for(n=10;n>0;n--){ if(n==5)continue; cout<<n<<“,”;} cout << “FIRE!“; return 0;} O/P: 10,9,8,7,6,4,3,2,1,FIRE!
  翻译: