loading

Rust if-else

'If' statement

The condition’s truth value is ascertained using the ‘if’ expression. Control executes the ‘if’ block if the condition is true; otherwise, control skips the ‘if’ block.

Different ways to represent 'if' block:

  • if block
  • if-else block
  • if else-if ladder
  • nested if

Syntax of 'if':

				
					if condition  
 {  
             //block statements;  
}  
				
			

The block statements in the syntax above are performed if the condition is true; else, the block is skipped.

Flow Chart of "if statement"

Rust If-Else -

Example:

Let’s see a simple example of ‘if’ statement.

				
					fn main()  
  
 let a=1;  
 if a==1  
  {  
      println!("a is equal to 1");  
   }  
				
			

Output:

				
					a is equal to 1
				
			

In this instance, a has a value of 1. The string supplied as an argument to println! is thus shown on the console, indicating that the condition stated in the ‘if’ statement is true.

if-else

The ‘if’ block is run and the statements inside the ‘else’ block are skipped if the condition is true. The statements inside the ‘if’ block are skipped and the ‘else’ block is performed if the condition is false.

Syntax

				
					 if condition  
{  
   //block statements  
}  
else  
{  
    //block statements  
 }  
				
			

Flow Chart of "if-else"

Rust If-Else -

Let’s see a simple example of ‘if-else’ statement.

				
					 fn main()  
{  
  let a=3;  
  let b=4;  
  if a>b  
  {  
     println!("a is greater than b");  
  }  
  else  
   {  
     println!("a is smaller than b");   
   }  
}  
				
			

Output:

				
					a is smaller than b
				
			

In this instance, the value of an is smaller than the value of b and equals 3. “A is smaller than b” is printed on the screen as a result of the else block being performed.

else-if

The ‘else-if’ expression is used when you wish to verify multiple conditions.

Syntax of else-if

				
					 if condition 1  
{  
  //block statements  
}  
else if condition 2  
{  
  //block statements  
}   
.  
.  
else{  
//block statements  
}  
				
			

When a block with the above syntax is executed by Rust, it searches for the first true condition and stops executing the remaining blocks.

Flow Chart of "else if"

Rust If-Else -

Let's see a simple example of else-if statement

				
					fn main()  
  
 let num= -5;  
 if num>0  
 {  
   println!("number is greater than 0");  
 }  
 else if num<0  
 {  
   println!("number is less than 0 ");  
 }  
 else  
 {  
   println!("number is not equal to 0");  
 }  
				
			

Output:

				
					number is less than 0
				
			

In this case, num is less than 0 and its value is equal to -5. Consequently, the otherwise if block is run.

Nested if-else

Nested if-else is the term used to describe if-else statements that are contained inside the body of another if or else block.

Syntax of Nested if-else

				
					 if condition 1  
{  
   // block statements  
   if condition 2  
{  
      //block statements  
}  
else  
{  
    //block statements  
}  
}  
else  
{  
   //block statements  
}  
				
			

Example

				
					fn main()  
  
 let a=5;  
 let b=6;  
 if a!=b  
 {  
   if a>b  
   {  
     println!("a is greater than b");  
   }  
   else  
    {  
      println!("a is less than b");  
    }  
 }  
  
 else  
{  
      println!("a is equal to b");  
 }  
				
			

Output:

				
					a is less than b
				
			

The values of a and b in this case do not equal. Consequently, control is placed inside the ‘if’ block when an is less than b. As a result, the ‘else’ block, which is included inside the ‘if’ block, gets executed.

Share this Doc

Rust if-else

Or copy link

Explore Topic