Branching with if-else in rust also is similar to other languages. Just the difference is the way of writing(syntax). Here the condition doesn’t need to be surrounded by parenthesis. Decision-making structure is an important part of life. Similarly, in programming also there are situations where you have to decide(make decisions) that for the specific conditions which code should be executed. For this, we use the if-else.
Decision-making statements in programming languages decide the direction of the flow of program execution.
Mainly there are 4 types that are used more often are explained in this article.
They are:
- if statement
- if-else statement
- else-if ladder statement
- Nested if-else statement
If Statement
Here only 1 condition is there and if that condition is true then a block of code inside the if statement will be executed.
Syntax:
if boolean_expression {
// statement(s) will execute if the boolean expression is true
}
Example: To check if the number is negative or positive.
Rust
fn main(){
let num = -5;
if num < 0 {
println!("number is negative") ;
}
}
Output:
number is negative
If-Else Statement
Here also there is only 1 condition but the difference here is that there is another block of code that is executed when the if the condition is false and suppose that the condition is true then the block of code inside if is executed and else part is ignored completely.
Syntax:
if boolean_expression {
// if the boolean expression is true statement(s) will execute
} else {
//if the boolean expression is false statement(s) will execute
}
Example: To check which number is bigger
Rust
fn main(){
let num = 3;
let num1= -3;
if num > num1 {
println!("1st number is bigger") ;
}
else {
println!("2nd number is bigger") ;
}
}
Output:
1st number is bigger
Else-If Ladder Statement
In this, there is more than 1 condition. In fact, there can be many and after that, there is else now suppose any of the conditions does not satisfy then all are ignored and block of code inside else part will be executed. But suppose if some condition is satisfied then the block of code in that part will be executed and rest will be ignored.
Syntax:
if boolean_expression1 {
//statements if the expression1 evaluates to true
} else if boolean_expression2 {
//statements if the expression2 evaluates to true
} else {
//statements if both expression1 and expression2 result to false
}
Example: To check if the number is negative, positive or zero
Rust
fn main() {
let num = 0 ;
if num > 0 {
println!("num is positive");
} else if num < 0 {
println!("num is negative");
} else {
println!("num is neither positive nor negative") ;
}
}
Output:
num is neither positive nor negative
Nested if-else statement
If we want to check multiple conditions we can use if statements within if statements we call it as nested statement. A nested if is an if statement that is the target of another if or else.
Syntax:
if condition1 {
// code block 1
if condition2 {
// code block 2
} else {
// code block 3
}
} else {
// code block 4
}
Example: This example check if the number is greater than other number also checks it is even or odd using nested if-else.
Rust
fn main() {
let x = 10;
let y = 5;
if x > y {
println!("x is greater than y");
if x % 2 == 0 {
println!("x is even");
} else {
println!("x is odd");
}
} else {
println!("x is not greater than y");
}
}
Outputx is greater than y
x is even
Similar Reads
C++ Nested if-else Statement
Nested if-else statements are those statements in which there is an if statement inside another if else. We use nested if-else statements when we want to implement multilayer conditions (condition inside the condition inside the condition and so on). C++ allows any number of nesting levels. Let's ta
3 min read
IF-ELSE-IF statement in R
if-else-if ladder in R Programming Language is used to perform decision making. This ladder is used to raise multiple conditions to evaluate the expressions and take an output based on it. This can be used to evaluate expressions based on single or multiple conditions connected by comparison or arit
2 min read
Rust - if let Statement
If-let statement in Rust is an expression that allows pattern matching and is similar to an if expression. Once the pattern condition is matched then the block of code is executed. A scrutinee is an expression that is matched during match statement executions. For example match g{ X =>5 , Y =>
2 min read
If-Else Statement in Solidity
If-else statement is a type of conditional statement that allows the program to execute one block of code if a certain condition is true and an else block of code is executed if the condition is false. It contains two blocks of code and depending on the condition any one of the blocks is getting exe
2 min read
R - if statement
If statement is one of the Decision-making statements in the R programming language. It is one of the easiest decision-making statements. It is used to decide whether a certain statement or block of statements will be executed or not i.e if a certain condition is true then a block of statement is ex
3 min read
Swift - Nested if-else Statement
In Swift, a situation comes when we have to check an if condition inside an if-else statement then a term comes named nested if-else statement. It means an if-else statement inside another if statement. Or in simple words first, there is an outer if statement, and inside it another if - else stateme
4 min read
Swift - If-else Statement
Just like other programming languages in Swift language also support the if-else statement. In the if-else statement, when the given condition is true then the code present inside the if condition will execute, and when the given condition is false then the code present inside the else condition wil
3 min read
Swift - If-else-if Statement
In Swift, the if-else if-else condition is used to execute a block of code among multiple options. It gives a choice between more than two alternative conditions as soon as one of the conditions is true, the statement associated with that if is executed. If all conditions are false or not satisfied
3 min read
Rust - While let Statement
A Rust While let Statement in Rust is a special statement that basically refractors and allows us to view a clean code. While let statement was inspired by the if let statement in Rust. Basically in an, if let statement, there is a pattern match and then the block of code gets executed. if let Some(
1 min read
Bash Scripting - Else If Statement
In this article, we will discuss how to write a bash script for the Else If statement. Conditional statements: The statements that perform specific functions based on certain conditions are called conditional statements. In bash scripting, we have several conditional statements like IF, IF-ELSE, IF-
3 min read