Engineering Problem Solving with C Fundamental Concepts - PowerPoint PPT Presentation

1 / 31
About This Presentation
Title:

Engineering Problem Solving with C Fundamental Concepts

Description:

Control Structures and Data Files. INTEC CS160 - Jeanine Ingber. Algorithm Development ... Practice! Convert the following nested if/else statements to a switch ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 32
Provided by: jeani162
Category:

less

Transcript and Presenter's Notes

Title: Engineering Problem Solving with C Fundamental Concepts


1
Engineering Problem Solving with C Fundamental
Concepts
  • Chapter 3
  • Control Structures and Data Files

2
Algorithm Development
3
Structured Programming
  • Sequence
  • Selection
  • Repetition

yes
no
4
Conditional Expressions
5
Relational Operators
  • equality
  • ! non equality
  • lt less than
  • gt greater than
  • lt less than equal to
  • gt greater than equal to

6
Logical Operators
  • ! not
  • and
  • or

7
Operator Precedence
  • lt lt gt gt
  • !

8
Selection Statements
9
Selection Statements
  • if
  • if else
  • switch

10
If statement
  • if(Boolean expression)
  • statement //single statement
  • if(Boolean expression)
  • //more than one statement
  • statement1
  • .
  • statement n

11
If statement - examples
  • if (xgt0)
  • k
  • if(xgt0)
  • xsqrt(x)
  • k

12
if - else statement
  • if(Boolean expression)
  • statement
  • else
  • statement
  • if(Boolean expression)
  • statement block
  • else
  • statement block

13
nested if-else
if(x gt y) if(y lt z) k else m else j

14
Practice!
int x9, y7, z2, k0, m0, j0 if(x gt y) if(y
lt z) k else m else j What are the
values of j, k and m?
15
Switch Statement
switch(expression) case constant statement(
s) break case constant statement(s) bre
ak / default is optional/ default statem
ent(s)
16
Switch Statement
  • Expression must be of type integer or character
  • The keyword case must be followed by a constant
  • break statement is required unless you want all
    subsequent statements to be executed.

17
Practice!
Convert the following nested if/else statements
to a switch statement if (rank1 ?? rank2)
printf("Lower division \n") else if
(rank3 ?? rank4) printf("Upper division
\n") else     if (rank5)
printf("Graduate student \n") else
printf("Invalid rank \n")
18
Loop Structures
19
repetition
  • while statement
  • do while statement
  • for statement

20
while statement
  • while(expression)
  • statement
  • while(expression)
  • statement
  • statement
  • .

21
do while
  • do
  • statement
  • while(expression)
  • do
  • statement1
  • statement2
  • .
  • while(expression)
  • note - the expression is tested after the
    statement(s) are executed, so statements are
    executed at least once.

22
for statement
  • for(initialization test increment/decrement)
  • statement
  • for(initialization test increment/decrement)
  • statement
  • statement
  • .

23
for statement
initalize
test
increment/ decrement
true
statement(s)
statement(s)
24
for statement - examples
  • int sum 0
  • for(int I1Ilt10I2)
  • sum sum I
  • int fact 1
  • for(int n5ngt1n- -)
  • fact fact n

25
Practice!
  • Determine the number of times that each of the
    following for loops are executed.
  • for (k3 klt20 k)
  • statements
  • for (k3 klt20 k)
  • statements
  • for (count-2 countlt14 count)
  • statements

26
break statement
  • break
  • terminates loop
  • execution continues with the first statement
    following the loop

27
continue statement
  • continue
  • forces next iteration of the loop, skipping any
    remaining statements in the loop

28
Data Files
29
Data Files
  • Each data file must have a file pointer
  • file pointer must be defined
  • FILE sensor1
  • FILE balloon
  • file pointer must be associated with a specific
    file using the fopen function
  • sensor1 fopen(sensor1.dat, r)
  • balloon fopen(balloon.dat, w)

30
I/O Statements
  • Input file - use fscanf instead of scanf
  • fscanf(sensor1, 1f lf, t, motion)
  • Output file - use fprint instead of printf
  • fprintf(balloon, f f f\n, time, height,
    velocity)

31
Reading Data Files
  • counter controlled loop
  • for loop
  • sentinel controlled loop
  • while loop
  • end of file controlled loop
  • while loop
Write a Comment
User Comments (0)
About PowerShow.com