Title: Chapter 3 Flow Control
1Chapter 3 Flow Control
- By C. Shing
- ITEC Dept
- Radford University
2Objectives
- Understand how to use assignment statement
- Understand how to use conditional statement
- Understand how to use loop statement
3Assignment Statement
- Identifier assignment_operator expression
- Represents
- identifier identifier assignment_operator
- (expression)
- After evaluating the right hand side of ,
- it stores result to the left hand side
identifier - (i.e. memory) and change to the data type of
left - hand side
4Assignment Statement assignment operator
- Assignment operator
- , ,-, , /,
- Example
- x 3
- Means xx3
5Assignment Statement (Cont.)
- Identifier expression
- Both sides of data type should match. If not,
then - the right hand side of type will be widened
- automatically to the left hand side data type.
- Otherwise, warning message will be given
- for narrowing data type.
6Assignment Statement (Cont.)
- Type conversion
- Automatic conversion rule
- Manual conversion use casting
7Assignment Statement (Cont.)
- Automatic conversion rule
- The widest data type in an expression determines
the - data type of the expression
- unsigned type is wider than the corresponding
type - the rest from widest double, float, long,
int, short (or char) - Finally, the expression data type will be widened
to - the data type of the identifier
8Assignment Statement (Cont.)
- Examples
- unsigned u
- long g
- int i
- unsigned long ul
- ul u-3g i // right hand side result type
- // long is converted to unsigned long
- // before stored in ul
9Assignment Statement (Cont.)
- Examples
- unsigned u
- float f
- int i
- double db
- db u/3-if // right hand side result type
- // float is converted to double before
- // stored to db
10Assignment Statement (Cont.)
- Casting
- Identifier (left hand side type) expression
11Assignment Statement (Cont.)
- Examples
- unsigned u
- long g
- int i
- unsigned long ul
- i (int) (u-3g 4) // right hand side result
type - // is long
12Conditional Statement - if
- if, if .. else, ?, switch
- if form
- if (condition)
-
-
-
13Conditional Statement if (Cont.)
- Note condition is evaluated using short-circuit
- If condition consists of condition1 and
condition2 - connected by a logic operator
- Condition1 condition2
- if condition1 is false,
- then it will not evaluate condition2
- Condition1 condition2
- if condition1 is true,
- then it will not evaluate condition2
14Conditional Statement if (Cont.)
- Example
- int x
- if (x 5)
-
- printf(Always TRUE is printed!\n)
-
15Conditional Statement if else
- if else form
- if (condition)
-
-
-
- else
-
-
-
16Conditional Statement if else (Cont.)
- Example
- if (xlty)
-
- min x
-
- else
-
- min y
-
17Conditional Statement ?
- form identifier
- (condition)?true partfalse part
-
18Conditional Statement ? (Cont.)
- Example (Same example as previous one)
- min (xlty) ? x y
-
19Conditional Statement switch
- switch form values cannot be range as in Visual
Basic - switch (expression)
-
- case value1
-
- break // quit the current (switch) statement
- case value2
- case value3
-
- break // do this part when value2 or value3
20Conditional Statement switch (Cont.)
- switch form (Cont.)
- case
-
- break
- default // everything else
-
21Conditional Statement switch (Cont.)
- Example
- switch (character)
- case a
- case A a_count
- break
-
- default
- printf(c is not a letter!\n, character)
22Loop Statement
- while
- do while
- for
- Steps of writing a loop
- Identify the loop body repetition part
- Identify condition that can make the
- repetition of loop body either true of false
- Make sure the first time, condition is true
23Loop Statement - while
- while form repeat loop body when condition is
- true pre-condition
- while (condition part)
-
-
-
- Note condition part can be more than one
statement, - separated by a comma
- Example while (scanf("c", character),
character !'\n') - What task does the statement above perform?
24Loop Statement - while
- Condition can be
- Counter control (definite loop) use counter
- Example
- int count1
- // the following prints out 5 times only
- while (count lt 5)
-
- count
- printf(d\n, count)
-
25Loop Statement - while
- Condition can be (Cont.)
- Sentinel control (indefinite loop)
- use pseudo data (sentinel) for condition
- Example sentinel data is any negative score
- int score
- scanf(d, score)
- while (score gt 0)
-
- printf(d\n, score)
- scanf(d, score)
-
- Yard-Meter Conversion
26Loop Statement - while
- Condition can be (Cont.)
- More than 1 statement
- Example
- Skip 1st Line
27Loop Statement while (Cont.)
- Example
- Hard to-find Error
- int x
- // the following is an infinite loop
- while (x 5)
-
- printf(Always TRUE is printed!\n)
-
28Loop Statement while (Cont.)
- Example (Use break to get out of loop)
- int x, count0
- // the following prints out TRUE 5 times
only - while (x 5)
-
- count
- printf(Always TRUE is printed!\n)
- if (count 5)
- break
-
29Loop Statement do while
- do while form repeat loop body when condition
- is true post-condition
- do
-
-
- while (condition)
30Loop Statement do while (Cont.)
- Example
- int x0
- do
-
- x
- printf(Always TRUE is printed!\n)
- while (x ! 5)
31Loop Statement - for
- for form repeat loop body when condition is
- true pre-condition
- Most general form can represent
- while and do while loops
32Loop Statement for (Cont.)
- for (initialization, exprPart1,
- exprPart2, ,condition
- exprPart3, ,last statement in loop body)
-
-
-
33Loop Statement for (Cont.)
- Example
- int i // loop index must be defined
- // outside for loop
- for (i0ilt5i)
-
- printf(TRUE is printed!\n) // print 5
times -
34Loop Statement for (Cont.)
- Example (Same as the previous one)
- int i0
- // print 5 times
- for (ilt5 printf(TRUE is printed!\n), i)
-
-
35Loop Statement for (Cont.)
- Example (Use continue statement to skip current
loop) - int x, count0
- // the following prints out TRUE 5 times
only - while (count lt10)
-
- count
- if (count 2 1) // skip odd number times
of print - continue
- printf(TRUE is printed!\n)
36Loop Statement for (Cont.)
- Represent while loop
- for (condition)
-
-
-
37Loop Statement for (Cont.)
- How to check end of file
- If read a character
- Use scanf(c, c)gt0 to read
- in a character to check any character being read
- (if reach to end of file, scanf returns -1)
- Example 1 a.out lt checkEOF.c
- If read an integer
- Use scanf(d, number)gt0 to read
38Loop Statement for (Cont.)
- Represent do while loop
- for (loop body statements, condition)
-
39Keyboard Input Format Function - scanf
- Form scanf(pure_format, variable_address)
- Format similar to those used in printf, however,
no character or strings - included
- i for decimal, octal (data begins with 0) or
hexadecimal (data begins with 0x) - If use s format, it reads all until it reaches
the first white space - variable represents the address of the memory
(or variable) - Example
- char c
- int i
- double db
- char s80 // remember s is address array of 80
characters - scanf(cdlfs, c, i, db, s)
- // sample dataa 100 -1.23 This is a sample data
- // ca, i100, db-1.23, sThis
40Class Example
- Example 1
- Example 2 print character 2
- in front of each line
- Read Character read a character after reading a
- number
- For loop
41Assignment
42Practice
- Given int a1,b2,c3,d4
- Find the values of the following table
- Expression Value
- ________ _____
- a bc a
- b cd5 b
-
43Practice - Answer
- Given int a1,b2,c3,d4
- Find the values of the following table
- Expression Value
- ________ _____
- a bc a 6
- b cd5 b 18
-
44References
- Herbert Schildt C The Complete Reference,
- 4th ed, Osborne Publishing
- Deitel Deitel C How to Program, 4th ed.,
- Chapter 3, 4 9, Prentice Hall