← Back to C# Course | Chapter 4: Control Flow | Lesson 1 of 7

if / else की जानकारी

if और else किसी program को यह चुनने देते हैं कि क्या करना है, जैसे 'अगर ठंड है, तो coat पहनो, नहीं तो t-shirt पहनो'। किसी block का code सिर्फ तभी चलता है जब उसका सवाल true हो।

In this page:

  1. if / else
Syntax
csharp
if (condition1)
{
    // ...
}
else if (condition2)
{
    // ...
}
else
{
    // none matched
}

if / else

एक if statement सिर्फ तभी एक block चलाता है जब इसकी condition true हो; एक optional else तब चलता है जब यह false हो। else if chains आपको order में कई conditions जांचने देती हैं, पहले match पर रुकते हुए।

Single statements के लिए braces optional हैं लेकिन स्पष्टता के लिए सुझाई जाती हैं।

Note: अलग if blocks को nest करने के बजाय else if से conditions को chain करें।

उदाहरण: if / else

markup
using System;

class Program
{
    static void Main(string[] args)
    {
        int score = 72;

        if (score >= 90)
            Console.WriteLine("A");
        else if (score >= 70)
            Console.WriteLine("B");
        else
            Console.WriteLine("C");
    }
}
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. एक equals sign के साथ if (x = 5) लिखना, जो एक assignment है और सिर्फ तभी compile होता है जब assigned value एक bool हो।
  2. condition के बाद एक semicolon लगाना, if (x > 0);, जिससे body हमेशा चलती है।
  3. braces छोड़ देना जिससे सिर्फ if के बाद का पहला statement इससे संबंधित हो।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 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.