本次授课内容如下:选择语句、循环
辅助教材为 《C语言程序设计现代方法(第2版)》
< > <= >=
== !=
! && ||
简单介绍各自含义
示例:
a<b&&a<c;
a==b||a==c;
a!=b
!(i<j)
if(a==b)
a++;
if(i<0&&i>-2);
if(i<0||i>3);
/************************/
if(a==4)
{
a=b;
b=3;
}
/************************/
if(a==b)
{
}
else//a!=b
{
}
/************************/
if()
{
if()
{
}
else
{
}
}
else
{
if()
{
}
else
{
}
}
/************************/
if(a==1)
{}
else if(a==2)
{}
else if(a==3)
{}
else
{}
/************************/
//悬空else
if (y != 0)
if (x != 0)
result = x / y;
else
printf("Error: y is equal to 0\n");
示例程序
#include
int main(void)
{
float commission, value;
printf("Enter value of trade: ");
scanf("%f", &value);
if (value < 2500.00f)
commission = 30.00f + .017f * value;
else if (value < 6250.00f)
commission = 56.00f + .0066f * value;
else if (value < 20000.00f)
commission = 76.00f + .0034f * value;
else if (value < 50000.00f)
commission = 100.00f + .0022f * value;
else if (value < 500000.00f)
commission = 155.00f + .0011f * value;
else
commission = 255.00f + .0009f * value;
if (commission < 39.00f)
commission = 39.00f;
printf("Commission: $%.2f\n", commission);
return 0;
}
int i,j,k;
i=1,j=2;
k=i>j?i:j;
k=(i>=0?i:0)+j;
/************************/
if(i>j)return i;
else return j;
return i>j?i:j;
if (grade == 4)
printf("Excellent");
else if (grade == 3)
printf("Good");
else if (grade == 2)
printf("Average");
else if (grade == 1)
printf("Poor");
else if (grade == 0)
printf("Failing");
else
printf("Illegal grade");
switch (grade) {
case 4: printf("Excellent");
break;
case 3: printf("Good");
break;
case 2: printf("Average");
break;
case 1: printf("Poor");
break;
case 0: printf("Failing");
break;
default: printf("Illegal grade");
break;
}
switch (表达式) {
case 常量表达式 : 语句
...
case 常量表达式 : 语句
default : 语句
}
switch (grade) {
case 4: printf("Excellent");
case 3: printf("Good");
case 2: printf("Average");
case 1: printf("Poor");
case 0: printf("Fai1ing");
default: printf("Illegal grade");
}
while(i<2)
i*=2;
while(k>0)
{
printf("%d\n",k);
k--;
}
while(1)
{
k++;
printf("%d\n",k);
}
i = 10;
while (i > 0) {
printf("T minus %d and counting\n", i);
i--;
}
示例程序
#include
int main(void)
{
int i, n;
printf("This program prints a table of squares.\n");
printf("Enter number of entries in table: ");
scanf("%d", &n);
i = 1;
while (i <= n) {
printf("%10d%10d\n", i, i * i);
i++;
}
return 0;
}
#include
int main(void)
{
int n, sum = 0;
printf("This program sums a series of integers.\n");
printf("Enter integers (0 to terminate): ");
scanf("%d", &n);
while (n != 0) {
sum += n;
scanf("%d", &n);
}
printf("The sum is: %d\n", sum);
return 0;
}
先判断后执行,先执行后判断,有时候会有一次执行的区别
i = 10;
do {
printf("T minus %d and counting\n", i);
--i;
} while (i > 0);
int i=1;
while(i!=1)
{
printf("%d\n",i);
}
printf("******************************\n");
do{
printf("%d\n",i);
}while(i!=1);//不加分号可能会被别的识别
i++;
示例程序
#include
int main(void)
{
int digits = 0, n;
printf("Enter a nonnegative integer: ");
scanf("%d", &n);
do {
n /= 10;
digits++;
} while (n > 0);
printf("The number has %d digit(s).\n", digits);
return 0;
}
同等目标下,用for实现循环更可控
for (i = 10; i > 0; i--)
printf("T minus %d and counting\n",i);
/**************************等价于*/
i = 10;
while (i > 0) {
printf("T minus %d and counting\n", i);
i--;
}
/**************************等价于*/
for (i = 10; i > 0;)
printf("T minus %d and counting\n", i--);
/**************************等价于*/
i=10;
for (; i > 0;)
printf("T minus %d and counting\n", i--);
for (i = 10; i > 0; --i)
printf("T minus %d and counting\n",i);
for(i=0;i<n;i++);
for(i=1;i<=n;i++);
for(i=n-1;i>=0;i--);
for(i=n;i>0;i--);
while(1);
for(;;;);
int sum=0,i=0;
for(sum=0,i=1;i<=n;i++)sum+=i;//可以叠加无数变量
只能用于循环
for(i=0;i<n;i++)
{
if(i%2==0)continue;
printf("%d",i);
}
for(int i=2;i<n;i++)
if(n%i==0)break;
for(int i=1;i<4;i++)
{
printf("i\n");
for(int j=1;j<4;j++)
{
printf("before break\n");
if(j==2)break;
printf("after break\n");
}
}
while()
{
switch(){
case ():break;
default:break;
}
}
for(d=2;d<n&&n%d!=0;d++);
if(d<n)printf("%d可以被%d整除",n,d);
for(i=0;i<1000;i++);
本次授课讲述第五章和第六章内容,关键点:选择和循环