0% found this document useful (0 votes)
13 views

S02. ExpressionOperatorControl

Uploaded by

Sơn Nguyễn
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

S02. ExpressionOperatorControl

Uploaded by

Sơn Nguyễn
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

C/C++ Programming Techniques

Statements & Expressions

Thanh-Hai Tran

Electronics and Computer Engineering


School of Electronics and Telecommunications
Hanoi University of Science and Technology
1 Dai Co Viet - Hanoi - Vietnam
Concepts

 The statement is used to perform a task in a program:


assign, calculate, read / write data, call a function, ...
 Classify:
 Single command
 printf ("Hello!");
 x = PI * R * R;

 Block of statements in {...}


 Branch statements and conditions: for, if, while,
switch, ...
 Special: blank statement, expression statement,
create label, return,

2020 2
Statements

 Selection statements
 if
 switch
 Iteration statements
 while
 do
 for
 Jump statements
 break
 continue
 goto

2020 3
Selection statements

if, switch

Electronics and Computer Engineering


School of Electronics and Telecommunications
Hanoi University of Science and Technology
1 Dai Co Viet - Hanoi - Vietnam
if statement

 Branching according to the conditions


 Syntax: if (<condition>) <statement>
[else <statement>]
 For example:
if (x! = 0.)
printf ("Tool =% f", 1 / x);
else printf ("No value available");

if (score> current_record)
NewRecord (score);
Don’t confuse == (equality) with =
(assignment)

2020 5
Cascaded If

2020 7
Condition expression

 Condition operator:

 Examples

 Conditional expression make the program shorter but


harder to understand so it’s probably best to avoid
them 9
2020
Conditional expression

2020
switch statement

 When we need to compare an expression against a


series of values to see if one it currently matches.
 A cascaded if could be used for this purpose

2020 12
switch

 Common form of switch statement

2020 14
Controlling expression

 The word switch must be followed by an integer


expression in parentheses
 Characters are treated as integers in C and thus can be
tested in switch statement
 Floating point numbers and strings don’t qualify

2020 15
Case label

 Each case begins with a label of the form

 Constant expression is a ordinary expression except


it cannot contain variables or function call
 Examples
 5 is a constant expression
 5+10 is a constant expression
 n+10 is not a constant expression (unless n is a macro that
represents a constant)
 The constant expression in a case label must evaluate
to an integer or character

2020 16
Case label

 Duplicate case labels are not allowed


 The order of case labels doesn’t matter
 The default case does not need to come last
 Several case labels may precede the same group of
statements
 Thee are no way to write a case label that specifies a
range of values

2020 17
The role of break statement

 Switch is as a computed jump


 A case label is a marker indicating the position within
the switch
 Without break, control will flow from one case into the
next case

2020 18
Loops

while, for, do

Electronics and Computer Engineering


School of Electronics and Telecommunications
Hanoi University of Science and Technology
1 Dai Co Viet - Hanoi - Vietnam
while

 While is the simplest and most fundamental C statement

2020 22
while

 Compound statement

2020 23
Infinite loop

 A while statement won’t terminate if the controlling


expression always has a nonzero value
 Example:

 This will execute forever unless its body contains a


statement that transfer control out of the loop (break, goto,
return)

2020 24
do statement

 Syntax:

 The do statement is quite similar to while statement


 When the do statement is executed, the loop body is
executed first, then the controlling expression is evaluated
 If the value of expression is nonzero, the loop body is
executed gain then the expression is evaluated once more
 If not, the state will terminate

2020 29
Example

 Calculating the number of digits in an integer

2020 30
for statement

 Syntax:

 Without some special cases: the closed while


statement

2020 31
For statement idioms

2020
Jump statement

break; continue; goto

Electronics and Computer Engineering


School of Electronics and Telecommunications
Hanoi University of Science and Technology
1 Dai Co Viet - Hanoi - Vietnam
Break statement

 A break can transfer


control out of a
switch statement
 Break can be used
to jump out of a
while, do, for loop

2020 36
break statement

 Example: check if the number n is a prime

2020 37
break statement

 Break is useful for writing loop in which the exit point


is in the middle of the body than at the beginning or
the end

 Break transfer control out of the innermost enclosing


while/do/for/switch 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

 Nothing, just use ";" to finish


 The empty statement does nothing, has no effect, and
does not take up machine runtime
 Sometimes it is necessary to use to validly terminate a
statement: loops, conditions, labels, ...
 For example:
 for (i=0; i<N; a[i]=b[i++]) ;
 for (i=0; i<10; printf("%d ",i++)) ;
 if (x==y) ;
else x=y;

2020 43
Expressions
 Arithmetic operators
 Assignment operator
 Increment and decrement operator
 Expression evaluation
 Expression statement

Electronics and Computer Engineering


School of Electronics and Telecommunications
Hanoi University of Science and Technology
1 Dai Co Viet - Hanoi - Vietnam
Arithmetic operators

 The operation between 2 integers => results in an integer


 Math operation with at least 1 real number => results in
real numbers
 Operator % requires integer operands
 C uses operator precedence rules: 2+3*4-1

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

 Increase or decrease 1 unit : a++ a-- ++a --a


 The operand must be a variable of type integer
 a ++ and a-- perform calculations with the value of a and then
increase / decrease the value of a, while ++ a and --a do the
opposite
 a++ * 2;

 ++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

 C Programming, A modern approach, chapter 5,


chapter 6
 Slide, C/C++ programming technique, Nguyen Thanh
Binh
 Slide, C/C++ programming technique, Dao Trung Kien

2020 55

You might also like