Example 14 : Creating a flowchart with for Loop Construct A 30 years old young man newly joined a company which offered him a job with monthly salary 1,500 USD. He is planning to save 20% of his monthly salary in a bank. The bank will offer him 1.2% yearly compound profit. He needs to know how much he will have in saving when he is 50 years old. Design flowchart to help him solving his problem. Figure 19: flowchart of for Loop construct to calculate saving amount
Introduction to Programming PROBLEM SOLVING SKILLS CHAPTER 1 UAE University, Faculty of Engineering, ERU | By. Marwan Alakhras 28 / 36 Example 15 : Creating a flowchart with For Loop Construct If you try to write programs to solve basic mathematical problems, like finding the sum of all positive numbers less than 100. Design the flowchart to display the sum. Begin False End Sum = Sum + Num Increment Num Num <= 100 Initialize Sum = 0 Initialize num=0 True Print Sum Figure 20: flowchart of for Loop construct to calculate sum of first 100 integers
Introduction to Programming PROBLEM SOLVING SKILLS CHAPTER 1 UAE University, Faculty of Engineering, ERU | By. Marwan Alakhras 29 / 36 Example 16 : Creating a flowchart with for Loop Construct Very complex mathematical applications can be solved use the factorial of numbers in there process. If you are asked to help solving this problem, then design a flowchart to find the factorial of any positive number. Begin False End Fact = Fact * Count Increment count Count <= Num Initialize Fact = 1 Initialize Count = 1 True Print Fact Get Num Figure 21: flowchart of for Loop construct to calculate the factorial
Introduction to Programming PROBLEM SOLVING SKILLS CHAPTER 1 UAE University, Faculty of Engineering, ERU | By. Marwan Alakhras 30 / 36 Generally all loops can replace each other (you can use any looping construct to solve any Problem), but sometimes using one of them will have some advantages over the others. An interesting question may arise here, is: When should do-while loop be used rather than while, or for loop? Actually there is no clear answer to this question, but some hints and cases based on the experience of the programmers, they can be summarized to help selecting the proper construct, especially for beginners; The for loop should be used whenever the number of iterations is known prior to the scope of decision. The while loop should be used when it is possible that the processes which are in the scope of the decision may not need to execute before knowing the exact situation, for example reading from file. The Do-while is preferable if some data necessary to be generated before testing the condition. For instance assume you are a world power in control of an arsenal of nuclear weapons and have written a program to launch missiles in the event of an attack. Your program contains a loop which launches a missile each time you are struck by an enemy missile, for example: Is a (Do-while) loop a good idea in this case?
You've reached the end of your free preview.
Want to read all 36 pages?
- Summer '15
- Ode, Control flow, Faculty of Engineering, UAE University, Marwan Alakhras