1. If...
1. General form:
if (expression) {statement;}
Expression:
a, use a non-zero value to represent true, and use 0 to represent false;
b, if(flag) is equivalent to if(1==flag)
c, floating-point numbers cannot be compared with 0, only approximate values can be used for comparison; : 1e-6 is equal to 1x10 to the -6th power and can be used as 0;
2. Used for single branch selection structure;
3. If there is a cross relationship, use a parallel if statement;
Example 1: Output the maximum value of two integers
#include <stdio.h>
void main()
{
int a,b;
printf("请输入两个整数:");
scanf("%d %d",&a,&b);
if(a>b)
{
printf("max = %d\n",a);
}
if(a<b)
{
printf("max = %d\n",b);
}
}
二、if…else
- General form:
if(表达式) {语句1; } else { 语句2;}
- Conditional statements for double branch control;
- When used in an either-or relationship;
Example 2: Output the maximum value of two integers
Example 2 Regarding the inspection and handling of illegal characters
#include <stdio.h>
void main()
{
int a,b,max,data;
printf("Input a,b:");
data = scanf(" %d,%d",&a,&b);/*记录scanf()的返回值;*/
if(data!=2)/*根据scanf()的返回值判断输入数据个数或者格式是否错误*/
{
printf("格式输入错误!");
fflush(stdin);/*清除输入缓冲区中的错误内容(fflush()可能带来可移植性的问题)*/
}
else
{
max = a>b?a:b;/*三目运算符(a>b值为真则输出a的值,反之输出b的值)*/
printf("%d\n",max);
}
}
三、if…else if… … else if…else
-
General form:
if(expression 1) {statement 1;}
else if(expression 2) {statement 2;}
…
else if(expression n) {statement n;}
else{statement n+1;} -
Conditional statements for multi-branch control;
Example 3: Determine the character
#include <stdio.h>
void main()
{
char ch;
printf("请输入一个字符:");
ch = getchar();/*getchar、putchar专门用于字符输入输出;
getchar()写法上要写为 变量 = getchar();
putchar()用法为putchar(变量);
putchar('\n')输出一个控制符;
putchar('字母/字符');输出字母/字符;
*/
if(ch<=31)
{
printf("这是一个控制字符或通讯专用字符!\n");
}
else if(ch >= '0' && ch <= '9')
{
printf("这是一个数字!\n");
}
else if(ch >= 'A' && ch <= 'Z')
{
printf("这是一个大写字母!\n");
}
else if(ch >= 'a' && ch <= 'z')
{
printf("这是一个小写字母!\n");
}
else
{
printf("这是其他字符!\n");
}
}
四、switch() case: …case:… … default:…
- General form:
switch(expression)
{ case constant 1: statement 1; case constant 2: statement 2; … case constant n: statement n; default: statement n+1; } - Sentences used for multiple selection;
The switch statement is equivalent to multiple if-else statements;
(expression) can only be char or int;
there must be at least one space after the case, and the colon
(expression) after the constant must be consistent with the constant type;
remember to remember When you need to jump out, add break after the statement;
Example 4: Simple calculation of addition, subtraction, multiplication and division
#include <stdio.h>
void main()
{
double a,b;
char ch;
printf("Input a(+ - * /)b:");
scanf("%f%c%f",&a,&ch,&b);
switch(ch)
{
case '+':
printf("%f%c%f=%.2f\n",a,ch,b,a+b);
break;
case '-':
printf("%f%c%f=%.2f\n",a,ch,b,a-b);
break;
case '*':
case 'X':
case 'x':
printf("%f%c%f=%.2f\n",a,ch,b,a*b);/*输入"x" "X" "*" 都执行这一条语句;不加break,会顺语句执行*/
break;
case '/':
printf("%f%c%f=%.2f\n",a,ch,b,a/b);
break;
default:
printf("请输入正确算式!\n");
}
}
If there are any errors in this article, please leave a comment and correct me.