1 / 31

Engineering Problem Solving with C Fundamental Concepts

Engineering Problem Solving with C Fundamental Concepts. Chapter 3 Control Structures and Data Files. Algorithm Development. no. yes. Structured Programming. Sequence Selection Repetition. no. yes. Conditional Expressions. Relational Operators. == equality != non equality

reba
Download Presentation

Engineering Problem Solving with C Fundamental Concepts

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Algorithm Development INTEC CS160 - Jeanine Ingber

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

  4. Conditional Expressions INTEC CS160 - Jeanine Ingber

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

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

  7. Operator Precedence • < <= > >= • == != • && • || INTEC CS160 - Jeanine Ingber

  8. Selection Statements INTEC CS160 - Jeanine Ingber

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

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

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

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

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

  14. 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

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

  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. INTEC CS160 - Jeanine Ingber

  17. 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

  18. Loop Structures INTEC CS160 - Jeanine Ingber

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

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

  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 atleast once. INTEC CS160 - Jeanine Ingber

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

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

  24. 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

  25. 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

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

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

  28. Data Files INTEC CS160 - Jeanine Ingber

  29. Data Files Each data file must have a filepointer • 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

  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); INTEC CS160 - Jeanine Ingber

  31. Reading Data Files • counter controlled loop • for loop • sentinel controlled loop • while loop • end of file controlled loop • while loop INTEC CS160 - Jeanine Ingber

More Related