کانال بله, جهت پشتیبانی و اطلاع رسانی کانال بله, جهت پشتیبانی و اطلاع رسانی
عضویت

آموزش دستور if-else در golang

Go If

دستور if در Go برای تست شرط استفاده می شود. اگر شرط درست ارزیابی شود، بدنه عبارت اجرا می شود. اگر نه، از بلوک if رد خواهد شد.

سینتکس :

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

مثال :

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

خروجی:

a is even number

Go if-else

if-else برای تست شرایط استفاده می شود. اگر شرط درست باشد، if اجرا خواهد شد در غیر این صورت بلوک else اجرا می شود.

سینتکس :

    if(boolean_expression) {  
        /* statement(s) got executed only if the expression results in true */  
     } else {  
        /* statement(s) got executed only if the expression results in false */  
     }  

مثال :

package main  
import "fmt"  
func main() {  
   /* local variable definition */  
   var a int = 10;  
   /* check the boolean condition */  
   if ( a%2 == 0 ) {  
      /* if condition is true then print the following */  
      fmt.Printf("a is even\n");  
   } else {  
      /* if condition is false then print the following */  
      fmt.Printf("a is odd\n");  
   }  
   fmt.Printf("value of a is : %d\n", a);  
}  

خروجی:

a is even
value of a is : 10

Go If-else: با ورودی

    func main() {  
    fmt.Print("Enter number: ")  
    var input int  
   fmt.Scanln(&input)  
    fmt.Print(input)  
    /* check the boolean condition */  
    if( input % 2==0 ) {  
       /* if condition is true then print the following */  
       fmt.Printf(" is even\n" );  
    } else {  
       /* if condition is false then print the following */  
       fmt.Printf(" is odd\n" );  
    }  
 }  

خروجی:

Enter number: 10
10 is even

Go If else-if chain

از زنجیره Go if else-if برای اجرای یک دستور چند شرطی استفاده می شود. می توانیم N عدد از عبارت if-else داشته باشیم.

پرانتزهای { } در عبارت if-else اجباری هستند حتی اگر یک عبارت در آن داشته باشید. کلمه کلیدی else-if و else باید بعد از بسته شدن پرانتز } در همان خط باشد.

مثال :

package main  
import "fmt"  
func main() {  
   fmt.Print("Enter text: ")  
   var input int  
   fmt.Scanln(&input)  
   if (input < 0 || input > 100) {  
      fmt.Print("Please enter valid no")  
   } else if (input >= 0 && input < 50  ) {  
      fmt.Print(" Fail")  
   } else if (input >= 50 && input < 60) {  
      fmt.Print(" D Grade")  
   } else if (input >= 60 && input < 70  ) {  
      fmt.Print(" C Grade")  
   } else if (input >= 70 && input < 80) {  
      fmt.Print(" B Grade")  
   } else if (input >= 80 && input < 90  ) {  
      fmt.Print(" A Grade")  
   } else if (input >= 90 && input <= 100) {  
      fmt.Print(" A+ Grade")  
   }  
}  

خروجی:

    Enter text: 84
    A Grade

Go Nested if-else

همچنین می‌توانیم دستور if-else را برای اجرای دستور چند شرط تودرتو به کار ببریم.

خروجی:

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

مثال if-else تو در تو

package main  
import "fmt"  
func main() {  
   /* local variable definition */  
   var x int = 10  
   var y int = 20  
   /* check the boolean condition */  
   if( x >=10 ) {  
      /* if condition is true then check the following */  
      if( y >= 10 )  {  
         /* if condition is true then print the following */  
         fmt.Printf("Inside nested If Statement \n" );  
      }  
   }  
   fmt.Printf("Value of x is : %d\n", x );  
   fmt.Printf("Value of y is : %d\n", y );  
}  

خروجی:

Inside nested If Statement 
Value of x is : 10
Value of y is : 20
1402/01/31 5632
رمز عبور : tahlildadeh.com یا www.tahlildadeh.com
نظرات شما

نظرات خود را ثبت کنید...