c programming for engineers iteration
play

C Programming for Engineers Iteration ICEN 360 Spring 2017 Prof. - PowerPoint PPT Presentation

C Programming for Engineers Iteration ICEN 360 Spring 2017 Prof. Dola Saha 1 Application: Summing even numbers 2 Application: Compound Interest Calculation Consider the following problem statement: A person invests $1000.00 in a


  1. C Programming for Engineers Iteration ICEN 360– Spring 2017 Prof. Dola Saha 1

  2. Application: Summing even numbers 2

  3. Application: Compound Interest Calculation Consider the following problem statement: Ø § A person invests $1000.00 in a savings account yielding 5% interest. Assuming that all interest is left on deposit in the account, calculate and print the amount of money in the account at the end of each year for 10 years. Use the following formula for determining these amounts: a = p(1 + r) n where p is the original amount invested (i.e., the principal) r is the annual interest rate n is the number of years a is the amount on deposit at the end of the n th year. 3

  4. C Code for Compound Interest Calculation 4

  5. Output 5

  6. Classwork Assignment Ø Write a program that finds the smallest of several integers. Assume that the first value read specifies the number of values remaining. Your program should read only one value each time scanf is executed. Ø A typical input sequence might be § 5 400 500 300 200 100 § where 5 indicates that the subsequent five values are to be used for finding minimum. 6

  7. Classwork Assignment Ø Write a program that prints the following patterns separately, one below the other. Use for loops to generate the patterns. [ Hint: The last two patterns require that each line begin with an appropriate number of blanks.] (A) (B) (C) (D) * ********** ********** * ** ********* ********* ** *** ******** ******** *** **** ******* ******* **** ***** ****** ****** ***** ****** ***** ***** ****** ******* **** **** ******* ******** *** *** ******** ********* ** ** ********* ********** * * ********** 7

  8. do ... while Iteration Statement Ø Similar to the while statement. while ( condition ) do Ø Ø statement while ( condition ) ; Ø The loop-continuation Ø The loop-continuation condition is tested at the condition after the loop beginning of the loop body is performed . Ø The loop body will be executed at least once. 8

  9. Example do ... while Iteration Statement 9

  10. Flowchart do ... while Iteration Statement 10

  11. break and continue Statements Ø Break § Used inside while, for, do…while, switch Statements § When executed, program exits the statements Ø Continue § Used in while, for, do…while Statements § When executed, the loop-continuation test is evaluated immediately after the continue statement is executed. § In the for statement, the increment expression is executed, then the loop-continuation test is evaluated. 11

  12. break Statement 12

  13. continue Statement 13

  14. Revisiting switch Statement If break is not used anywhere in Ø a switch statement, then each time a match occurs in the statement, the statements for all the remaining case s will be executed—called fallthrough . If no match occurs, the default Ø case is executed, and an error message is printed. 14

  15. Code Snippet (1) 15

  16. Code Snippet (2) 16

  17. Code Snippet (3) 17

  18. Code Snippet (4) & Output 18

  19. Logical Operators Ø Used to form more complex conditions by combining simple conditions. Ø The logical operators are && (logical AND), || (logical OR) and ! (logical NOT also called logical negation) Ø Logical AND – used to ensure that two conditions are both true before we choose a certain path of execution Ø Logical OR – used to ensure that at least one condition is true before we choose a certain path of execution Ø Logical NOT – used to “reverse” the meaning of a condition. 19

  20. Truth Table Ø Table of Logic 20

  21. Operator Precedence 21

  22. Structured Program Summary (1) 22

  23. Structured Program Summary (2) 23

  24. Rules for forming structured programs Ø Begin with the simplest flowchart Ø Stacking Rule – Any rectangle (action) can be replaced by two rectangles (actions) in sequence Ø Nesting Rule – Any rectangle (action) can be replaced by any control statement Ø Stacking & Nesting Rule rules may be applied in any order. 24

  25. Simplest Flowchart 25

  26. Stacking Rule 26

  27. Nesting Rule 27

  28. Structured Program Building Blocks 28

  29. Structured Programming Ø Structured programming promotes simplicity. Ø Bohm and Jacopini showed that only three forms of control are needed: § Sequence § Selection § Iteration 29

  30. Structured Programming Options Ø Sequence is straightforward. Ø Selection is implemented in one of three ways: § if statement (single selection) § if … else statement (double selection) § switch statement (multiple selection) Ø Iteration is implemented in one of three ways: § while statement § do … while statement § for statement 30

Recommend


More recommend