go基础——if语法

package main

import "fmt"

/*
	条件语句:if
	注意点:
		1.if后的{,要与if条件写在同一行;
		2.else要跟在}之后,不能另起一行;
		3.if和else,二者必选其一
*/
func main() {

	num := 10
	if num > 10 {
		fmt.Println("大于10")
	} else if num < 10 {
		fmt.Println("小于10")
	} else {
		fmt.Println("等于10")
	}

	//if嵌套
	if num == 10 {
		fmt.Println("等于10")
	} else {
		if num > 10 {
			fmt.Println("大于10")
		} else {
			fmt.Println("小于10")
		}
	}

	if num != 10 {
		if num > 10 {
			fmt.Println("大于10")
		} else {
			fmt.Println("小于10")
		}

	} else {
		fmt.Println("等于10")
	}

	//特殊用法
	if num1 := -4; num1 > 0 {
		fmt.Printf("%d是正数", num1)
		//num1作用域只在该if语句
	} else {
		fmt.Printf("%d是负数", num1)
	}
}

等于10
等于10
等于10
-4是负数

posted @ 2021-09-10 12:35  沄持的学习记录  阅读(144)  评论(0编辑  收藏  举报