Operators for logical use – Computer Sir Ki Class

Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  

Login
[lwa]



Code Learning #CPP#1739 siteicon   siteicon   siteicon  

Operators for logical use

Testing the use of logical operators like not, and and or operators.

Learning Objectives

  • Learning of logical operators like not (!), and (&&) or (||) operator.

Source Code

TC++ #1739

 

Source Code

Run Output

! tested
&& tested
|| tested

Code Understanding

int a=10, b=0;
Initialisation of variables is being done so that logical conditions can be tested.

if(!b) cout<<“! tested”<<endl;
Here we are making not of b, which is true as b has been initialised to 0 so not of b would be non-zero, hence if condition will be true.

if(a && !b) cout<<“&& tested”<<endl;
Here a is being tested for true and !b is also being tested for true. Since both conditions are true so the required matter would be printed.

if(a || b) cout<<“|| tested”<<endl;
Here either a being true or b being true is being tested. If any one is true then result will be printed. Since in this case a is not zero, so it is true and b is 0 so it is not true. But one condition is true so whole expression will be treated as true and if condition will do its job of printing the subsequent expression.

Notes

  • Notice that ! operator works on one value only while && and || operators require two values on each side, Thus we can also call ! operator as unary logical operator and && and || binary logical operator


Suggested Filename(s): operator-logic.cpp, logical-op.cpp



Share

CSKC| Created: 16-Dec-2017 | Updated: 28-Aug-2018|






×
Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning 2D Array Memory Addressing Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Multi-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues


Back