1、if语句
#include <stdio.h>
int main()
{
int x;
scanf("%d", &x);
int f=0;
if ( x < 0 ) {
f = -1;
} else if ( x == 0 ) {
f = 0;
} else if ( x >5 ) {
f = 2 * x;
} else {
f = 3*x;
}
printf("%d
", f);
return 0;
}
2、switch..case语句
#include <stdio.h>
int main()
{
printf("输入成绩(0-100)");
int grade;
scanf("%d", &grade);
grade /=10;
switch ( grade ) {
case 10:
case 9:
printf("A
");
break;
case 8:
printf("B
");
break;
case 7:
printf("C
");
break;
case 6:
printf("D
");
break;
default:
printf("F
");
break;
}
return 0;
}
#include <stdio.h>
int main()
{
printf("请输入月份:");
int month;
scanf("%d", &month);
switch ( month )
{
case 1: printf("January
"); break;
case 2: printf("February
"); break;
case 3: printf("March
"); break;
case 4: printf("April
"); break;
case 5: printf("May
"); break;
case 6: printf("June
"); break;
case 7: printf("July
"); break;
case 8: printf("August
"); break;
case 9: printf("September
"); break;
case 10:printf("October
"); break;
case 11:printf("November
"); break;
case 12:printf("December
"); break;
}
return 0;
}