S02. ExpressionOperatorControl
S02. ExpressionOperatorControl
Thanh-Hai Tran
2020 2
Statements
Selection statements
if
switch
Iteration statements
while
do
for
Jump statements
break
continue
goto
2020 3
Selection statements
if, switch
if (score> current_record)
NewRecord (score);
Don’t confuse == (equality) with =
(assignment)
2020 5
Cascaded If
2020 7
Condition expression
Condition operator:
Examples
2020
switch statement
2020 12
switch
2020 14
Controlling expression
2020 15
Case label
2020 16
Case label
2020 17
The role of break statement
2020 18
Loops
while, for, do
2020 22
while
Compound statement
2020 23
Infinite loop
2020 24
do statement
Syntax:
2020 29
Example
2020 30
for statement
Syntax:
2020 31
For statement idioms
2020
Jump statement
2020 36
break statement
2020 37
break statement
2020 38
continue statement
2020 39
goto statement
A goto statement in C
programming provides an
unconditional jump from the
'goto' to a labeled statement
in the same function
Use of goto statement is
highly discouraged in any
programming language
because it makes difficult to
trace the control flow of a
program, making the goto label;
program hard to understand ..
and hard to modify .
label: statement;
2020 40
goto statement
2020
Examples
Label: used to mark a place in source code
The goto statement: jumps to the position of the label and
continues
printf ("Enter now:");
scanf ("% d", & now);
if (now <1 || now> 31) goto error;
printf ("Enter ladder:");
scanf ("% d", & thang);
if (scale <1 || thang> 12) goto error;
/ * ... * /
error:
printf ("Error inputting data!");
There must be at least one statement after the label (empty
statements can be used if needed).
The goto statement breaks the structure, so it should be used
2020 limited 42
Null statement
2020 43
Expressions
Arithmetic operators
Assignment operator
Increment and decrement operator
Expression evaluation
Expression statement
2020 46
Assignment operators
Simple operator: a = b
Compound assignment:
Assignment Similar Assignment Similar
a += b a = a + b a &= b a = a & b
a -= b a = a – b a |= b a = a | b
a *= b a = a * b a ^= b a = a ^ b
a /= b a = a / b a <<= b a = a << b
a %= b a = a % b a >>= b a = a >> b
Some examples:
x = y = 15;
if ((c = getchar()) == 'y' || c == 'n') { … }
if (a = b) { … } /* vẫn dịch được dù viết nhầm */
2020 47
Increment and decrement operator
++a * 2;
a++ + a++;
2020 49
Logic and comparison operators
Logic:
and: A && B
or: A || B
not: !A
Comparison:
x == y
x != y
x > y
x < y
x >= y
x <= y
2020 51
Bit operators
Include:
and: a & b
or: a | b
xor: a ^ b
not: ~a
Dịch trái, phải: a<<b a>>b
Apply only to integer operands
Attention: do not confuse to logic operators
2020 52
Other operators
Unary addition / subtraction: +a, -a
sizeof(): size of type/variable/constant
Operator (): (a+b)*c
Operator “,”: do nothing, regardless the result
x++, y = x*2;
for (i=2, j=3, x=1; i<10; i+=2, j+=3, x++) {…}
Access to element of an array: a[4]
Pointer: *a
Address: &a
Take an attribute: sv.ten, b->tuoi
Type casting: (int)a
Function call (): sin(x), pow(x, 3)
Condition: <condition> ? <expression 1> : <expression 2>
biggest = a > b ? a : b;
2020 53
References
2020 55