← Back to Rust Course | Chapter 3: Control Flow | Lesson 1 of 7

if/else के Expressions

if और else आपके program को यह चुनने देते हैं कि कोई चीज़ true है या false, इसके आधार पर एक अलग path अपनाना।
Syntax
rust
if condition {
    // body
} else if condition2 {
    // body
} else {
    // body
}

let name = if condition { value1 } else { value2 };

Basic if/else

एक if expression एक boolean condition evaluate करता है और associated block सिर्फ तब चलाता है जब यह true हो; एक optional else block अन्यथा चलता है। Condition के आस-पास कोई parentheses की ज़रूरत नहीं, C या Java के विपरीत।

उदाहरण: Basic if/else

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `number`, set to `7`
    let number = 7;
    // Check whether `number > 5`
    if number > 5 {
        // Print "Greater than five" to the console (with a trailing newline)
        println!("Greater than five");
    // Otherwise, run this branch
    } else {
        // Print "Five or less" to the console (with a trailing newline)
        println!("Five or less");
    }
}

else if Chains

कई conditions को else if से chain किया जा सकता है, top से bottom तक order में जांचे गए। पहला matching branch चलता है, और बाकी skip हो जाते हैं।

उदाहरण: else if Chains

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `grade`, set to `85`
    let grade = 85;
    // Check whether `grade >= 90`
    if grade >= 90 {
        // Print "A" to the console (with a trailing newline)
        println!("A");
    // Otherwise, if `grade >= 80`, run this branch
    } else if grade >= 80 {
        // Print "B" to the console (with a trailing newline)
        println!("B");
    // Otherwise, run this branch
    } else {
        // Print "C or below" to the console (with a trailing newline)
        println!("C or below");
    }
}

if as an Expression

चूंकि if एक expression है, सिर्फ एक statement नहीं, यह सीधे एक value produce कर सकता है, जिसे आप एक variable को assign कर सकते हैं। दोनों branches को एक ही type की values return करनी चाहिए।

Note: 5 और 6 के बाद कोई semicolons note करें -- वे हर block की tail expressions हैं।

उदाहरण: if as an Expression

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `condition`, set to `true`
    let condition = true;
    // Declare an immutable variable `number`, set to `if condition { 5 } else { 6 }`
    let number = if condition { 5 } else { 6 };
    // Print "number is {}" to the console, substituting in `number` (with a trailing newline)
    println!("number is {}", number);
}

Condition Must Be a Bool

Rust चाहता है कि if condition एक असली bool value हो; यह implicitly किसी nonzero integer को true नहीं मानेगा, C के विपरीत। यह mistyped conditions से bugs की एक पूरी category रोकता है।

उदाहरण: Condition Must Be a Bool

markup
// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `count`, set to `3`
    let count = 3;
    // Check whether `count != 0`
    if count != 0 {
        // Print "count is non-zero: {}" to the console, substituting in `count` (with a trailing newline)
        println!("count is non-zero: {}", count);
    }
}
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. Condition को if (x > 5) जैसे parentheses में wrap करना, जो Rust में अनावश्यक और unidiomatic है।
  2. यह भूल जाना कि if एक expression है और एक value return कर सकता है, फिर extra let mut variables के साथ अत्यधिक verbose code लिखना।
  3. if को एक expression के रूप में उपयोग करते समय if और else branches के बीच types mix करना, जो compile होने में fail होता है।
चैप्टर सारांश
  • if/else if/else boolean conditions पर branch करते हैं; condition के आस-पास कोई parentheses ज़रूरी नहीं।
  • Rust में if एक expression है, इसलिए यह एक value produce कर सकता है जो सीधे किसी variable को assign होती है।
  • एक expression के रूप में उपयोग किए गए if के सभी branches को एक जैसे type तक evaluate होना चाहिए।
  • Condition एक bool होना चाहिए -- Rust numbers को implicitly booleans में convert नहीं करता।
🔒

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.