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

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

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

INTEC CS160 - Jeanine Ingber Algorithm Development

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

INTEC CS160 - Jeanine Ingber Conditional Expressions

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

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

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

INTEC CS160 - Jeanine Ingber Selection Statements

INTEC CS160 - Jeanine Ingber Selection Statements if if else switch

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

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

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

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

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?

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

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.

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"); }

INTEC CS160 - Jeanine Ingber Loop Structures

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

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

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.

INTEC CS160 - Jeanine Ingber for statement for(initialization; test; increment/decrement) statement; for(initialization; test; increment/decrement) { statement;. }

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

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;

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 ; }

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

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

INTEC CS160 - Jeanine Ingber Data Files

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);

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);

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