Title: Engineering Problem Solving with C Fundamental Concepts
1Engineering Problem Solving with C Fundamental
Concepts
- Chapter 3
- Control Structures and Data Files
2Algorithm Development
3Structured Programming
- Sequence
- Selection
- Repetition
yes
no
4Conditional Expressions
5Relational Operators
- equality
- ! non equality
- lt less than
- gt greater than
- lt less than equal to
- gt greater than equal to
6Logical Operators
7Operator Precedence
8Selection Statements
9Selection Statements
10If statement
- if(Boolean expression)
- statement //single statement
- if(Boolean expression)
- //more than one statement
- statement1
- .
- statement n
-
11If statement - examples
- if (xgt0)
- k
- if(xgt0)
-
- xsqrt(x)
- k
-
12if - else statement
- if(Boolean expression)
- statement
- else
- statement
- if(Boolean expression)
-
- statement block
-
- else
-
- statement block
-
-
13nested if-else
if(x gt y) if(y lt z) k else m else j
14Practice!
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?
15Switch Statement
switch(expression) case constant statement(
s) break case constant statement(s) bre
ak / default is optional/ default statem
ent(s)
16Switch 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.
17Practice!
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")
18Loop Structures
19repetition
- while statement
- do while statement
- for statement
20while statement
- while(expression)
- statement
- while(expression)
-
- statement
- statement
- .
-
21do 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.
22for statement
- for(initialization test increment/decrement)
- statement
- for(initialization test increment/decrement)
-
- statement
- statement
- .
-
23for statement
initalize
test
increment/ decrement
true
statement(s)
statement(s)
24for statement - examples
- int sum 0
- for(int I1Ilt10I2)
- sum sum I
- int fact 1
- for(int n5ngt1n- -)
- fact fact n
25Practice!
- 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
-
-
26break statement
- break
- terminates loop
- execution continues with the first statement
following the loop -
27continue statement
- continue
- forces next iteration of the loop, skipping any
remaining statements in the loop
28Data Files
29Data 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)
30I/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)
31Reading Data Files
- counter controlled loop
- for loop
- sentinel controlled loop
- while loop
- end of file controlled loop
- while loop