SlideShare a Scribd company logo
C++ Programming
Logical Opperators, Pointers and arrays
Mohammed Jehan
Lecturer
Logical Operators
Use logical operators to combine conditional statements and return true or false.
The AND operator works the following way:
In the AND operator,
both operands must
be true for the entire
expression to be true.
The AND Operator
For example:
int age = 20;
if (age > 16 && age < 60) {
cout << "Accepted!" << endl;
}
In the example above, the logical AND operator was used to combine both expressions.
The expression in the if statement evaluates to true only if both expressions are true.
The AND Operator
Within a single if statement, logical operators can be used to combine multiple conditions.
int age = 20;
int grade = 80;
if (age > 16 && age < 60 && grade > 50) {
cout << "Accepted!" << endl;
}
The OR Operator
The OR (||) operator returns true if any one of its operands is true.
Example:
int age = 16;
int score = 90;
if (age > 20 || score
> 50) {
cout << "Accepted!"
<< endl;
}
You can combine any number of logical OR statements you want.
In addition, multiple OR and AND statements may be chained together.
Logical NOT
The logical NOT (!) operator works with just a single operand, reversing its logical state. Thus, if a
condition is true, the NOT operator makes it false, and vice versa.
int age = 10;
if ( !(age > 16) ) {
cout << "Your age is less than 16" << endl;
}
// Outputs "Your age is less than 16"
Be careful
using this,
because !false
means true.
while vs. do...while
Try the following example to understand all the logical operators
#include <iostream>
using namespace std;
main() {
int a = 5;
int b = 20;
int c ;
if(a && b) {
cout << "Line 1 - Condition is
true"<< endl ;
}
if(a || b) {
cout << "Line 2 - Condition is
true"<< endl ;
}
/* Let's change the values of a and b */
a = 0;
b = 10;
if(a && b) {
cout << "Line 3 - Condition is
true"<< endl ;
} else {
cout << "Line 4 - Condition is
not true"<< endl ;
}
if(!(a && b)) {
cout << "Line 5 - Condition is
true"<< endl ;
}
return 0;

More Related Content

PPT
PPTX
ppt on logical/arthimatical/conditional operators
PPTX
Operators in c++
PPTX
Lecture 2
PPT
PPT
Lecture 3
PPT
Control All
PPTX
Ref Lec 4- Conditional Statement (1).pptx
ppt on logical/arthimatical/conditional operators
Operators in c++
Lecture 2
Lecture 3
Control All
Ref Lec 4- Conditional Statement (1).pptx

Similar to Lecture 6 (20)

PPTX
Lecture 4
PPTX
Introduction& Overview-to-C++_programming.pptx
PPTX
Introduction to C++ programming language
PPTX
lesson 2.pptx
PPTX
PPTX
10-Lec - Nested IF Statement.pptx
PPTX
Pseudocode
PDF
Loops and conditional statements
PPTX
Chapter 3:Programming with Java Operators and Strings
PPTX
12-Lec - Repetition For Loop.pptx
PDF
OIT 116 LOOPS AND CONDITION STATEMENTS.pdf
PPTX
Lec7 - Loops updated.pptx
PPTX
Chapter 3 : Programming with Java Operators and Strings
PPT
PDF
Java unit 3
PPTX
Introduction to JavaScript Scripting Language
PDF
ICP - Lecture 9
PPTX
Lecture 5
PPTX
LOOPS AND DECISIONS
PPTX
05. Conditional Statements
Lecture 4
Introduction& Overview-to-C++_programming.pptx
Introduction to C++ programming language
lesson 2.pptx
10-Lec - Nested IF Statement.pptx
Pseudocode
Loops and conditional statements
Chapter 3:Programming with Java Operators and Strings
12-Lec - Repetition For Loop.pptx
OIT 116 LOOPS AND CONDITION STATEMENTS.pdf
Lec7 - Loops updated.pptx
Chapter 3 : Programming with Java Operators and Strings
Java unit 3
Introduction to JavaScript Scripting Language
ICP - Lecture 9
Lecture 5
LOOPS AND DECISIONS
05. Conditional Statements
Ad

More from Mohammed Khan (15)

PPTX
Lecture 9
PPTX
Lecture 7
PPTX
Lecture 3
PPTX
Lecture 1
PPTX
Module 11 therapeutic intervention play yoga
PPTX
Module 10 principles of learning, practice, reinforcement understanding spec...
PPTX
Module 9 speci ed
PPTX
Module 8 spec ed
PPTX
Module 7 special ed
PPTX
Module 6 spec ed
PPTX
Module 5 special ed
PPTX
Module 4 spec needs
PPTX
Module 3 special education
PPTX
Module 2 special ed
PPTX
Module 1 special ed
Lecture 9
Lecture 7
Lecture 3
Lecture 1
Module 11 therapeutic intervention play yoga
Module 10 principles of learning, practice, reinforcement understanding spec...
Module 9 speci ed
Module 8 spec ed
Module 7 special ed
Module 6 spec ed
Module 5 special ed
Module 4 spec needs
Module 3 special education
Module 2 special ed
Module 1 special ed
Ad

Recently uploaded (20)

PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 1)
PDF
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
PDF
IGGE1 Understanding the Self1234567891011
PDF
Practical Manual AGRO-233 Principles and Practices of Natural Farming
PDF
HVAC Specification 2024 according to central public works department
PPTX
Introduction to pro and eukaryotes and differences.pptx
PPTX
Unit 4 Computer Architecture Multicore Processor.pptx
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PDF
Trump Administration's workforce development strategy
PPTX
Introduction to Building Materials
PPTX
Share_Module_2_Power_conflict_and_negotiation.pptx
PDF
Indian roads congress 037 - 2012 Flexible pavement
PDF
advance database management system book.pdf
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
My India Quiz Book_20210205121199924.pdf
PPTX
B.Sc. DS Unit 2 Software Engineering.pptx
PPTX
TNA_Presentation-1-Final(SAVE)) (1).pptx
PDF
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Virtual and Augmented Reality in Current Scenario
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 1)
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
IGGE1 Understanding the Self1234567891011
Practical Manual AGRO-233 Principles and Practices of Natural Farming
HVAC Specification 2024 according to central public works department
Introduction to pro and eukaryotes and differences.pptx
Unit 4 Computer Architecture Multicore Processor.pptx
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
Trump Administration's workforce development strategy
Introduction to Building Materials
Share_Module_2_Power_conflict_and_negotiation.pptx
Indian roads congress 037 - 2012 Flexible pavement
advance database management system book.pdf
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
My India Quiz Book_20210205121199924.pdf
B.Sc. DS Unit 2 Software Engineering.pptx
TNA_Presentation-1-Final(SAVE)) (1).pptx
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Virtual and Augmented Reality in Current Scenario

Lecture 6

  • 1. C++ Programming Logical Opperators, Pointers and arrays Mohammed Jehan Lecturer
  • 2. Logical Operators Use logical operators to combine conditional statements and return true or false. The AND operator works the following way: In the AND operator, both operands must be true for the entire expression to be true.
  • 3. The AND Operator For example: int age = 20; if (age > 16 && age < 60) { cout << "Accepted!" << endl; } In the example above, the logical AND operator was used to combine both expressions. The expression in the if statement evaluates to true only if both expressions are true.
  • 4. The AND Operator Within a single if statement, logical operators can be used to combine multiple conditions. int age = 20; int grade = 80; if (age > 16 && age < 60 && grade > 50) { cout << "Accepted!" << endl; }
  • 5. The OR Operator The OR (||) operator returns true if any one of its operands is true. Example: int age = 16; int score = 90; if (age > 20 || score > 50) { cout << "Accepted!" << endl; } You can combine any number of logical OR statements you want. In addition, multiple OR and AND statements may be chained together.
  • 6. Logical NOT The logical NOT (!) operator works with just a single operand, reversing its logical state. Thus, if a condition is true, the NOT operator makes it false, and vice versa. int age = 10; if ( !(age > 16) ) { cout << "Your age is less than 16" << endl; } // Outputs "Your age is less than 16" Be careful using this, because !false means true.
  • 7. while vs. do...while Try the following example to understand all the logical operators #include <iostream> using namespace std; main() { int a = 5; int b = 20; int c ; if(a && b) { cout << "Line 1 - Condition is true"<< endl ; } if(a || b) { cout << "Line 2 - Condition is true"<< endl ; } /* Let's change the values of a and b */ a = 0; b = 10; if(a && b) { cout << "Line 3 - Condition is true"<< endl ; } else { cout << "Line 4 - Condition is not true"<< endl ; } if(!(a && b)) { cout << "Line 5 - Condition is true"<< endl ; } return 0;