Title: CSCI6370: Topics in Computer Science Advanced Topics in Algorithms and Applications Fall Semester, 2002
1Lecture 10 Reviews
2Control Structures
- All C programs written in term of 3 control
structures - Sequence structures
- Programs executed sequentially by default
- Selection structures
- Used to choose among alternative courses of
action - Three types if, if else, and switch
- Repetition structures
- Used to repeat an action when some condition
remains true. - Three types while, do while and for
3Sequence Structures
- Normally, statements are executed one after the
other in the order written
4Selection Structures - if
- if statement
- if (condition)
- statement(s)
-
- Only performs an action if the condition is true.
- Example
- Flowchart
if ( grade gt 60 ) printf( Passed\n )
if ( grade gt 60 ) printf( Passed\n )
printf( Congratulations!\n )
if ( grade gt 60 ) printf( Passed\n )
printf( Congratulations!\n )
5Selection Structures - ifelse
- Ifelse statement
- if (condition)
- statement(s)
-
- else
- statement(s)
-
- Specifies an action to be performed both when the
condition is true and when it is false. - Flowchart
6Selection Structures - ifelse
- Nested ifelse statements
- Test for multiple cases by placing ifelse
selection statements inside ifelse selection
statement - Once condition is met, rest of statements skipped
if ( score gt 90 ) printf( "The grade is
'A'.\n" ) else if ( score gt 80 )
printf( "The grade is 'B'.\n" )
else if ( score gt 70 )
printf( "The grade is 'C'.\n" )
else if ( score gt 60 )
printf( "The grade is
'D'.\n" )
else
printf( "The grade is 'F'.\n" )
gt 90 A 80 89 B 70 79 C 60 69 D lt 60 F
7Selection Structures - ifelse
- ifelse statements in sequence
if ( score gt 90 ) printf( "The grade is
'A'.\n" ) else if ( score gt 80 )
printf( "The grade is 'B'.\n" )
if ( score gt 70 ) printf( "The grade
is 'C'.\n" ) else if ( score gt 60 )
printf( "The grade is 'D'.\n" )
else printf( "The grade is
'F'.\n" )
8Selection Structures - switch
- switch statement
- Useful when a variable or expression is tested
for all the values it can assume and different
actions are taken. - Format
- Series of case labels and an optional default
case - switch ( controlling expression )
- case value1
- actions
- case value2
- actions
- default
- actions
-
- The switch statement can be used only for testing
a constant integral expression.
switch (grade) case gt 90 case gt
80 ..
9Selection Structures - switch
- switch statement with break statements
- switch ( controlling expression )
- case value1
- actions
- break
- case value2
- actions
- break
- default
- actions
- break
-
- It causes program control to continue with the
first statement after the switch statement.
10Selection Structures - switch
- switch statement with break statements
- Example
- switch ( n )
- case 1
- printf( The number is 1\n)
- break
- case 2
- printf( The number is 2\n)
- break
- case 3
- printf( The number is 3\n)
- break
- default
- printf( The number is not 1, 2, or
3\n) - break
-
11Selection Structures - switch
- switch statement with break statements
- Example
- switch ( n )
- case 1
- printf( The number is 1\n)
- case 2
- printf( The number is 2\n)
- break
- case 3
- printf( The number is 3\n)
- break
- default
- printf( The number is not 1, 2, or
3\n) - break
-
12Repetition Structures
- Two types of repetition structures
- Counter-controlled repetition
- Definite repetition know how many times loop
will execute - Control variable used to count repetitions
- Sentinel-controlled repetition
- Indefinite repetition
- Used when number of repetitions not known
- Sentinel value indicates "end of data entry
- Loop ends when user inputs the sentinel value
13Repetition Structures
- Common error
- Infinite loops are caused when the
loop-continuation condition in a while, for or
do...while statement never becomes false. To
prevent - a) Make sure there is not a semicolon
immediately - after the header of a while statement.
- b) In a counter-controlled loop, make sure the
control variable is incremented (or decremented)
in the loop. - c) In a sentinel-controlled loop, make sure the
sentinel value is eventually input.
j 1 while (j lt 10) printf(d\n,
--j) j
j 1 while (j lt 10) printf(d\n, j)
j--
j 1 while (j lt 10) printf(d\n, j)
j
j 1 while (j lt 10) printf(d\n, j)
14Repetition Structures - while
- while loop repeated until condition becomes false
- Counter-controlled repetition
- Sentinel-controlled repetition
- Format
- while ( condition )
- statement(s)
-
- Flowchart
15Repetition Structures - while
- Counter-controlled while loop
- Loop repeated until counter reaches a certain
value - Count the number of iterations
j 1 while (j lt 10) printf(Good
Luck\n) j
j 0 while (j lt 10) printf(Good
Luck\n) j
j 0 while (j lt 10) printf(Good
Luck\n) j
j 1 while (j lt 10) printf(Good
Luck\n) j 2
j -1 while (j lt 10) printf(Good
Luck\n) j 3
16Repetition Structures - while
- Sentinel-controlled while loop
- Loop repeated until user inputs the sentinel
value - Sentinel value chosen so it cannot be confused
with a regular input - Examples
- Simple calculator
- ATM menu interface
- Prime number
17Repetition Structures - dowhile
- Similar to the while statement
- Condition for repetition only tested after the
body of the loop is performed. - All actions are performed at least once.
- Format
- do
- statement(s)
- while ( condition )
- Flowcharting the dowhile repetition statement
18Repetition Structures - dowhile
counter 1 while (counter lt 10) printf(
d\n, counter) counter
counter 1 do printf( d\n, counter)
counter while (counter lt 10)
counter 12 while (counter lt 10) printf(
d\n, counter) counter
counter 12 do printf( d\n, counter)
counter while (counter lt 10)
19Repetition Structures - for
- Can loop a known number of times
- Format when using for loops
- for ( initialization loopContinuationTest
increment ) - statement(s)
-
- initialization
- while ( loopContinuationTest )
- statement(s)
- increment
-
for (j 1 j lt 10 j) printf(d\n,
j)
for (j 10 j gt 0 j--) printf(d\n,
j)
for (j 10 j gt 0 j - 2) printf(d\n,
j)
Increasing/decreasing other than 1
increment
decrement
20Nested Control Loops
j
- Nested while loops
- i 1
- while (i lt 5)
- j 1
- while (j lt 5)
- printf(d d d\n, i, j, ij)
- j
-
- i
-
- Nested for loops
- for (i 1 i lt 5 i)
- for (j 1 j lt 5 j)
- printf(d d d\n, i, j, ij)
-
-
i
21Nested Control Loops
j
- Nested while loops
- i 1
- while (i lt 5)
- j 1
- while ( j lt i )
- printf(d d d\n, i, j, ij)
- j
-
- i
-
- Nested for loops
- for (i 1 i lt 5 i)
- for (j 1 j lt i j)
- printf(d d d\n, i, j, ij)
-
-
i
j lt i
j lt i
22Nested Control Loops
j
- Nested while loops
- i 1
- while (i lt 5)
- j 1
- while ( j lt 5 )
- printf(d d d\n, i, j, ij)
- j
-
- i 2
-
- Nested for loops
- for (i 1 i lt 5 i 2)
- for (j 1 j lt 5 j)
- printf(d d d\n, i, j, ij)
-
-
i
23Operators
- Arithmetic operators
- , -, , /,
- Assignment operator
-
- Arithmetic assignment operators
- , -, , /,
- Increment / decrement operators
- , --
- Relational operators
- lt, lt, gt, gt
- Equality operators
- , !
- Logical operators
- , , !
24 and
- Swapping both operators does not ordinarily cause
syntax errors.
int payCode 0 payCode 4 if (payCode 4)
printf(You get a bonus!\n)
int payCode 0 payCode 4 if (payCode
4) printf(You get a bonus!\n)
int payCode 0 payCode 3 if (payCode 4)
printf(You get a bonus!\n)
int payCode 0 payCode 3 if (payCode 4)
printf(You get a bonus!\n)
25Pre-increment and Post-increment
- Pre-increment/Pre-decrement
- Operator is used before the variable (c or --c)
- Variable is changed before the expression it is
in is evaluated - Post-increment/Post-decrement
- Operator is used after the variable (c or c--)
- Expression executes before the variable is
changed.
c 5 printf(d\n, c) printf(d\n, c)
c 5 printf(d\n, c) printf(d\n, c)
c 5 c printf(d\n, c)
c 5 c printf(d\n, c)
26Logical Operators
x
- Logical AND -
- Logical OR -
- Logical NOT - !
y
x
y
27Logical Operators
- Examples
- int F 0, T 1
- int x, y
- x !240
- x !0
- x (F 1) (T F)
- y (T !F) (!F T)
- x !(T F 1)
- if (grade gt 80 grade lt90)
- printf(B\n)
- while ((x gt 0) (y gt 10))
- x--
- y--
28scanf and printf
- scanf()
- Used to obtain a value (values) from the user
using standard input (usually keyboard). - Examples
- int num, j
- float x
- scanf( d, num)
- scanf( dd, num, j)
- scanf( f, x)
- scanf( ddf, num, j, x)
- scanf( d, num)
- scanf( dd, num)
29scanf and printf
- printf()
- int num 3, j 2
- float x 3.0
- printf( Good Luck!\n) / prints a string /
- printf( num d\n, num) / prints the value
of a variable / - printf( d d d\n, num, j, numj)
- / prints the value of an expression /
- printf( c\n, b) / prints the character
b / - j a
- printf( c\n, j) / prints the character a
/ - printf( x f\n, x) / prints a
floating-point number / - printf( x .2f\n, x )
- / prints result with 2 digits after decimal
point /