Conditional Execution and Interations



Programming Most problems in real life may not be as simple as simple computation of numbers. Many problems require decisions to be made. Let us look at an example where two values have to be accepted from the user. The values have to be then compared and a result indicating whether they are equal or not has to be displayed. Details The if decision-making construct specifies a conditional expression that needs to be evaluated. A decision is made based on the result of the conditional expression. There are many types of the ifconstruct. The three main types of ifconstructs are: Simple ifconstruct if…elseconstruct Nested if…elseconstruct The switch…case Construct Another conditional construct available is, switch…caseconstruct. It is used when a variable needs to be compared with multiple values to make a decision. The switch…caseconstruct enables you to make a decision by selecting from a number of choices. This construct consists of one switchstatement, a number of casestatements, and a defaultstatement. Iteration One of the most important characteristics of a computer is its ability to execute a series of instructions repeatedly. This gives you the flexibility to control the number of times a task is to be repeated. Let us take an example where you have to accept 10 numbers, find their sum, and display the result. To solve this problem, you will declare 10 variables and then find their sum. What happens if you have to accept 50 numbers and find their sum? Here, the task of accepting the number is repeated. To solve such problems, you can use the concept of iteration/ loops where a set of tasks are performed repeatedly. A loop is a sequence of instructions that is repeated more than once. A loop must always perform certain steps in a specified sequence. There are two types of loops, fixed loop where the number of repetitions is known and variable loop where the number of repetitions is not known. The following types of loops can be used in pseudocode/ flowchart to perform the repeated tasks: for while repeat…until

Sent from Windows Mail

Popular posts from this blog

ch11 review silberschatz operating systems concepts essentials 2nd ed