📄 08.txt
字号:
CS 1355
Introduction to Programming in C
originally scheduled for Thursday 2006.10.5 (Week 4)
made-up on Thursday 2006.10.12 (Week 5)
Lecture notes (at http://r638-2.cs.nthu.edu.tw/C/notes/08.txt)
Chapter 4: C Program Control (cont'd)
- do-while loop
- break, continue
- logical operators
- do-while loop:
- syntax
do {
statement....
} while (condition);
- meaning:
- enter the loop (statement...),
execute the body statement AT LEAST ONCE!!
- do the loop testing at the end of the loop:
- if condition is true, then loop again
- if condition is false, then exit loop
Example: repeat from 1..10 (inclusively)
int counter = 1;
do {
printf("%d ", counter);
} while ( ++counter <= 10 );
Note:
- if you say (counter++ <= 10), then it goes from 1..9.
- if you say (++counter < 10), then it goes from 1..8.
Example: calculator, with an "accumulator"
Syntax:
command number
------- ------
+ number
- number
* number
/ number
c /* clear */
q /* quit */
#include <stdio.h>
int main() {
float accumulator = 0;
float arg;
int command = 0;
do {
if (command != ' ') {
printf(">> "); /* print the prompt character */
}
command = getchar();
switch (command) {
case '+':
if (scanf("%f", &arg) == 1) {
accumulator += arg;
printf("%f\n", accumulator);
} else { /* failed to get a number */
command = 'q';
}
break; /* 铬
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -