Presentation is loading. Please wait.

Presentation is loading. Please wait.

INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.

Similar presentations


Presentation on theme: "INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files."— Presentation transcript:

1 INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files

2 INTEC CS160 - Jeanine Ingber Algorithm Development

3 INTEC CS160 - Jeanine Ingber Structured Programming Sequence Selection Repetition yesno yes no

4 INTEC CS160 - Jeanine Ingber Conditional Expressions

5 INTEC CS160 - Jeanine Ingber Relational Operators ==equality !=non equality greater than <=less than equal to >=greater than equal to

6 INTEC CS160 - Jeanine Ingber Logical Operators !not &&and ||or

7 INTEC CS160 - Jeanine Ingber Operator Precedence 1 >= 2 == != 3 && 4 ||

8 INTEC CS160 - Jeanine Ingber Selection Statements

9 INTEC CS160 - Jeanine Ingber Selection Statements if if else switch

10 INTEC CS160 - Jeanine Ingber If statement if(Boolean expression) statement;//single statement if(Boolean expression) {//more than one statement statement1;. statement n; }

11 INTEC CS160 - Jeanine Ingber If statement - examples if (x>0) k++; if(x>0) { x=sqrt(x); k++; }

12 INTEC CS160 - Jeanine Ingber if - else statement if(Boolean expression) statement; else statement; if(Boolean expression) { statement block } else { statement block }

13 INTEC CS160 - Jeanine Ingber nested if-else if(x > y) if(y < z) k++; else m++; else j++;

14 INTEC CS160 - Jeanine Ingber Practice! int x=9, y=7, z=2, k=0, m=0, j=0; if(x > y) if(y < z) k++; else m++; else j++; What are the values of j, k and m?

15 INTEC CS160 - Jeanine Ingber Switch Statement switch(expression) { case constant: statement(s); break; case constant: statement(s); break; /* default is optional*/ default: statement(s); }

16 INTEC CS160 - Jeanine Ingber 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 INTEC CS160 - Jeanine Ingber Practice! Convert the following nested if/else statements to a switch statement: if (rank==1 || rank==2) printf("Lower division \n"); else { if (rank==3 || rank==4) printf("Upper division \n"); else { if (rank==5) printf("Graduate student \n"); else printf("Invalid rank \n"); }

18 INTEC CS160 - Jeanine Ingber Loop Structures

19 INTEC CS160 - Jeanine Ingber repetition while statement do while statement for statement

20 INTEC CS160 - Jeanine Ingber while statement while(expression) statement; while(expression) { statement;. }

21 INTEC CS160 - Jeanine Ingber 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 INTEC CS160 - Jeanine Ingber for statement for(initialization; test; increment/decrement) statement; for(initialization; test; increment/decrement) { statement;. }

23 INTEC CS160 - Jeanine Ingber for statement initalize test increment/ decrement true statement(s)

24 INTEC CS160 - Jeanine Ingber for statement - examples int sum =0; for(int I=1;I<10;I+=2) sum = sum + I; int fact =1; for(int n=5;n>1;n- -) fact = fact * n;

25 INTEC CS160 - Jeanine Ingber Practice! Determine the number of times that each of the following for loops are executed. for (k=3; k<=20; k++) { statements ; } for (k=3; k<=20; ++k) { statements ; } for (count=-2; count<=14; count++) { statements ; }

26 INTEC CS160 - Jeanine Ingber break statement break; –terminates loop –execution continues with the first statement following the loop

27 INTEC CS160 - Jeanine Ingber continue statement continue; –forces next iteration of the loop, skipping any remaining statements in the loop

28 INTEC CS160 - Jeanine Ingber Data Files

29 INTEC CS160 - Jeanine Ingber 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 INTEC CS160 - Jeanine Ingber 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 INTEC CS160 - Jeanine Ingber Reading Data Files counter controlled loop –for loop sentinel controlled loop –while loop end of file controlled loop –while loop


Download ppt "INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files."

Similar presentations


Ads by Google