[C语言]流程控制, 复合赋值, 优先级, 循环控制

----------------------------------------------------------------------------------------

//单一判断
if(a < 0) {
  printf("小于0");
}

//else
if(a < 0) {
  printf("小于0");
} else {
  printf("不小于0");
}

//if嵌套
if(a < 0) {
  if(a < -5) {
    printf("小于-5");
  } else {
    printf("不小于-5");
  }
} else {
  printf("不小于0");
}

//级联if else if
if(a < 0) {
  printf("小于0");
} else if(a < -5) {
  printf("小于-5");
} else if(a < -10) {
  printf("小于-10");
} else {
  printf("不小于0");
}

//级联if else if 另一种写法(推荐写法:单一出口)
int sign = 0;
if(a < 0) {
  t = -1;
} else if(a < -5) {
  t = -6;
} else if(a < -10) {
  t = -11;
} else {
  t = 1;
}
printf("%d", t);

//多路分支
switch(a) {
    case 1:
        printf("1");
    break;
    case 2:
        printf("2");
    break;
    default:
        printf("other");
}
Tips: 1. 不省略大括号;
    2. 赋值运算符与相等关系运算符在判断时注意不要用错

 

++与--

#include <stdio.h>

int main() {

  int a;

  a = 2;

  printf("a++=%d \n", a++);   //2

  printf("%d \n", a);       //3

  printf("++a=%d \n", ++a);   //4

  printf("%d \n", a);       //4

  return 0;
}

注:不管是a++还是++a,最后a的值都为a+1;区别是a++是一个表达式,结果为a+1前的结果;++作为前缀,++a的结果是a+1后的结果。

 

while与do..while

int a = 0;
scanf("输入一个数:%d", &a);
while(a > 0) {
  printf("a");
  a--;
}
printf("a");

do{
  printf("a");
  a--;
}while(a > 0);

 

猜数游戏

//
//  main.c
//  demo11
//
//  Created by weichen on 14/12/22.
//  Copyright (c) 2014年 weichen. All rights reserved.
//

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(int argc, const char * argv[]) {
    //猜数
    srand(time(0));
    int a = rand()%100 + 1;
    int number;
    int count = 0;    
    
    do{
        printf("请输入1-100的数:");
        scanf("%d", &number);
        
        count++;
        
        if(a > number) {
            printf("您输的数小了!\n");
        }else if(a < number){
            printf("您输的数大了!\n");
        }else{
            printf("恭喜,您在第%d次猜对了!", count);
        }
    }while(a != number);

    return 0;
}

 

for循环

#include <stdio.h>

int main(int argc, const char * argv[]) {
    //求阶乘
    int n;
    
    printf("请输入一个数:");
    
    scanf("%d", &n);
    
    int i = 1;
    int total = 1;
    /*
    while(i <= n) {
        total *= i;
        i++;
    }
    */
    
    /*
    for(i; i<=n; i++) {
        total *= i;
    }
    */
    
    for(int i=2; i<=n; i++) {
        total *= i;
    }  
    
    /*
    int temp = n;
for(n; n>=i; n--) { total *= n; } printf("%d的阶乘为%d", temp, total);
*/ printf("%d的阶乘为%d", n, total); return 0; }

 

优先级

()
++ -- + - !
* / %
+ -
< <= > >=
== !=
&&
||
*= /= %= += -= =

 

循环控制

#include <stdio.h>

int main(int argc, const char * argv[]) {
    // 用1角,2角,5角凑成5元的所有的方式
    int one = 0;
    int two = 0;
    int five = 0;
    for(one = 1; one < 50; one++) {
        for(two = 1; two < 50/2; two++) {
            for(five = 1; five < 50/5; five++) {
                if(one + two*2 + five*5 == 50) {
                    printf("%d个1角加%d个2角加%d个5角可以凑成5元!\n", one, two, five);
                }
            }
        }
    }
    
    return 0;
}
//上述的通用形式
#include <stdio.h>

int main(int argc, const char * argv[]) {
    // 用1角,2角,5角凑成x元的方式
    int one = 0;   //1角个数
    int two = 0;   //2角个数
    int five = 0;  //5角个数
    int x = 0;    //钱数
    int sign = 0;  //凑足标记

    printf("请输入:");
    scanf("%x", &x);

    for(one = 1; one < x*10; one++) {
        for(two = 1; two < x*10/2; two++) {
            for(five = 1; five < x*10/5; five++) {
                if(one + two*2 + five*5 == 50) {
                    printf("%d个1角加%d个2角加%d个5角可以凑成%x元!\n", one, two, five, x);
                    sign = 1;
                }
                if(sign == 1) {
                    goto out;  //跳至指定位置(出循环,只输出一次满足需求的方式)
                }
            }
        }
    }
    
    out:

    return 0;
}   

注:break跳出当前循环,不包括父级循环;continue跳过本次循环,继续下次循环

 

Refer:详解C流程控制

Link: http://www.cnblogs.com/farwish/p/4172901.html

posted on 2014-12-22 01:19  ercom  阅读(751)  评论(1编辑  收藏  举报