Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Go – if-else use cases

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 :

if(boolean_expression)

 {  

   /* statement(s) got executed only if the expression results in true */  

}  

Use Case 1:

package main 

import “fmt” 

funcmain() { 

     var a int = 10 

if( a % 2==0 ) {      // if condition is true then print the following

fmt.Printf(“a is even number” ) 

   } 

Output:

Use Case 2:

package main 

import “fmt” 

funcmain() { 

   var iint = 10; 

      if ( i%2 == 0 ) { 

      /* if condition is true then print the following */ 

fmt.Printf(“i is even number”); 

   } else { 

      /* if condition is false then print the following */ 

fmt.Printf(“i is odd number”); 

   } 

Output:

Use Case 3:

package main 

import “fmt” 

funcmain() { 

fmt.Print(“Enter number: “) 

   var i int 

fmt.Scanln(&i) 

fmt.Print(i) 

   /* check the boolean condition */ 

if( i % 2==0 ) { 

fmt.Println(” is even” ); 

   } else { 

fmt.Println(” is odd” ); 

   } 

Output:

Use Case 4:

package main 

import “fmt” 

funcmain() { 

fmt.Print(“Enter text: “) 

   var Input int 

fmt.Scanln(&input) 

   if (input 100) { 

fmt.Print(“Please enter valid no”) 

   } else if (input >= 0 && input

fmt.Print(” Fail”) 

   } else if (input >= 50 && input

fmt.Print(” D Grade”) 

   } else if (input >= 60 && input

fmt.Print(” C Grade”) 

   } else if (input >= 70 && input

fmt.Print(” B Grade”) 

   } else if (input >= 80 && input

fmt.Print(” A Grade”) 

   } else if (input >= 90 && input

fmt.Print(” A+ Grade”) 

   } 

Output:

The post Go – if-else use cases appeared first on Prwatech.



This post first appeared on Learn Big Data Hadoop In Bangalore, please read the originial post: here

Share the post

Go – if-else use cases

×

Subscribe to Learn Big Data Hadoop In Bangalore

Get updates delivered right to your inbox!

Thank you for your subscription

×