本文目錄一覽:
- 1、C語言 DO WHILE
- 2、C語言do while怎麼使用
- 3、c語言中的do-while循環怎麼用啊?給個例子唄。
- 4、C語言do while 語句
- 5、c語言do while循環語句舉例
- 6、C語言 用do while語句寫
C語言 DO WHILE
/* 主函數 */
/************************************/
void main()
{
Ok_Menu () ;
while(true)
{
do
{
Read_Temperature() ;
Disp_Temperature() ;
}
while(!presence);
Error_Menu () ;
do
{
Init_DS18B20() ;
beep() ;
}
while(presence) ;
}
}
單片機主函數都有個死循環的 。
如果不加死循環,你的單片機程序就只能執行一次裏面的程序就結束了,
換句話說,你的單片機只幹了一件事情就什麼都不幹了,和斷電了一樣沒有功能了
C語言do while怎麼使用
工具/材料
c語言編譯環境(gcc/visual studio等)
01
do{code}
while(condition);是基本的格式。
02
下面我們舉一個計算1到10的和的例子。
03
編譯,運行結果如下。它和單獨的while不同之處在於無論,無論while條件是否為true都會執行一次運算,而while需要首先判斷條件是否為true才會決定是否執行循環體。
特別提示
這裡使用ubuntu下的gcc編譯器與vim編輯器進行演示。不同編輯環境會略有不同。
c語言中的do-while循環怎麼用啊?給個例子唄。
以下列代碼為例:
main()
{
int day = 1;
do
{
printf(“%d\n”, day);
day++;
}
while (day = 7);
return 0;
}
程序在執行的過程為:首先進入第一次循環顯示1並將day的值加1,然後做條件判斷day有值為2,於是day = 7的結果為真,返回到do後面的循環體進入下一次循環…直到day的值為7時。
當printf顯示出7,然後day的值加1,然後做條件判斷,day的值為8,於是day = 7的結果為假,結束循環。
擴展資料:
C 語言提供了以下幾種循環類型:
1、while循環
只要給定的條件為真,C 語言中的 while 循環語句會重複執行一個目標語句。
2、for循環
for 循環允許您編寫一個執行指定次數的循環控制結構。
3、do…while
不像 for 和 while 循環,它們是在循環頭部測試循環條件。在 C 語言中,do…while 循環是在循環的尾部檢查它的條件。
do…while 循環與 while 循環類似,但是 do…while 循環會確保至少執行一次循環。
循環控制語句包括break語句、continue語句、goto語句,這些都可以改變代碼的執行順序,通過它可以實現代碼的跳轉。
參考資料:
循環語句——百度百科
C語言do while 語句
#include stdio.h
int main()
{
int score;
do
{
printf(“請輸入學生成績(輸入-1結束):”);
scanf(“%d”,score);
if(score==-1)
break;
else if(score100||score0)
printf(“輸入成績應在0~100分之間,請重新輸入!\n”);
else if(score=90)
printf(“等級:優\n”);
else if(score=80)
printf(“等級:良\n”);
else if(score=70)
printf(“等級:中\n”);
else if(score=60)
printf(“等級:及格\n”);
else
printf(“等級:不及格\n”);
}
while(1);
printf(“謝謝使用!\n”);
}
c語言do while循環語句舉例
這篇文章主要給大家介紹了關於C語言中do-while語句的2種寫法示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨着小編來一起學習學習吧
while循環和for循環都是入口條件循環,即在循環的每次迭代之前檢查測試條件,所以有可能根本不執行循環體中的內容。C語言還有出口條件循環(exit-condition loop),即在循環的每次迭代之後檢查測試條件,這保證了至少執行循環體中的內容一次。這種循環被稱為do while循環。
看下面的例子:
#include stdio.h
int main(void)
{
const int secret_code = 13;
int code_entered;
do
{
printf(“To enter the triskaidekaphobia therapy club,\n”);
printf(“please enter the secret code number: “);
scanf(“%d”, code_entered);
} while (code_entered != secret_code);
printf(“Congratulations! You are cured!\n”);
return 0;
}
運行結果:
To enter the triskaidekaphobia therapy club,
please enter the secret code number: 12
To enter the triskaidekaphobia therapy club,
please enter the secret code number: 14
To enter the triskaidekaphobia therapy club,
please enter the secret code number: 13
Congratulations! You are cured!
使用while循環也能寫出等價的程序,但是長一些,如程序清單6.16所示。
#include stdio.h
int main(void)
{
const int secret_code = 13;
int code_entered;
printf(“To enter the triskaidekaphobia therapy club,\n”);
printf(“please enter the secret code number: “);
scanf(“%d”, code_entered);
while (code_entered != secret_code)
{
printf(“To enter the triskaidekaphobia therapy club,\n”);
printf(“please enter the secret code number: “);
scanf(“%d”, code_entered);
}
printf(“Congratulations! You are cured!\n”);
return 0;
}
C語言 用do while語句寫
這個程序不錯,推薦你嘗試下
#include “stdio.h”
void main()
{
int i=1,k;
char t[2];
do
{
printf(“請輸入一個整數\n\r”);
gets(t);
if(t[0]48t[0]57)
{
k=(int)(t[0]-48);
i=0;
if(i==2)
{
printf(“是2\r\n”);
}
else if(i%2==0)
{
printf(“偶數\r\n”);
}
else
{
printf(“奇數\r\n”);
}
}
else
{
i=1;
}
}while(i);
}
歡迎加入QQ群:218691837
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/271985.html