← Back to C# Course | Chapter 3: Operators | Lesson 3 of 6

Logical Operators कैसे काम करते हैं

Logical operators true-or-false सवालों को combine करते हैं, जैसे 'क्या मेरे पास मेरा ticket है AND क्या gate खुला है?'। C# कई conditions से decisions लेने के लिए and, or, और not उपयोग करता है।

In this page:

  1. Logical Operators
Syntax
csharp
bool result = condition1 && condition2;   // AND
bool result = condition1 || condition2;   // OR
bool result = !condition;                 // NOT

Logical Operators

Logical operators boolean expressions को combine करते हैं: && (AND), || (OR), और ! (NOT)। && और || short-circuit करते हैं — अगर left side पहले ही result तय कर दे, तो right side कभी evaluate नहीं होता। यह तब मायने रखता है जब right side के side effects हों।

Note: Short-circuiting आपको बिना null-check exception के if (obj != null && obj.IsValid) सुरक्षित रूप से लिखने देता है।

उदाहरण: Logical Operators

markup
using System;

class Program
{
    static void Main(string[] args)
    {
        bool isLoggedIn = true;
        bool isAdmin = false;

        Console.WriteLine(isLoggedIn && isAdmin);
        Console.WriteLine(isLoggedIn || isAdmin);
        Console.WriteLine(!isAdmin);
    }
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. && और || के बजाय & और | का उपयोग करना, जो short-circuit नहीं करते और हमेशा दोनों sides evaluate करते हैं।
  2. x == 1 || 2 लिखना, जो compile नहीं होता क्योंकि 2 एक bool नहीं है।
  3. && के right side में किसी method call पर भरोसा करना कि यह हमेशा चलेगा जब short-circuiting इसे skip कर सकता है।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.