1. If statement:
If
A statement is a conditional statement that is used to execute different blocks of code depending on whether a condition is true or false. Its basic form is as follows:
if (条件) {
// 条件为真时执行的代码
} else {
// 条件为假时执行的代码
}
Write a basic If
sentence
#include<stdio.h>
int main(){
int x = 10;
if (x > 5){
printf("x 大于 5\n");
} else{
printf("x 不大于 5\n");
}
}
operation result:
Example 1: Enter an integer. If the number is greater than 80, then output "Xiao Zhai is handsome"; if the number is not greater than 80, then output "Xiao Zhai is handsome".
#include <stdio.h>
int main() {
int a;
scanf("%d", &a);
if (a > 80) {
printf("小翟帅\n");
} else {
printf("小翟贼帅\n");
}
return 0;
}
Running results (take input 88 as an example):
Example 2: There is a function
, please write a program. When an integer x is input, the corresponding y value will be output.
#include <stdio.h>
int main() {
int x, y;
scanf("%d", &x);
if (x < 0) {
y = -1;
} else if (x > 0) {
y = 1;
} else {
y = 0;
}
printf("x=%d, y=%d\n", x, y);
return 0;
}
Running results (take input 2 as an example):
Example 3: Input two real numbers a and b, and output these two numbers in order from small to large.
#include <stdio.h>
int main() {
double a, b, t;
scanf("%lf, %lf", &a, &b);
if (a > b) {
t = a;
a = b;
b = t;
}
printf("%f, %f\n", a, b);
return 0;
}
Running results (take input 2, 11 as an example):
Example 5: Input three real numbers a, b, c, and output these three numbers in order from small to large.
#include <stdio.h>
int main() {
double a, b, c, t;
scanf("%lf, %lf, %lf", &a, &b, &c);
if (a > b) {
t = a;
a = b;
b = t;
}
if (a > c) {
t = a;
a = c;
c = t;
}
if (b > c) {
t = b;
b = c;
c = t;
}
printf("%f, %f, %f\n", a, b, c);
return 0;
}
Running results (take input 7, 11, 1 as an example):
2. Common expression forms:
There are many common expression forms in C language, including arithmetic expressions, relational expressions, logical expressions, etc. These expressions are used to perform various calculations and comparison operations. For example:
- Arithmetic expression:
a + b
,x * y
,z / 2
- Relational expression:
x == y
,a < b
,c >= d
- Logical expression:
(x > 5) && (y < 10)
,(a == b) || (c != d)
the following is the example you provided, which has been typeset and output according to the correct C language format:
Example 1: There is a function,
please write a program, when an integer x is input, the corresponding y value is output. The first way of editing:
#include <stdio.h>
int main() {
int x, y;
scanf("%d", &x);
if (x < 0) {
y = -1;
} else if (x > 0) {
y = 1;
} else {
y = 0;
}
printf("x=%d, y=%d\n", x, y);
return 0;
}
Running results (take input 1 as an example):
The second way of editing:
#include <stdio.h>
int main() {
int x, y;
scanf("%d", &x);
if (x < 0) {
y = -1;
} else if (x == 0) {
y = 0;
} else {
y = 1;
}
printf("x=%d, y=%d\n", x, y);
return 0;
}
Running results (take input 2 as an example):
Example 2: Please compile a program to determine whether a certain year is a leap year.
#include <stdio.h>
int main() {
int a;
printf("请输入年份\n");
scanf("%d", &a);
if ((a % 100 != 0 && a % 4 == 0) || a % 400 == 0) {
printf("%d 年是闰年\n", a);
} else {
printf("%d 年不是闰年\n", a);
}
return 0;
}
Running results (taking input 2023 as an example):
Example 3: Enter a character, determine whether it is an uppercase letter, if so, convert it to a lowercase letter, if not, do not convert it, and then output the final character.
#include <stdio.h>
int main() {
char ch;
scanf("%c", &ch);
if (ch >= 'A' && ch <= 'Z') {
ch = ch + 32;
}
printf("%c\n", ch);
return 0;
}
Running results (take input a as an example):
3. Expression 1? Expression 2: Expression 3:
This is the conditional operator (also known as the ternary operator) in C language. It allows the selection of different expressions to be executed depending on whether the condition is true or false. Its syntax is as follows:
条件 ? 表达式1 : 表达式2
If the condition is true, the value of expression1 is returned; if the condition is false, the value of expression2 is returned. For example:
int x = 10;
int y = (x > 5) ? 20 : 30;
In this example, since x
it is greater than 5, y
the value of will be 20.
Here are the two examples you provided, formatted and output in the correct C language format:
Example 1: Enter a character, determine whether it is an uppercase letter, if so, convert it to a lowercase letter, if not, do not convert it, and then output the final character. The first way of editing:
#include <stdio.h>
int main() {
char ch;
scanf("%c", &ch);
if (ch >= 'A' && ch <= 'Z') {
ch = ch + 32;
}
printf("%c\n", ch);
return 0;
}
Running results (take input a as an example):
Example 1: The second way of editing:
#include <stdio.h>
int main() {
char ch;
scanf("%c", &ch);
ch = (ch >= 'A' && ch <= 'Z') ? (ch + 32) : ch;
printf("%c\n", ch);
return 0;
}
Running results (take input a as an example):
Example 2: Input two real numbers a and b, and output these two numbers in order from small to large. The first way of editing:
#include <stdio.h>
int main() {
double a, b, t;
scanf("%lf, %lf", &a, &b);
if (a > b) {
t = a;
a = b;
b = t;
}
printf("%f, %f \n", a, b);
return 0;
}
Running results (take input 5 and 8 as an example):
Example 2: The second way of editing:
#include <stdio.h>
int main() {
double a, b;
scanf("%lf, %lf", &a, &b);
a > b ? printf("%f, %f \n", b, a) : printf("%f, %f\n", a, b);
return 0;
}
Running results (take input 8 and 7 as an example):
4. Switch statement:
Switch
A statement is a multi-branch conditional statement that is used to select and execute different branches based on the value of an expression. Its basic form is as follows:
switch (表达式) {
case 值1:
// 当表达式等于值1时执行的代码
break;
case 值2:
// 当表达式等于值2时执行的代码
break;
// 更多 case 分支
default:
// 如果表达式不等于任何一个值时执行的代码
}
Switch
Each case
branch of the statement represents a possible value, and when the value of the expression matches one case
, the code for that branch is executed. If no match is found case
, default
branch (optional). For example:
int day = 3;
switch (day) {
case 1:
printf("星期一\n");
break;
case 2:
printf("星期二\n");
break;
// 更多 case 分支
default:
printf("其他\n");
}
Example 1: The grades of a certain course were originally graded A, B, C, and D, and now they need to be converted into percentile grades.
#include <stdio.h>
int main() {
char dengji;
scanf("%c", &dengji);
switch (dengji) {
case 'A':
printf("该生分数为 85~100\n");
break;
case 'B':
printf("该生分数为 70~84\n");
break;
case 'C':
printf("该生分数为 60~69\n");
break;
case 'D':
printf("该生分数为<60\n");
break;
default:
printf("该生的等级输得不对!\n");
break;
}
return 0;
}
Running results (take input A as an example):
Example 2: The grades of a certain course were originally graded A, B, C, and D, and now they need to be converted into percentile grades.
#include <stdio.h>
int main() {
char dengji;
scanf("%c", &dengji);
switch (dengji) {
case 'A':
case 'B':
printf("该生分数为 70~100\n");
break;
case 'C':
case 'D':
printf("该生分数为<70\n");
break;
default:
printf("该生的等级输得不对!\n");
break;
}
return 0;
}
Running results (take input A as an example):
Example 3: The grade of a certain course was originally a hundred-point system, and now it needs to be converted into a grade.
#include <stdio.h>
int main() {
double fenshu;
printf("请输入分数:");
scanf("%lf", &fenshu);
switch ((int)(fenshu / 10)) {
case 10:
case 9:
printf("该生等级为 A\n");
break;
case 8:
printf("该生等级为 B\n");
break;
case 7:
printf("该生等级为 C\n");
break;
case 6:
printf("该生等级为 D\n");
break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0:
printf("该生等级为 E\n");
break;
default:
printf("你特么输的是成绩?!\n");
break;
}
return 0;
}
Running results (take input 1 as an example):