if / else की जानकारी
if और else किसी program को यह चुनने देते हैं कि क्या करना है, जैसे 'अगर ठंड है, तो coat पहनो, नहीं तो t-shirt पहनो'। किसी block का code सिर्फ तभी चलता है जब उसका सवाल true हो।
In this page:
Syntax
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
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");
}
}
Login to try C/C++/Java/PHP code in the editor
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. #}
आम गलतियां
- एक equals sign के साथ
if (x = 5)लिखना, जो एक assignment है और सिर्फ तभी compile होता है जब assigned value एक bool हो। - condition के बाद एक semicolon लगाना,
if (x > 0);, जिससे body हमेशा चलती है। - braces छोड़ देना जिससे सिर्फ
ifके बाद का पहला statement इससे संबंधित हो।
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: