chapter 3 flow control by c. shing itec dept radford university
TRANSCRIPT
![Page 1: Chapter 3 Flow Control By C. Shing ITEC Dept Radford University](https://reader035.vdocument.in/reader035/viewer/2022062304/56649f1f5503460f94c37c38/html5/thumbnails/1.jpg)
Chapter 3 Flow Control
By C. Shing
ITEC Dept
Radford University
![Page 2: Chapter 3 Flow Control By C. Shing ITEC Dept Radford University](https://reader035.vdocument.in/reader035/viewer/2022062304/56649f1f5503460f94c37c38/html5/thumbnails/2.jpg)
Slide 2
Objectives Understand how to use assignment statement Understand how to use conditional statement Understand how to use loop statement
![Page 3: Chapter 3 Flow Control By C. Shing ITEC Dept Radford University](https://reader035.vdocument.in/reader035/viewer/2022062304/56649f1f5503460f94c37c38/html5/thumbnails/3.jpg)
Slide 3
Assignment 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
![Page 4: Chapter 3 Flow Control By C. Shing ITEC Dept Radford University](https://reader035.vdocument.in/reader035/viewer/2022062304/56649f1f5503460f94c37c38/html5/thumbnails/4.jpg)
Slide 4
Assignment Statement – assignment operator
Assignment operator =, +=,-=, *=, /=, %=
Example:
x *= 3
Means x=x*3
![Page 5: Chapter 3 Flow Control By C. Shing ITEC Dept Radford University](https://reader035.vdocument.in/reader035/viewer/2022062304/56649f1f5503460f94c37c38/html5/thumbnails/5.jpg)
Slide 5
Assignment 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.
![Page 6: Chapter 3 Flow Control By C. Shing ITEC Dept Radford University](https://reader035.vdocument.in/reader035/viewer/2022062304/56649f1f5503460f94c37c38/html5/thumbnails/6.jpg)
Slide 6
Assignment Statement (Cont.) Type conversion:
Automatic conversion rule Manual conversion: use casting
![Page 7: Chapter 3 Flow Control By C. Shing ITEC Dept Radford University](https://reader035.vdocument.in/reader035/viewer/2022062304/56649f1f5503460f94c37c38/html5/thumbnails/7.jpg)
Slide 7
Assignment 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
![Page 8: Chapter 3 Flow Control By C. Shing ITEC Dept Radford University](https://reader035.vdocument.in/reader035/viewer/2022062304/56649f1f5503460f94c37c38/html5/thumbnails/8.jpg)
Slide 8
Assignment Statement (Cont.) Examples:
unsigned u;
long g;
int i;
unsigned long ul;
ul = u-3*g +i; // right hand side result type
// long is converted to unsigned long
// before stored in ul
![Page 9: Chapter 3 Flow Control By C. Shing ITEC Dept Radford University](https://reader035.vdocument.in/reader035/viewer/2022062304/56649f1f5503460f94c37c38/html5/thumbnails/9.jpg)
Slide 9
Assignment Statement (Cont.) Examples:
unsigned u;
float f;
int i;
double db;
db = u/3-i+f; // right hand side result type
// float is converted to double before
// stored to db
![Page 10: Chapter 3 Flow Control By C. Shing ITEC Dept Radford University](https://reader035.vdocument.in/reader035/viewer/2022062304/56649f1f5503460f94c37c38/html5/thumbnails/10.jpg)
Slide 10
Assignment Statement (Cont.) Casting:
Identifier = (left hand side type) (expression)
![Page 11: Chapter 3 Flow Control By C. Shing ITEC Dept Radford University](https://reader035.vdocument.in/reader035/viewer/2022062304/56649f1f5503460f94c37c38/html5/thumbnails/11.jpg)
Slide 11
Assignment Statement (Cont.) Examples:
unsigned u;
long g;
int i;
unsigned long ul;
i = (int) (u-3*g +4); // right hand side result type
// is long
![Page 12: Chapter 3 Flow Control By C. Shing ITEC Dept Radford University](https://reader035.vdocument.in/reader035/viewer/2022062304/56649f1f5503460f94c37c38/html5/thumbnails/12.jpg)
Slide 12
Conditional Statement - if if, if .. else, ?:, switch if form:
if (condition)
{
…
}
![Page 13: Chapter 3 Flow Control By C. Shing ITEC Dept Radford University](https://reader035.vdocument.in/reader035/viewer/2022062304/56649f1f5503460f94c37c38/html5/thumbnails/13.jpg)
Slide 13
Conditional Statement – if (Cont.)Note: condition is evaluated using short-circuitIf 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
![Page 14: Chapter 3 Flow Control By C. Shing ITEC Dept Radford University](https://reader035.vdocument.in/reader035/viewer/2022062304/56649f1f5503460f94c37c38/html5/thumbnails/14.jpg)
Slide 14
Conditional Statement – if (Cont.) Example:
int x; if (x = 5)
{
printf(“Always TRUE is printed!\n”);
}
![Page 15: Chapter 3 Flow Control By C. Shing ITEC Dept Radford University](https://reader035.vdocument.in/reader035/viewer/2022062304/56649f1f5503460f94c37c38/html5/thumbnails/15.jpg)
Slide 15
Conditional Statement – if … else if … else form:
if (condition) {
… } else {
… }
![Page 16: Chapter 3 Flow Control By C. Shing ITEC Dept Radford University](https://reader035.vdocument.in/reader035/viewer/2022062304/56649f1f5503460f94c37c38/html5/thumbnails/16.jpg)
Slide 16
Conditional Statement – if … else (Cont.)
Example: if (x<y) {
min = x; } else {
min = y; }
![Page 17: Chapter 3 Flow Control By C. Shing ITEC Dept Radford University](https://reader035.vdocument.in/reader035/viewer/2022062304/56649f1f5503460f94c37c38/html5/thumbnails/17.jpg)
Slide 17
Conditional Statement – ?: form: identifier =
(condition)?true part:false part
![Page 18: Chapter 3 Flow Control By C. Shing ITEC Dept Radford University](https://reader035.vdocument.in/reader035/viewer/2022062304/56649f1f5503460f94c37c38/html5/thumbnails/18.jpg)
Slide 18
Conditional Statement – ?: (Cont.) Example: (Same example as previous one)
min = (x<y) ? x : y;
![Page 19: Chapter 3 Flow Control By C. Shing ITEC Dept Radford University](https://reader035.vdocument.in/reader035/viewer/2022062304/56649f1f5503460f94c37c38/html5/thumbnails/19.jpg)
Slide 19
Conditional Statement – switch switch form: values (char/int) 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
![Page 20: Chapter 3 Flow Control By C. Shing ITEC Dept Radford University](https://reader035.vdocument.in/reader035/viewer/2022062304/56649f1f5503460f94c37c38/html5/thumbnails/20.jpg)
Slide 20
Conditional Statement – switch (Cont.)
switch form: (Cont.) case …:
…
break;
default: // everything else
…
}
![Page 21: Chapter 3 Flow Control By C. Shing ITEC Dept Radford University](https://reader035.vdocument.in/reader035/viewer/2022062304/56649f1f5503460f94c37c38/html5/thumbnails/21.jpg)
Slide 21
Conditional Statement – switch (Cont.) Example:
switch (character) { case ‘a’: case ‘A’: a_count++;
break;…default:
printf(“%c is not a letter!\n”, character);}
![Page 22: Chapter 3 Flow Control By C. Shing ITEC Dept Radford University](https://reader035.vdocument.in/reader035/viewer/2022062304/56649f1f5503460f94c37c38/html5/thumbnails/22.jpg)
Slide 22
Loop Statement while do … while forSteps of writing a loop:1. Identify the loop body: repetition part2. Identify condition that can make the repetition of loop body either true of false;Make sure the first time, condition is true3. Initialize loop condition before loop4. Update loop condition at end of loop body
![Page 23: Chapter 3 Flow Control By C. Shing ITEC Dept Radford University](https://reader035.vdocument.in/reader035/viewer/2022062304/56649f1f5503460f94c37c38/html5/thumbnails/23.jpg)
Slide 23
Loop 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?
![Page 24: Chapter 3 Flow Control By C. Shing ITEC Dept Radford University](https://reader035.vdocument.in/reader035/viewer/2022062304/56649f1f5503460f94c37c38/html5/thumbnails/24.jpg)
Slide 24
Loop Statement - while Condition can be
Counter control (definite loop): use counter
Example:int count=1; // the following prints out 5 times onlywhile (count <= 5)
{ ++count;
printf(“%d\n”, count);}
![Page 25: Chapter 3 Flow Control By C. Shing ITEC Dept Radford University](https://reader035.vdocument.in/reader035/viewer/2022062304/56649f1f5503460f94c37c38/html5/thumbnails/25.jpg)
Slide 25
Loop Statement - while Condition can be (Cont.)
Sentinel control (indefinite loop): use pseudo data (sentinel) for condition
Example: sentinel data is any negative scoreint score;scanf(“%d”, &score);while (score >= 0)
{ printf(“%d\n”, score);
scanf(“%d”, &score);}
Yard-Meter Conversion
![Page 26: Chapter 3 Flow Control By C. Shing ITEC Dept Radford University](https://reader035.vdocument.in/reader035/viewer/2022062304/56649f1f5503460f94c37c38/html5/thumbnails/26.jpg)
Slide 26
Loop Statement - while Condition can be (Cont.)
More than 1 statement
Example:
Skip 1st Line
![Page 27: Chapter 3 Flow Control By C. Shing ITEC Dept Radford University](https://reader035.vdocument.in/reader035/viewer/2022062304/56649f1f5503460f94c37c38/html5/thumbnails/27.jpg)
Slide 27
Loop Statement – while (Cont.) Example:
Hard –to-find Error:
int x;
// the following is an infinite loopwhile (x = 5)
{
printf(“Always TRUE is printed!\n”);
}
![Page 28: Chapter 3 Flow Control By C. Shing ITEC Dept Radford University](https://reader035.vdocument.in/reader035/viewer/2022062304/56649f1f5503460f94c37c38/html5/thumbnails/28.jpg)
Slide 28
Loop Statement – while (Cont.) Example: (Use break to get out of loop) int x, count=0; // the following prints out TRUE 5 times only
while (x = 5) { ++count;
printf(“Always TRUE is printed!\n”); if (count == 5) break; }
![Page 29: Chapter 3 Flow Control By C. Shing ITEC Dept Radford University](https://reader035.vdocument.in/reader035/viewer/2022062304/56649f1f5503460f94c37c38/html5/thumbnails/29.jpg)
Slide 29
Loop Statement – do while do while form: repeat loop body when condition
is true – post-condition do
{
…
} while (condition);
![Page 30: Chapter 3 Flow Control By C. Shing ITEC Dept Radford University](https://reader035.vdocument.in/reader035/viewer/2022062304/56649f1f5503460f94c37c38/html5/thumbnails/30.jpg)
Slide 30
Loop Statement – do while (Cont.) Example:
int x=0;
do {
++x;
printf(“Always TRUE is printed!\n”);
} while (x != 5)
![Page 31: Chapter 3 Flow Control By C. Shing ITEC Dept Radford University](https://reader035.vdocument.in/reader035/viewer/2022062304/56649f1f5503460f94c37c38/html5/thumbnails/31.jpg)
Slide 31
Loop Statement - for for form: repeat loop body when condition is
true – pre-condition Most general form: can represent
while and do while loops
![Page 32: Chapter 3 Flow Control By C. Shing ITEC Dept Radford University](https://reader035.vdocument.in/reader035/viewer/2022062304/56649f1f5503460f94c37c38/html5/thumbnails/32.jpg)
Slide 32
Loop Statement – for (Cont.) for (initialization, exprPart1, …;
exprPart2, …,condition;
exprPart3, …,last statement in loop body) {
…
}
![Page 33: Chapter 3 Flow Control By C. Shing ITEC Dept Radford University](https://reader035.vdocument.in/reader035/viewer/2022062304/56649f1f5503460f94c37c38/html5/thumbnails/33.jpg)
Slide 33
Loop Statement – for (Cont.) Example:
int i; // loop index must be defined
// outside for loopfor (i=0;i<5;++i)
{
printf(“TRUE is printed!\n”); // print 5 times
}
![Page 34: Chapter 3 Flow Control By C. Shing ITEC Dept Radford University](https://reader035.vdocument.in/reader035/viewer/2022062304/56649f1f5503460f94c37c38/html5/thumbnails/34.jpg)
Slide 34
Loop Statement – for (Cont.) Example: (Same as the previous one)
int i=0;
// print 5 timesfor (;i<5; printf(“TRUE is printed!\n”), ++i);
![Page 35: Chapter 3 Flow Control By C. Shing ITEC Dept Radford University](https://reader035.vdocument.in/reader035/viewer/2022062304/56649f1f5503460f94c37c38/html5/thumbnails/35.jpg)
Slide 35
Loop Statement – for (Cont.) Example: (Use continue statement to skip current loop) int x, count=0; // the following prints out TRUE 5 times only
while (count <=10) { ++count; if (count %2 == 1) // skip odd number times of print continue;
printf(“TRUE is printed!\n”);}
![Page 36: Chapter 3 Flow Control By C. Shing ITEC Dept Radford University](https://reader035.vdocument.in/reader035/viewer/2022062304/56649f1f5503460f94c37c38/html5/thumbnails/36.jpg)
Slide 36
Loop Statement – for (Cont.) Represent while loop:
for (;condition;) {
…
}
![Page 37: Chapter 3 Flow Control By C. Shing ITEC Dept Radford University](https://reader035.vdocument.in/reader035/viewer/2022062304/56649f1f5503460f94c37c38/html5/thumbnails/37.jpg)
Slide 37
Loop Statement – for (Cont.) How to check end of file:1. If read a character: (Must deal with \n)Use scanf(“%c”, &c)>0 to read in a character to check any character being read (if reach to end of file, scanf returns -1)
Example 1: a.out < checkEOF.c
2. If read an integer:Use scanf(“%d”, &number)>0 to read
![Page 38: Chapter 3 Flow Control By C. Shing ITEC Dept Radford University](https://reader035.vdocument.in/reader035/viewer/2022062304/56649f1f5503460f94c37c38/html5/thumbnails/38.jpg)
Slide 38
Loop Statement – for (Cont.) Represent do while loop:
for (;loop body statements, condition;);
![Page 39: Chapter 3 Flow Control By C. Shing ITEC Dept Radford University](https://reader035.vdocument.in/reader035/viewer/2022062304/56649f1f5503460f94c37c38/html5/thumbnails/39.jpg)
Slide 39
Keyboard 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 s[80]; // remember s is address: array of 80 charactersscanf(“%c%d%lf%s”, &c, &i, &db, s); // sample data:a 100 -1.23 This is a sample data// c=‘a’, i=100, db=-1.23, s=“This”
![Page 40: Chapter 3 Flow Control By C. Shing ITEC Dept Radford University](https://reader035.vdocument.in/reader035/viewer/2022062304/56649f1f5503460f94c37c38/html5/thumbnails/40.jpg)
Slide 40
Class Example Example 1 Example 2 : print character ‘2’
in front of each line Read Character : read a character after reading a
number For loop
![Page 41: Chapter 3 Flow Control By C. Shing ITEC Dept Radford University](https://reader035.vdocument.in/reader035/viewer/2022062304/56649f1f5503460f94c37c38/html5/thumbnails/41.jpg)
Slide 41
Assignment Assignment #1
![Page 42: Chapter 3 Flow Control By C. Shing ITEC Dept Radford University](https://reader035.vdocument.in/reader035/viewer/2022062304/56649f1f5503460f94c37c38/html5/thumbnails/42.jpg)
Slide 42
Practice
Given int a=1,b=2,c=3,d=4;
Find the values of the following table:
Expression Value
________ _____ a +=b+c a=
b *=c=d+5 b=
![Page 43: Chapter 3 Flow Control By C. Shing ITEC Dept Radford University](https://reader035.vdocument.in/reader035/viewer/2022062304/56649f1f5503460f94c37c38/html5/thumbnails/43.jpg)
Slide 43
Practice - Answer
Given int a=1,b=2,c=3,d=4;
Find the values of the following table:
Expression Value
________ _____ a +=b+c a= 6
b *=c=d+5 b= 18
![Page 44: Chapter 3 Flow Control By C. Shing ITEC Dept Radford University](https://reader035.vdocument.in/reader035/viewer/2022062304/56649f1f5503460f94c37c38/html5/thumbnails/44.jpg)
Slide 44
References Herbert Schildt: C: The Complete Reference,
4th ed, Osborne Publishing Deitel & Deitel: C How to Program, 4th ed.,
Chapter 3, 4 & 9, Prentice Hall