SlideShare a Scribd company logo
1 of 16
If Statements &
Relational
Operators
Programming
ndamentals I: If Statements & Relational Operators /Slide 2
Conditional Statements
A conditional statement allows us to control
whether a program segment is executed or
not.
Two constructs
if statement
– if
– if-else
– if-else-if
switch statement
ndamentals I: If Statements & Relational Operators /Slide 3
The Basic if Statement
Syntax
if(condition)
action
if the condition is true then
execute the action.
action is either a single
statement or a group of
statements within braces.
condition
action
true
false
ndamentals I: If Statements & Relational Operators /Slide 4
Choice (if)
Put multiple action statements
within braces
if (it's raining){
<take umbrella>
<wear raincoat>
}
ndamentals I: If Statements & Relational Operators /Slide 5
Absolute Value
// program to read number & print its absolute value
#include <iostream>
using namespace std;
int main(){
int value;
cout << "Enter integer: ";
cin >> value;
if(value < 0)
value = -value;
cout << "The absolute value is " << value << endl;
return 0;
}
ndamentals I: If Statements & Relational Operators /Slide 6
Relational Operators
Relational operators are used to compare two values to
form a condition.
Math C++ Plain English
= == equals [example: if(a==b) ]
[ (a=b) means put the value of b into a ]
< < less than
≤ <= less than or equal to
> > greater than
≥ >= greater than or equal to
≠ != not equal to
ndamentals I: If Statements & Relational Operators /Slide 7
Conditions
Examples:
Number_of_Students < 200
10 > 20
20 * j == 10 + i
ndamentals I: If Statements & Relational Operators /Slide 8
Operator Precedence
Which comes first?
* / %
+ -
< <= >= >
== !=
=
Answer:
ndamentals I: If Statements & Relational Operators /Slide 9
The Boolean Type
C++ contains a type named bool for conditions.
A condition can have one of two values:
true (corresponds to a non-zero value)
false (corresponds to zero value)
Boolean operators can be used to form more complex conditional
expressions.
The and operator is &&
The or operator is ||
The not operator is !
ndamentals I: If Statements & Relational Operators /Slide 10
The Boolean Type
Truth table for "&&" (AND):
Operand1 Operand2 Operand1 &&
Operand2
true true true
true false false
false true false
false false false
ndamentals I: If Statements & Relational Operators /Slide 11
The Boolean Type
Truth table for “||" (OR):
Operand1 Operand2 Operand1 ||
Operand2
true true true
true false true
false true true
false false false
ndamentals I: If Statements & Relational Operators /Slide 12
The Boolean Type
Truth table for "!" (NOT):
Operand !Operand
true false
false true
ndamentals I: If Statements & Relational Operators /Slide 13
A Boolean Type
Assignments to bool type variables
bool P = true;
bool Q = false;
bool R = true;
bool S = P && Q;
bool T = !Q || R;
bool U = !(R && !Q);
ndamentals I: If Statements & Relational Operators /Slide 14
More Operator Precedence
Precedence of operators (from highest to lowest)
Parentheses ( … )
Unary operators !
Multiplicative operators * / %
Additive operators + -
Relational ordering < <= >= >
Relational equality == !=
Logical and &&
Logical or ||
Assignment =
ndamentals I: If Statements & Relational Operators /Slide 15
More Operator Precedence
Examples
5 != 6 || 7 <= 3
(5 !=6) || (7 <= 3)
5 * 15 + 4 == 13 && 12 < 19 || !false == 5 < 24
ndamentals I: If Statements & Relational Operators /Slide 16
Sorting Two Numbers
int value1;
int value2;
int temp;
cout << "Enter two integers: ";
cin >> value1 >> value2;
if(value1 > value2){
temp = value1;
value1 = value2;
value2 = temp;
}
cout << "The input in sorted order: "
<< value1 << " " << value2 << endl;

More Related Content

What's hot

10. switch case
10. switch case10. switch case
10. switch caseWay2itech
 
Bt0067 c programming and data structures 1
Bt0067 c programming and data structures 1Bt0067 c programming and data structures 1
Bt0067 c programming and data structures 1Techglyphs
 
Logical and Conditional Operator In C language
Logical and Conditional Operator In C languageLogical and Conditional Operator In C language
Logical and Conditional Operator In C languageAbdul Rehman
 
Decision making using if statement
Decision making using if statementDecision making using if statement
Decision making using if statementCHANDAN KUMAR
 
Control Structures in Visual Basic
Control Structures in  Visual BasicControl Structures in  Visual Basic
Control Structures in Visual BasicTushar Jain
 
Control structures in Java
Control structures in JavaControl structures in Java
Control structures in JavaRavi_Kant_Sahu
 
If and select statement
If and select statementIf and select statement
If and select statementRahul Sharma
 
Conditional operators
Conditional operatorsConditional operators
Conditional operatorsBU
 
EXTERN -- wherever u define variables, it will get access to use them
EXTERN -- wherever u define variables, it will get access to use themEXTERN -- wherever u define variables, it will get access to use them
EXTERN -- wherever u define variables, it will get access to use themAjay Chimmani
 
Fcp chapter 2 upto_if_else (2)
Fcp chapter 2 upto_if_else (2)Fcp chapter 2 upto_if_else (2)
Fcp chapter 2 upto_if_else (2)Jagdish Kamble
 
Php-Continuation
Php-ContinuationPhp-Continuation
Php-Continuationlotlot
 
Types of operators in C
Types of operators in CTypes of operators in C
Types of operators in CPrabhu Govind
 
Switch statements in Java
Switch statements  in JavaSwitch statements  in Java
Switch statements in JavaJin Castor
 
Chapter 4.2
Chapter 4.2Chapter 4.2
Chapter 4.2sotlsoc
 
L hopitals rule
L hopitals ruleL hopitals rule
L hopitals rulejaflint718
 
CCv1.0 ppt02 - operators
CCv1.0 ppt02 - operatorsCCv1.0 ppt02 - operators
CCv1.0 ppt02 - operatorsappAcademy
 

What's hot (20)

10. switch case
10. switch case10. switch case
10. switch case
 
07 flow control
07   flow control07   flow control
07 flow control
 
Control Structures: Part 1
Control Structures: Part 1Control Structures: Part 1
Control Structures: Part 1
 
Bt0067 c programming and data structures 1
Bt0067 c programming and data structures 1Bt0067 c programming and data structures 1
Bt0067 c programming and data structures 1
 
Logical and Conditional Operator In C language
Logical and Conditional Operator In C languageLogical and Conditional Operator In C language
Logical and Conditional Operator In C language
 
Decision making using if statement
Decision making using if statementDecision making using if statement
Decision making using if statement
 
Control Structures in Visual Basic
Control Structures in  Visual BasicControl Structures in  Visual Basic
Control Structures in Visual Basic
 
Control structures in Java
Control structures in JavaControl structures in Java
Control structures in Java
 
If and select statement
If and select statementIf and select statement
If and select statement
 
C language assignment help
C language assignment helpC language assignment help
C language assignment help
 
Conditional operators
Conditional operatorsConditional operators
Conditional operators
 
EXTERN -- wherever u define variables, it will get access to use them
EXTERN -- wherever u define variables, it will get access to use themEXTERN -- wherever u define variables, it will get access to use them
EXTERN -- wherever u define variables, it will get access to use them
 
Fcp chapter 2 upto_if_else (2)
Fcp chapter 2 upto_if_else (2)Fcp chapter 2 upto_if_else (2)
Fcp chapter 2 upto_if_else (2)
 
Php-Continuation
Php-ContinuationPhp-Continuation
Php-Continuation
 
Types of operators in C
Types of operators in CTypes of operators in C
Types of operators in C
 
Switch statements in Java
Switch statements  in JavaSwitch statements  in Java
Switch statements in Java
 
Chapter 4.2
Chapter 4.2Chapter 4.2
Chapter 4.2
 
L hopitals rule
L hopitals ruleL hopitals rule
L hopitals rule
 
CCv1.0 ppt02 - operators
CCv1.0 ppt02 - operatorsCCv1.0 ppt02 - operators
CCv1.0 ppt02 - operators
 
Operator.ppt
Operator.pptOperator.ppt
Operator.ppt
 

Similar to If

10-Lec - Nested IF Statement.pptx
10-Lec - Nested IF Statement.pptx10-Lec - Nested IF Statement.pptx
10-Lec - Nested IF Statement.pptxAqeelAbbas94
 
Computer Programming - if Statements & Relational Operators
Computer Programming - if Statements  & Relational OperatorsComputer Programming - if Statements  & Relational Operators
Computer Programming - if Statements & Relational OperatorsJohn Paul Espino
 
Ap Power Point Chpt3 B
Ap Power Point Chpt3 BAp Power Point Chpt3 B
Ap Power Point Chpt3 Bdplunkett
 
Ap Power Point Chpt3
Ap Power Point Chpt3Ap Power Point Chpt3
Ap Power Point Chpt3dplunkett
 
C language UPTU Unit3 Slides
C language UPTU Unit3 SlidesC language UPTU Unit3 Slides
C language UPTU Unit3 SlidesRakesh Roshan
 
4. programing 101
4. programing 1014. programing 101
4. programing 101IEEE MIU SB
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionalish sha
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionalish sha
 
C statements.ppt presentation in c language
C statements.ppt presentation in c languageC statements.ppt presentation in c language
C statements.ppt presentation in c languagechintupro9
 
C operators
C operatorsC operators
C operatorsGPERI
 
The Three Basic Selection Structures in C++ Programming Concepts
The Three Basic Selection Structures in C++ Programming ConceptsThe Three Basic Selection Structures in C++ Programming Concepts
The Three Basic Selection Structures in C++ Programming ConceptsTech
 

Similar to If (20)

Ch04
Ch04Ch04
Ch04
 
10-Lec - Nested IF Statement.pptx
10-Lec - Nested IF Statement.pptx10-Lec - Nested IF Statement.pptx
10-Lec - Nested IF Statement.pptx
 
Computer Programming - if Statements & Relational Operators
Computer Programming - if Statements  & Relational OperatorsComputer Programming - if Statements  & Relational Operators
Computer Programming - if Statements & Relational Operators
 
Programming for Problem Solving
Programming for Problem SolvingProgramming for Problem Solving
Programming for Problem Solving
 
Ap Power Point Chpt3 B
Ap Power Point Chpt3 BAp Power Point Chpt3 B
Ap Power Point Chpt3 B
 
Ap Power Point Chpt3
Ap Power Point Chpt3Ap Power Point Chpt3
Ap Power Point Chpt3
 
Control statements in c
Control statements in cControl statements in c
Control statements in c
 
Ternary operator
Ternary operatorTernary operator
Ternary operator
 
C language UPTU Unit3 Slides
C language UPTU Unit3 SlidesC language UPTU Unit3 Slides
C language UPTU Unit3 Slides
 
Java unit 3
Java unit 3Java unit 3
Java unit 3
 
LOOPS AND DECISIONS
LOOPS AND DECISIONSLOOPS AND DECISIONS
LOOPS AND DECISIONS
 
If-else and switch-case
If-else and switch-caseIf-else and switch-case
If-else and switch-case
 
4. programing 101
4. programing 1014. programing 101
4. programing 101
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
 
C statements.ppt presentation in c language
C statements.ppt presentation in c languageC statements.ppt presentation in c language
C statements.ppt presentation in c language
 
C operators
C operatorsC operators
C operators
 
slides03.ppt
slides03.pptslides03.ppt
slides03.ppt
 
M C6java5
M C6java5M C6java5
M C6java5
 
The Three Basic Selection Structures in C++ Programming Concepts
The Three Basic Selection Structures in C++ Programming ConceptsThe Three Basic Selection Structures in C++ Programming Concepts
The Three Basic Selection Structures in C++ Programming Concepts
 

Recently uploaded

Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 

Recently uploaded (20)

Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 

If

  • 2. ndamentals I: If Statements & Relational Operators /Slide 2 Conditional Statements A conditional statement allows us to control whether a program segment is executed or not. Two constructs if statement – if – if-else – if-else-if switch statement
  • 3. ndamentals I: If Statements & Relational Operators /Slide 3 The Basic if Statement Syntax if(condition) action if the condition is true then execute the action. action is either a single statement or a group of statements within braces. condition action true false
  • 4. ndamentals I: If Statements & Relational Operators /Slide 4 Choice (if) Put multiple action statements within braces if (it's raining){ <take umbrella> <wear raincoat> }
  • 5. ndamentals I: If Statements & Relational Operators /Slide 5 Absolute Value // program to read number & print its absolute value #include <iostream> using namespace std; int main(){ int value; cout << "Enter integer: "; cin >> value; if(value < 0) value = -value; cout << "The absolute value is " << value << endl; return 0; }
  • 6. ndamentals I: If Statements & Relational Operators /Slide 6 Relational Operators Relational operators are used to compare two values to form a condition. Math C++ Plain English = == equals [example: if(a==b) ] [ (a=b) means put the value of b into a ] < < less than ≤ <= less than or equal to > > greater than ≥ >= greater than or equal to ≠ != not equal to
  • 7. ndamentals I: If Statements & Relational Operators /Slide 7 Conditions Examples: Number_of_Students < 200 10 > 20 20 * j == 10 + i
  • 8. ndamentals I: If Statements & Relational Operators /Slide 8 Operator Precedence Which comes first? * / % + - < <= >= > == != = Answer:
  • 9. ndamentals I: If Statements & Relational Operators /Slide 9 The Boolean Type C++ contains a type named bool for conditions. A condition can have one of two values: true (corresponds to a non-zero value) false (corresponds to zero value) Boolean operators can be used to form more complex conditional expressions. The and operator is && The or operator is || The not operator is !
  • 10. ndamentals I: If Statements & Relational Operators /Slide 10 The Boolean Type Truth table for "&&" (AND): Operand1 Operand2 Operand1 && Operand2 true true true true false false false true false false false false
  • 11. ndamentals I: If Statements & Relational Operators /Slide 11 The Boolean Type Truth table for “||" (OR): Operand1 Operand2 Operand1 || Operand2 true true true true false true false true true false false false
  • 12. ndamentals I: If Statements & Relational Operators /Slide 12 The Boolean Type Truth table for "!" (NOT): Operand !Operand true false false true
  • 13. ndamentals I: If Statements & Relational Operators /Slide 13 A Boolean Type Assignments to bool type variables bool P = true; bool Q = false; bool R = true; bool S = P && Q; bool T = !Q || R; bool U = !(R && !Q);
  • 14. ndamentals I: If Statements & Relational Operators /Slide 14 More Operator Precedence Precedence of operators (from highest to lowest) Parentheses ( … ) Unary operators ! Multiplicative operators * / % Additive operators + - Relational ordering < <= >= > Relational equality == != Logical and && Logical or || Assignment =
  • 15. ndamentals I: If Statements & Relational Operators /Slide 15 More Operator Precedence Examples 5 != 6 || 7 <= 3 (5 !=6) || (7 <= 3) 5 * 15 + 4 == 13 && 12 < 19 || !false == 5 < 24
  • 16. ndamentals I: If Statements & Relational Operators /Slide 16 Sorting Two Numbers int value1; int value2; int temp; cout << "Enter two integers: "; cin >> value1 >> value2; if(value1 > value2){ temp = value1; value1 = value2; value2 = temp; } cout << "The input in sorted order: " << value1 << " " << value2 << endl;