Go循环、判断和switch

目录

 

一、循环

1、一个标准的 for 循环的写法

2、while条件循环

3、无限循环

二、if 条件

1、标准写法

2、与其它主要编程语言的差异

a、condition 表达式结果必须为布尔值

b、支持变量赋值

三、switch 条件

1、标准写法

2、与其他主要编程语言的差异

a、case后面跟多个选项示例

b、case 后面接表达式的示例


一、循环

与其它主流语言的差异:Go语言仅支持关键字 for 

注意:Go语言不需要 括号

1、一个标准的 for 循环的写法

package loop

import (
	"fmt"
	"testing"
)

func TestStandardForLoop(t *testing.T) {
	for j := 0; j < 10; j++{
		//.....
		fmt.Println(j)
	}
}

2、while条件循环

package loop

import (
	"fmt"
	"testing"
)

func TestWhileLoop(t *testing.T) {
	//while(n < 5)的写法
	for n := 0; n < 5; n++  {
		fmt.Println(n)
	}
}

3、无限循环

package loop

import (
	"fmt"
	"testing"
)

func TestInfiniteLoop(t *testing.T) {
	//无限循环的写法
	n := 0
	for {
		//.....
		fmt.Println(n)
	}
}

二、if 条件

1、标准写法

if condition {
	// code to be executed if condition is true
} else {
	// code to be executed if condition is false
}

if condition-1 {
	// code to be executed if condition-1 is true
} else if condition-2 {
	// code to be executed if condition-2 is true
} else {
	// code to be executed if both condition1 and condition2 are false
}

2、与其它主要编程语言的差异

a、condition 表达式结果必须为布尔值

b、支持变量赋值

if var declaration; condition {
	// code to be executed if condition is true
}
package condition

import "testing"

func TestIfMultiSec(t *testing.T) {
	//if 支持这这种先变量赋值,在写条件的写法,在结果函数可以有多返回值,可以很方便的做错误判断
	if val,err = someFunc(); err == nil {
		t.Log(true)
	} else {
		t.Log(false)
	}
}

三、switch 条件

1、标准写法

package _switch

import (
	"fmt"
	"runtime"
	"testing"
)

func TestSwitch(t *testing.T) {
	switch os := runtime.GOOS; os {
	case "darwin":
		fmt.Println("OS X.")
		//这里不需要写 break, go语言会自动break
	case "linux":
		fmt.Println("Linux.")
	default:
		fmt.Printf("%s.", os)
	}

}

2、与其他主要编程语言的差异

  • 条件表达式不限制为常量或者整数
  • 单个 case 中,可以出现多个结果选项,使用逗号分隔;
  • Go语言不需要使用 break 来明确退出一个 case;
  • 可以不设定 switch 之后的条件表达式,在此种情况下,整个 switch 结构与多个 if{...}elseif{...}的逻辑作用相同。

a、case后面跟多个选项示例

package _switch

import (
	"fmt"
	"runtime"
	"testing"
)

//case 后面跟多个选项示例
func TestSwitchMultiCase(t *testing.T) {
	for i := 0; i<5; i++ {
		switch i {
		//重点:Go语言中支持一个 case 后面跟多个选项
		case 0, 2:
			t.Log("It is even between 0-3")
		case 1, 3:
			t.Log("It is odd between 0-3")
		default:
			t.Log("It not between 0-3")
		}
	}
}

b、case 后面接表达式的示例

package _switch

import (
	"fmt"
	"runtime"
	"testing"
)

//case 后面跟多个选项示例
func TestSwitchMultiCase(t *testing.T) {
	for i := 0; i<5; i++ {
		switch i {
		//重点:Go语言中支持一个 case 后面跟多个选项
		case 0, 2:
			t.Log("It is even between 0-3")
		case 1, 3:
			t.Log("It is odd between 0-3")
		default:
			t.Log("It not between 0-3")
		}
	}
}

//case 后面接表达式的示例
func TestSwitchCaseCondition(t *testing.T) {
	for i := 0; i<5; i++ {
		switch {
		case i%2 == 0:
			t.Log("It is an even number")
		case i %2 == 1:
			t.Log("It is a odd number")
		default:
			t.Log("Unknown")
		}
	}
}

:这篇博文是我学习中的总结,如有转载请注明出处:https://blog.csdn.net/DaiChuanrong/article/details/117755450

上一篇Go运算符

下一篇Go数组和切片

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
循环嵌套是指在一个循环语句中嵌套了另一个或多个循环语句。常见的循环有for循环、while循环和do-while循环循环嵌套可以用来解决一些复杂的问题。 以下是一个for循环嵌套的例子,它可以输出九九乘法表中的所有结果: ```c for(int i = 1; i <= 9; i++){ for(int j = 1; j <= i; j++){ printf("%d*%d=%d ", j, i, i*j); } printf("\n"); } ``` switch语句是一种多分支语句,它的作用与一系列if语句的作用类似。switch语句可以根据某个表达式的值来选择执行哪个分支,通常表达式为整数或字符类型。switch语句的语法如下: ```c switch(expression){ case constant1: // 执行语句1 break; case constant2: // 执行语句2 break; // ... default: // 执行语句n break; } ``` 其中,expression是一个表达式,常见的表达式类型为整数或字符类型;constant1、constant2等是常量表达式,它们的值必须是整数或字符类型;每个case分支后面可以跟一条或多条语句,如果省略break语句,则会继续执行下一个case分支;default分支是可选的,表示如果上面所有分支都不满足,则执行default分支的语句。 以下是一个switch语句的例子,它可以根据用户输入的月份输出对应的季节: ```c int month; printf("请输入月份:"); scanf("%d", &month); switch(month){ case 1: case 2: case 12: printf("冬季\n"); break; case 3: case 4: case 5: printf("春季\n"); break; case 6: case 7: case 8: printf("夏季\n"); break; case 9: case 10: case 11: printf("秋季\n"); break; default: printf("输入错误\n"); break; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值