Saturday 28 August 2021

Go If

 The if statement in Go is used to test the condition. If it evaluates to true, the body of the statement is executed. If it evaluates to false, if block is skipped.

Syntax :

  1. if(boolean_expression) {  
  2.    /* statement(s) got executed only if the expression results in true */  
  3. }  

Go if example

  1. package main  
  2. import "fmt"  
  3. func main() {  
  4.    /* local variable definition */  
  5.    var a int = 10  
  6.    /* check the boolean condition using if statement */  
  7.    if( a % 2==0 ) {      /* if condition is true then print the following 
  8. */ fmt.Printf("a is even number" )  
  9.    }  
  10. }  

Output:

a is even number

Go if-else

The if-else is used to test the condition. If condition is true, if block is executed otherwise else block is executed.

Syntax :

  1. if(boolean_expression) {  
  2.    /* statement(s) got executed only if the expression results in true */  
  3. else {  
  4.    /* statement(s) got executed only if the expression results in false */  
  5. }  

Go if-else example

  1. package main  
  2. import "fmt"  
  3. func main() {  
  4.    /* local variable definition */  
  5.    var a int = 10;  
  6.    /* check the boolean condition */  
  7.    if ( a%2 == 0 ) {  
  8.       /* if condition is true then print the following */  
  9.       fmt.Printf("a is even\n");  
  10.    } else {  
  11.       /* if condition is false then print the following */  
  12.       fmt.Printf("a is odd\n");  
  13.    }  
  14.    fmt.Printf("value of a is : %d\n", a);  
  15. }  

Output:

a is even
value of a is : 10

Go If-else example: with input from user

  1. func main() {  
  2.    fmt.Print("Enter number: ")  
  3.    var input int  
  4.   fmt.Scanln(&input)  
  5.    fmt.Print(input)  
  6.    /* check the boolean condition */  
  7.    if( input % 2==0 ) {  
  8.       /* if condition is true then print the following */  
  9.       fmt.Printf(" is even\n" );  
  10.    } else {  
  11.       /* if condition is false then print the following */  
  12.       fmt.Printf(" is odd\n" );  
  13.    }  
  14. }  

Output:

Enter number: 10
10 is even

Go If else-if chain

The Go if else-if chain is used to execute one statement from multiple conditions.

We can have N numbers of if-else statement. It has no limit.

The curly braces{ } are mandatory in if-else statement even if you have one statement in it. The else-if and else keyword must be on the same line after the closing curly brace }.

Go If else-if chain Example

  1.       
  2. package main  
  3. import "fmt"  
  4. func main() {  
  5.    fmt.Print("Enter text: ")  
  6.    var input int  
  7.    fmt.Scanln(&input)  
  8.    if (input < 0 || input > 100) {  
  9.       fmt.Print("Please enter valid no")  
  10.    } else if (input >= 0 && input < 50  ) {  
  11.       fmt.Print(" Fail")  
  12.    } else if (input >= 50 && input < 60) {  
  13.       fmt.Print(" D Grade")  
  14.    } else if (input >= 60 && input < 70  ) {  
  15.       fmt.Print(" C Grade")  
  16.    } else if (input >= 70 && input < 80) {  
  17.       fmt.Print(" B Grade")  
  18.    } else if (input >= 80 && input < 90  ) {  
  19.       fmt.Print(" A Grade")  
  20.    } else if (input >= 90 && input <= 100) {  
  21.       fmt.Print(" A+ Grade")  
  22.    }  
  23. }  

Output:

Enter text: 84
 A Grade

Go Nested if-else

We can also nest the if-else statement to execute one statement from multiple conditions.

Syntax

  1. if( boolean_expression 1) {  
  2.     /* statement(s) got executed only if the expression 1 results in true */  
  3.    if(boolean_expression 2) {  
  4.     /* statement(s) got executed only if the expression 2 results in true */  
  5.    }  
  6. }  

nested if-else example

  1. package main  
  2. import "fmt"  
  3. func main() {  
  4.    /* local variable definition */  
  5.    var x int = 10  
  6.    var y int = 20  
  7.    /* check the boolean condition */  
  8.    if( x >=10 ) {  
  9.       /* if condition is true then check the following */  
  10.       if( y >= 10 )  {  
  11.          /* if condition is true then print the following */  
  12.          fmt.Printf("Inside nested If Statement \n" );  
  13.       }  
  14.    }  
  15.    fmt.Printf("Value of x is : %d\n", x );  
  16.    fmt.Printf("Value of y is : %d\n", y );  
  17. }  

Output:

Inside nested If Statement 
Value of x is : 10
Value of y is : 20


No comments:

Post a Comment