分支语句

年龄问题

第一种
#include <stdio.h>

int main ()
{
   int age = 45;
  if(age<18)
  printf ("未成年\n");
  else if (age >=18 && age <28)
  printf ("青年\n");
  else if(age >=28 && age <50)
  printf ("壮年\n");
  else if(age >=58 && age <90)
  printf ("老年\n");
  else
  printf ("期颐\n");


  return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
第二种
#include <stdio.h>

int main ()
{
   int age = 45;
  if(age<18)
  printf ("未成年\n");
  else 
       {if (age >=18 && age <28)
       printf ("青年\n");
       else if(age >=28 && age <50)
       printf ("壮年\n");
       else if(age >=58 && age <90)
       printf ("老年\n");
       else 
       printf ("期颐\n");
  }

  return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.

100以内奇数

第一种

#include <stdio.h>

int main ()
{
  int i =1;
  while(i<=100)
  {
    if(i%2!=0)//i%2==1
    printf ("%d ",i);
    i++;
  }

  return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.


switch语句

输出星期

#include <stdio.h>

int main ()
{
   int day = 0;
   scanf ("%d",&day);
   switch (day)
   {
   case 1:
       printf("星期一\n");
       break;
   case 2:
       printf("星期二\n");
       break;
   case 3:
       printf("星期三\n");
       break;
   case 4:
       printf("星期四\n");
       break;
   case 5:
       printf("星期五\n");
       break;
   case 6:
       printf("星期六\n");
       break;
   case 7:
       printf("星期七\n");
       break;
   default:
    break;
   }



  return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.

工作日weekday和周末weekend

#include <stdio.h>

int main ()
{
   int day = 0;
   scanf ("%d",&day);
   switch (day)
   {case 1:
    case 2:
    case 3:
    case 4:
    case 5:
       printf("weekday\n");
       break;
    case 6:
    case 7:
       printf("weekend\n");
       break;
  defaut ://以上情况都不能输出是才输出这条
  printf("error\n");
  break//有break才跳出循环没有就继续往下执行且只用于结束所在switch内的程序

   }



  return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.

循环语句

while循环

break在while循环中的作用:

其实在循环中只要遇到break,就停止后期的所有循环,直接终止循环。所以while中的break是用于永久终止循环的。

continue在while循环中的作用:

continue是用于终止本次循环的,也就是本次循环中continue后面的代码不会再执行,而是直接跳转到while语句的判断部分。进行下一次循环的入口判断。

#include <stdio.h>

int main ()
{
   int i =1;
   while (i<=10)
   {
    if (i==5)
      continue;
      printf("%d",i);
      i++;
   }
  

  return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.

结果:

1234 加上一个循环

#include <stdio.h>

int main ()
{
   int i =0;
   while (i<=10)
   {
    i++;
    if (i==5)
      continue;
      printf("%d",i);

   }
  

  return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.

结果123467891011

getchar (从键盘上输入一个字符串)和putchar(输出一个字符串)

#include <stdio.h>

int main ()
{
   int ch =getchar();
   putchar(ch);
   printf("%c\n",ch);


  return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

结果

e

ee

for循环

for(初始化;判断;调整)

生成数字1到10
#include <stdio.h>
int main ()
{
  int i=0;
  for(i=1;i<=10;i++)
  {
    printf("%d ",i);
  }  
   return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

不可以在for循环体内改变循环变量,防止for循环失去控制

取值建议前闭后开

for变种

无线循环hehe

#include <stdio.h>
int main ()
{
  for(;;)
  {
    printf ("hehe");
  }
   return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

for循环的初始化,判断,调整都可以省略

for循环的额判断部分被省略就是恒为真

do  while循环

do(后面不加大括号是只能写一条语句,超过一条加大括号)

  循环语句;

while(表达式);

至少执行一次

do  while输出1-10

1.i++在do的大括号外面是无限循环1

#include <stdio.h>
int main ()
{
  int i=1;
  do
  {
    printf("%d",i);
  }
   while (i<=10);
    i++;
   return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

2.i++在do的大括号里面是1到10

#include <stdio.h>
int main ()
{
  int i=1;
  do
  {
    printf("%d ",i);
     i++;
  }
   while (i<=10);
    i++;
   return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

do while也可以加break和continue