C Learning 2 - Operators & Control Statements

Made by Mike_Zhang


Unfold C/C++ Topics | 展开 C/C++ 主题 >


1. Operators

1.1 Assignment Operator

Operator Description Example
= Assigns the value at its right to the variable at its left. c = a + b assigns value of a + b into c
+= Adds the value at its right to the variable at its left and assigns the result to the variable at its left. c += a is equivalent to c = c + a
-= Subtracts the value at its right from the variable at its left and assigns the result to the variable at its left. c -= a is equivalent to c = c - a
*= Multiplies the variable at its left by the value at its right and assigns the result to the variable at its left. c *= a is equivalent to c = c * a
/= Divides the variable at its left by the value at its right and assigns the result to the variable at its left. c /= a is equivalent to c = c / a
%= Divides the variable at its left by the value at its right and assigns the remainder to the variable at its left. c %= a is equivalent to c = c % a
<<= Shifts the variable at its left left by the number of bits specified by the value at its right and assigns the result to the variable at its left. c <<= 2 is equivalent to c = c << 2
>>= Shifts the variable at its left right by the number of bits specified by the value at its right and assigns the result to the variable at its left. c >>= 2 is equivalent to c = c >> 2
&= Performs a bitwise AND operation between the variable at its left and the value at its right and assigns the result to the variable at its left. c &= a is equivalent to c = c & a
^= Performs a bitwise exclusive OR operation between the variable at its left and the value at its right and assigns the result to the variable at its left. c ^= a is equivalent to c = c ^ a
|= Performs a bitwise inclusive OR operation between the variable at its left and the value at its right and assigns the result to the variable at its left. c |= a is equivalent to c = c|a

1.2 Arithmetic Operators

Operator Description Example (a = 10, b = 20)
+ (binary) Adds the value at its right to the value at its left. a + b will give 30
- (binary) Subtracts the value at its right from the value at its left. a - b will give -10
+ (unary) Indicate or to change the algebraic sign of a value. +a will give 10
- (unary) Indicate or to change the algebraic sign of a value. -a will give -10
* Multiplies the value at its left by the value at its right. a * b will give 200
/ Divides the value at its left by the value at its right. The answer is truncated if both operands are integers. b / a will give 2
% Yields the remainder when the value at its left is divided by the value to its right (integers only). b % a will give 0
++ Adds 1 to the value of the variable to its right (prefix mode) or to the value of the variable to its left (postfix mode). a++ will give 11
-- Like ++, but subtracts 1. a-- will give 9

1.3 Relational Operators

Operator Description Example (a = 10, b = 20)
== Is equal to (a == b) is false
!= Is not equal to (a != b) is true
> Is greater than (a > b) is false
< Is less than (a < b) is true
>= Is greater than or equal to (a >= b) is false
<= Is less than or equal to (a <= b) is true

1.4 Logical Operators

Operator Description Example (a = 1, b = 0)
&& Logical AND (a && b) is false
|| Logical OR (a || b) is true
! Logical NOT !b is true

1.5 Bitwise Operators

Operator Description Example (a = 60 (0011 1100), b = 13 (0000 1101))
& Bitwise AND (a & b) will give 12, which is 0000 1100
Bitwise OR (a | b) will give 61, which is 0011 1101
^ Bitwise XOR (a ^ b) will give 49, which is 0011 0001
~ Bitwise NOT ~a will give -61, which is 1100 0011 in 2’s complement form due to a signed binary number.
<< Bitwise left shift a << 2 will give 240, which is 1111 0000
>> Bitwise right shift a >> 2 will give 15, which is 0000 1111

1.6 Operators Precedence

Rank Category Operator Associativity
1 Postfix () [] -> . ++ - - Left to right
2 Unary + - ! ~ ++ - - (type) * & sizeof Right to left
3 Multiplicative * / % Left to right
4 Additive + - Left to right
5 Shift << >> Left to right
6 Relational < <= > >= Left to right
7 Equality == != Left to right
8 Bitwise AND & Left to right
9 Bitwise XOR ^ Left to right
10 Bitwise OR Left to right
11 Logical AND && Left to right
12 Logical OR || Left to right
13 Conditional ?: Right to left
14 Assignment = += -= *= /= %= <<= >>= &= ^= |= Right to left
15 Comma , Left to right

2. Expressions & Statements

Expression: a combination of operators and operands.
Statement: a command to the computer.

  • Declaration statement:
1
int toes;
  • Assignment statement:
1
toes = 12;
  • Function call statement:
1
printf("%d\n", toes);
  • Structured statement:
1
while (toes < 20) toes = toes + 2;
  • Return statement:
1
return 0;
  • null statement:
1
; /* does nothing */

3. Control Statements

3.1 Loop

3.1.1 while Loop

1
2
3
while (expression) {
statement
}

The statement portion is repeated until the expression becomes false or 0.


3.1.2 for Loop

1
2
3
for (initialize ; test ; update) {
statement
}

The loop is repeated until test becomes false or zero.


3.1.3 do-while Loop

  • The while loop and the for loop are both entry-condition loops. The test condition is checked before each iteration of the loop, so it is possible for the statements in the loop to never execute.

do-while loop

  • An exit-condition loop, in which the condition is checked after each iteration of the loop, guaranteeing that statements are executed at least once.
1
2
3
do {
statement
} while (expression);

The statement portion is repeated until the expression becomes false or zero.

[Example]

1
2
3
4
do {
scanf("%d", &number);
}
while (number != 20);

Entry-condition loop vs. an exit-condition loop

  • Your answer should usually be an entry-condition loop.
      1. The general principle that it is better to look before you leap (or loop) than after.
      1. A program is easier to read if the loop test is found at the beginning of the loop.
      1. In many uses, it is important that the loop be skipped entirely if the test is not initially met.

for ( ;test; ) $\iff$ while (test)

1
2
3
4
5
initialize; 
while (test) {
body;
update;
}

is the same as

1
2
3
for (initialize; test; update) {
body;
}

3.1.4 continue Statement

  • It causes the rest of an iteration to be skipped and the next iteration to be started.
  • If the continue statement is inside nested structures, it affects only the innermost structure containing it.

3.1.5 break Statement

  • It causes the program to break free of the loop that encloses it and to proceed to the next stage of the program.
  • If the break statement is inside nested loops, it affects only the innermost loop containing it.

3.1.6 goto Statement

In principle, you never need to use the goto statement in a C program.

  • It causes program control to jump to a statement bearing the indicated label.
  • A colon is used to separate a labeled statement from its label. Label names follow the rules for variable names.
  • The labeled statement can come either before or after the goto.
1
2
3
4
5
goto label; 

// ...

label : statement

[Example]

1
2
3
4
5
6
7
8
if (size > 12) {
goto a;
}
goto b;

a: cost = cost * 1.05;
flag = 2;
b: bill = cost * flag;

As same as

1
2
3
4
5
if (size > 12) {
cost = cost * 1.05;
flag = 2;
}
bill = cost * flag;

3.2 Branch

3.2.1 if Statement

1
2
3
if (expression) {
statement
}

The statement is executed if the expression is true.


1
2
3
4
5
6
if (expression) {
statement1
}
else {
statement2
}

If the expression is true, statement1 is executed. Otherwise, statement2 is executed.


1
2
3
4
5
6
7
8
9
if (expression) {
statement1
}
else if (expression2) {
statement2
}
else {
statement3
}
  • If expression1 is true, statement1 is executed.
  • If expression1 is false but expression2 is true, statement2 is executed.
  • Otherwise, if both expressions are false, statement3 is executed.

3.2.2 Conditional Expression

1
expression1 ? expression2 : expression3

Conditional Operator: ?:

If expression1 is true (nonzero), the whole conditional expression has the same value as expression2.
If expression1 is false (zero), the whole conditional expression has the same value as expression3.

[Example]

Setting a variable equal to the maximum of two values:

1
max = (a > b) ? a : b;

3.2.3 switch Statement

Program control jumps to the case label bearing the value of expression.

  • Program flow then proceeds through all the remaining statements unless redirected again with a break statement.
  • Both expression and case labels must have integer values (type char is included), and the labels must be constants or expressions formed solely from constants.
  • If no case label matches the expression value, control goes to the statement labeled default, if present. Otherwise, control passes to the next statement following the switch statement.
1
2
3
4
switch (expression) {
case label1 : statement1 // use break to skip to end
case label2 : statement2
default : statement3 }

break:

  • It causes the program to break out of the switch and skip to the next statement after the switch.
  • Without the break statement, every statement from the matched label to the end of the switch would be processed.

[Example]

1
2
3
4
5
6
7
8
switch (choice) { 
case 1 :
case 2 : printf("Darn tootin'!\n");
break;
case 3 : printf("Quite right!\n");
case 4 : printf("Good show!\n");
break;
default : printf("Have a nice day.\n"); }

If choice has the integer value 1 or 2, the first message is printed.
If it is 3, the second and third messages are printed. (Flow continues to the following statement because there is no break statement after case 3.)
If it is 4, the third message is printed.
Other values print only the last message.


References

C - Operators - Tutorialspoint, Tutorialspoint.com, 2019. https://www.tutorialspoint.com/cprogramming/c_operators.htm

S. Prata, C primer plus. Upper Saddle, Nj: Addison-Wesley, 2014.


原创文章,转载请标明出处
Made by Mike_Zhang




感谢你的支持 | Thank you for supporting

C Learning 2 - Operators & Control Statements
https://ultrafish.io/post/c-learning-2/
Author
Mike_Zhang
Posted on
December 24, 2022
Licensed under