daily notes[28]
摘要:本文介绍了C语言中的基本运算符和循环结构。运算符部分包括算术运算符(+、-、*、/、%),其中整数除法会截断小数部分,浮点数除法保留小数;自增++和自减--运算符的前后缀区别;以及sizeof运算符的用法。
·
文章目录
operatons
- the fundamental operators such as
+,-,*in C language can be simply manipulated.
int sum = 5 + 3; // sum = 8
int difference = 10 - 4; // difference = 6
int product = 6 * 7; // product = 42
the operator / was left to introduce just a moment because that it’s special as follows.
5/2=2
5.0/2.0=2.5
5/2.0=2.5
int quotient = 15 / 4; // quotient = 3 (integer division)
float result = 15.0 / 4; // result = 3.75
the operator % can get the remainder after division
int remainder = 15 % 4; // remainder = 3
++accomplishes the operation of increasing by 1 by itself
int x = 1;
int y= x++;
as similar principle, -- apply the compuation of decreasing by 1 by itself.
int x = 1;
int y= x--;
--x and ++x mean that the final result is returned affter these operation have finished.
3. The sizeof operator is a compile-time unary operator that returns the size (in bytes) of a variable, data type, or expression.
sizeof(type)
sizeof(expression)
sizeof variable_name
loops
- Executes a block of code as long as a condition is true.
int i = 0;
while (i < 5) {
printf("%d ", i);
i++;
}
// Output: 0 1 2 3 4
- do-while loop:as similar as
while,the condition of loop which is true decides to continue loop but there is an obvious difference that the do-while loop inspects the loop’s condition after finishing the block of the loop one time ,no matter whether the condition was met .
int i = 0;
do {
printf("%d ", i);
i++;
} while (i < 5);
// Output: 0 1 2 3 4
- for Loop
for (int i = 0; i < 5; i++) {
printf("%d ", i);
}
// Output: 0 1 2 3 4
- break - Exits the loop immediately
for (int i = 0; i < 10; i++) {
if (i == 5) break;
printf("%d ", i);
}
// Output: 0 1 2 3 4
- continue - Skips the current iteration and continues with the next
for (int i = 0; i < 5; i++) {
if (i == 2) continue;
printf("%d ", i);
}
// Output: 0 1 3 4
references
- deepseek
更多推荐


所有评论(0)