何时退出一个循环
#include <stdio.h>
void main()
{
int n = 5;
while (n < 7 ) {
printf("n = %d \n ",n);
n ++ ;
printf("now n = %d \n",n);
}
printf("the loop has finished \n. ");
}
无限循环语句
#include <stdio.h>
void main()
{
int n = 0;
while (n < 3 )
printf("n = %d \n ",n);
n ++ ;
printf("now n = %d \n",n);
printf("the loop has finished \n. ");
}
浮点数比较
#include <math.h>
#include <stdio.h>
int main()
{
const double Answer = 3.14159;
double response ;
printf("what is the value of pi ? \n");
scanf( " %lf", &response);
while (fabs(response - Answer)> 0.0001)
{
printf("try again !\n");
scanf( " %lf", &response);
}
printf("close enough\n");
}
c中的真和假
#include <stdio.h>
void main()
{
int ture_val,false_val;
ture_val = (10>2);
false_val = (10 == 2);
printf("true = %d , false = %d ",ture_val,false_val);
}
那些值为真
#include <stdio.h>
void main()
{
int n =3 ;
while (n ) {
printf("%2d is true \n ",n--);
}
printf("%2d is false \n",n);
n = -3 ;
while (n) {
printf("%2d is true\n",n++);
}
printf("%2d is false \n",n);
}
#include <stdio.h>
void main()
{
long num ;
long sum = 0l;
int status ;
printf("please enter next integer ( q to quit );");
printf("(qto quit :");
status =scanf("%ld",&num);
while(status == 1 )// while (status == 1 )会导致无限循环
{
sum +=num ;
printf("please enter next integer ( q to quit );");
status =scanf("%ld",&num);
}
printf("those integers sum to %ld \n",sum);
}
、
使用_bool 变量
#include <stdio.h>
void main()
{
long num ;
long sum = 0l;
_Bool input_is_good ;
printf("plese enter an integer to be summed ");
printf("(q to quit ):");
input_is_good = (scanf("%ld", &num)==1);
while (input_is_good) {
sum += sum;
printf("please enter next integer (q to quit ");
input_is_good = (scanf("%ld", &num)==1);
}
printf("those integers sum to %ld ",sum);
}
一个计数程序
#include <stdio.h>
void main()
{
const int NUMBER =22;
int count = 1 ;
while (count < NUMBER) {
printf("Be my valentine %10d\n" , count );
count ++;
}
}
for 循环
#include <stdio.h>
void main()
{
const int NUMBER =22;
intcount ;
for (count = 1; count < NUMBER; count ++) {
printf("Be my valentine %10d \n", count);
}
}
for 自减
#include <stdio.h>
void main()
{
int secs;
for (secs = 5 ; secs; secs --) {
printf("%d seconds \n",secs);
}
printf("we have ignition \n");
}
用字符代替数字进行计数
#include <stdio.h>
void main()
{
int n ;
char ch ;
for(ch ='a'; ch <= 'z'; ch ++) {
printf("ch = %c %d\n",ch,ch);
}
for (n = 2; n <60 ; n += 12 ) {
printf("n = %d \n", n);
}
}
#include <stdio.h>
void main()
{
int x ;
int y = 55;
for (x = 1; y <=75 ; y = (++x *5) +50 ) {
printf("%10d %10d \n",x,y);
}
}
可以让一个活着多个表达式为空,但不要遗漏 ,只须 确保在循环中包含了一些能使循环最终结束的语句
#include <stdio.h>
void main()
{
int ans , n ;
ans =2;
for (n = 3 ; ans <=25 ; ) {
ans *= n ;
}
printf("n =%5d ,ans = %d \n",n ,ans);
}
#include <stdio.h>
void main()
{
int num = 0 ;
for (printf(" keep entering num bers \n"); num != 6; ) {
scanf("%d",&num);
}
printf("that's the one I wangt \n");
}
逗号运算符
#include <stdio.h>
void main()
{
const int FIRST_oz = 37;
const int NEXT_oz = 23;
intonuces ,cost ;
for (onuces = 1 ,cost = FIRST_oz;onuces <= 16; onuces++, cost += NEXT_oz ) {
printf("%5d $%4.2f \n",onuces,cost/100.0);
}
}
序列数的和
#include <stdio.h>
void main()
{
int t_ct , limit;
double time ,x;
printf("enter the number of terms you want ;");
scanf("%d",&limit);
for (time = 0 , x = 1 ,t_ct = 1; t_ct <= limit; t_ct++,x *= 2.0) {
time +=1.0/x;
printf("time = %f whe terms = %d .\n ",time,t_ct);
}
}
推出条件循环
#include <stdio.h>
void main()
{
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 ,your are cured ");
}
入口条件
#include <stdio.h>
void main()
{
const int secret_code = 13;
int code_entered ;
printf("to enter the triskaidekaphobia therapy club \n");
printf("please enter the secret code number \n");
scanf("%d",&code_entered);
while (code_entered != secret_code) {
printf("to enter the triskaidekaphobia therapy club \n");
printf("please enter the secret code number \n");
scanf("%d",&code_entered);
}
printf(" congratulation you are cured \n");
}
for循环嵌套
#include <stdio.h>
#define ROWS 6
#define CHAES 10
void main()
{
int row;
char ch ;
for (row = 0 ; row < ROWS; row++) {
for (ch = 'A'; ch < ('A' + CHAES); ch++ ) {
printf(" %c",ch);
}
printf("\n");
}
}
使内部循环依赖于外部循环的嵌套循环
#include <stdio.h>
#define ROWS 6
#define CHAES 6
void main()
{
int row;
char ch ;
for (row = 0 ; row < ROWS; row++) {
for (ch = 'A'+row ; ch < ('A' + CHAES); ch++ ) {
printf(" %c",ch);
}
printf("\n");
}
}
使用循环进行数组处理
#include <stdio.h>
#define SIZE 10
#define PAR 72
void main()
{
int index ,score[SIZE];
int sum = 0; float avage ;
printf("enter %d golf scores \n ",SIZE);
for (index = 0 ; index < SIZE ; index ++ ) {
scanf("%d",&score[index]);
}
printf("the scores read in are sa follows \n");
for (index = 0 ; index < SIZE; index++) {
printf("%5d",score[index]);
}
printf("\n");
for ( index = 0 ; index < SIZE ; index++) {
sum += score[index];
}
avage = (float) sum / SIZE;
printf("sum of score = %d average = %.2f\n",sum ,avage);
printf("that's a handicap of %.0f .\n",avage-PAR);
}
计算数值的整数次幂
#include <stdio.h>
double power (double n ,int p )
{
double pow = 1 ;
int i ;
for (i = 1; i <= p ; i ++) {
pow *= n;
}
return pow;
}
void main()
{
double x ,xpow ;
int exp ;
printf("Enter a number and the positive intefger power ");
printf("to which \n the number will be raised enter q");
printf(" to quit \n");
while (scanf( "%lf%d",&x,&exp) == 2)//成功的读取了两个数值,并返回2
{
xpow =power(x,exp);
printf(" %.3g to the ower %d is %.5g \n ", x ,exp,xpow);
printf("Enter next pair of numbers of q to tuit.\n");
}
printf("hope you enjoyed this power trip ---bye ! \n");
}