C学习笔记(七)C控制语句:分支和跳转

if语句

if语句被称为分支语句或选择语句。

if(expression)
        statement

如果expression求得的值为真(非零),就执行statement;否则跳过该语句。

在if语句中添加else关键字

if(expression)
        statement1
else
        statement2

如果expression为真(非零),就执行statement1;如果expression为假或非零,就执行else后面的语句。

getchar()和putchar()

getchar()读取一个字符,putchar()打印一个字符。

ctype.h系列字符函数

ANSI C有一系列标准的函数用来分析字符,ctype.h偷吻检查包含这些函数的原型。这些函数接受一个字符作为参数,如果该参数属于某种特定的类型则返回非零值(真),否则返回零(假)。

以下是字符判断函数

 

 以下是字符映射函数

 注意,映射函数并不改变原始参数,它只返回改变之后的值。

多重选择else if

if(score<1000)
    bonus=0;
else if(score<1500)
    bonus=1;
else if(score<2000)
    bonus=2;
else if(score<2500)
    bonus=4;
else
    bonus=6;

当有众多的else和if时,else和与它最近的一个if相匹配。

逻辑运算符

 仅当exp1和exp2都为真的时候,exp1&&exp2才为真。

如果exp1或exp2为真或两者都为真,exp1||exp2为真。

如果exp1为假,则!exp1为真;并且如果exp1为真,则!exp1为假。

改变拼写法:iso646.h

iso646.h头文件里定义了逻辑运算符的可供选择的拼写法。

包含了这个头文件,就可以用and代替&&,用or代替||,用not代替!。

 优先级

!运算符的优先级高于乘法运算,和增量运算符的优先级相同,仅此于圆括号。&&运算符的优先级高于||,这两者的优先级都低于关系运算符而高于赋值运算符。

求职的顺序

逻辑表达式是从左到右求值的。一旦发现使表达式为假的因素,立即停止求值。

 条件运算符?:

expression1 ? expression2 : expression3

当expression1的值为真,整个表达式的值和expression2的值相同。如果expression1为假,整个表达式的值等于expression3的值。

continue和break

continue语句

#include<stdio.h>
int main(void)
{
const float MIN=0.0f;
const float MAX=100.0f;

float score;
float total=0.0f;
int n=0;
float min=MAX;
float max=MIN;

printf("Enter the first score(q to quit):");
while(scanf("%f",&score)==1)
   {
     if(score<MIN || score>MAX)
        {
          printf("%0.1f is an invalid value.Try again:",score);
          continue;
        }
     printf("Accepting %0.1f: \n",score);
     min=(score<min)?score:min;
     max=(score<max)?score:max;
     total+=score;
     n++;
     printf("Enter next score(q to quit):");
   }
if(n>0)
  {
   printf("Average of %d scores is %0.1f.\n",n,total/n);
   printf("Low=%0.1f,high=%0.1f\n",min,max);
  }
else
printf("No valid scores were entered.\n");
return 0;
}

continue语句可用于三种循环形式。当运行到该语句时,它将导致剩余的迭代部分被忽略,开始下一次迭代。

 break语句

 break语句导致跳出循环。

#include<stdio.h>
int main(void)
{
float length,width;
    
printf("Enter the length of the rectangle:\n");
while(scanf("%f",&length)==1)
  {
    printf("Length=%0.2f:\n",length);
    printf("Enter its \n");
    if(scanf("%f",&width)!=1)
          break;
    printf("Width=%0.2f:\n",width);
    printf("Area=%0.2f:\n",length*width);
    printf("Enter the length of the rectangle:\n");
  }
printf("Done.\n");
return 0;
}

多重选择:switch和break

#include<stdio.h>
#include<ctype.h>
int main(void)
{
    char ch;
    printf("Give me a letter of the alphabet,and i will give");
    printf("an animal name\nbeginning with that letter.\n");
    printf("Please type in a letter; type # to end my act.\n");
    while((ch=getchar())!='#')
    {
      if('\n'==ch)
      continue;
      if(islower(ch))
         swich(ch)
          {
             case 'a':
                    printf("argali,a wild sheep of Asia");
                    break;
             case 'b':
                    printf("babirusa,a wild pig of Malay\n");
                    break;
             case 'c':
                    printf("coati,racoonlike mammal\n");
                    break;
             case 'd':
                    printf("desman,aquatic,molelike critter\n");
                    break;
             case 'e':
                    printf("echidna,the spiny anteater\n");
                    break;
             case 'f':
                    printf("fisher,brownish marten\n");
                    break;
             default:
                    printf("That's a stumper!\n");
          }
          else
               printf("I recognize only lowercase letters.\n");
          while(getchar()!='\n')
                  continue;
          printf("Please type another letter or a #.\n");
    }
   printf("Bye!\n");
   return 0;
}

 此示例程序中当输入dab时,仅处理了第一个字符。产生这种动作的是下面的代码:

while(getchar()!='\n')
     continue;

也可以使用多重case标签。

switch(ch)
{
          case 'a':
          case 'A':a_ct++;
                      break;
....... }

goto语句

goto label;
      .
      .
      .
label: statement
原文地址:https://www.cnblogs.com/hahazexia/p/3041121.html