c學習筆記--4 迴圈控制語句 if for while do
阿新 • • 發佈:2018-12-12
好久之前的了,學習c語言的筆記。 依舊是老套路,從基礎的變數型別,到函式定義一步步學起
#include <stdio.h>
#include <string.h>
//控制語句 這個在不同語言裡幾乎都不變
void test5()
{
int a = 4;
//if判斷語句
if (0 < a < 2)
printf("var is level 1\n");
else if (a >= 2 && a <= 5)
{
printf("var is level 2\n");
}
else
{
printf("var is level 3\n" );
}
//switch 分支語句
switch (a)
{
case 1:printf("var vlue is 1\n"); break;
case 2:printf("var vlue is 2\n"); break;
case 3:printf("var vlue is 3\n"); break;
case 4:printf("var vlue is 4\n"); break;
default:printf("var vlue is out of control\n"); break;
}
//for迴圈語句
for (int i = 0; i <= a && i != 3; i++)
{
printf("for vlue is :%d\n", i);
}
//while 迴圈
int j = 0;
while (j <= a)
{
printf("while vlue is :%d\n", j);
j++;
}
//do迴圈 跟while相比 至少迴圈一下 因為迴圈體先執行後判斷
j = 0;
do
{
printf("do - while vlue is :%d\n", j);
j++;
}
while (j <= 4);
//goto語句的標籤 後期可以跳轉到這裡執行
aaa:
j-- ;
printf("goto j vlue is :%d\n", j);
if (j > 1)
goto aaa;
gets();
}