What is the difference between a looping construct and a selection construct




















You'd then ask, "Do I have any more toothpaste? But if the answer is yes , you would just use the toothpaste. This is really all a selection is doing: answering a question based on what it finds. The third programming structure is a loop. Like selections, loops ask questions. However, the difference is that they ask the same question over and over and over again, until a certain task is complete.

For example, take the act of hammering a nail. Even though you may not realize it, you're constantly asking yourself, "Is the nail all the way in? Parenthesis may also be used to control the order in which boolean expressions are evaluated. Here we present another looping construct. It is most often used when iterating through a sequence of a pre-defined length.

Navigation index next previous Applications in C for Engineering Technology » 2. Topic 1 - Getting Started ». Note In C, extend the Boolean statements to integers. Table Of Contents 2. Control Constructs 2. Relational and Logical Operators 2. The break; statement transfers control to the closing brace of the switch construct.

Braces around the statements between case labels are unnecessary. If a break statement is missing for a particular case , control flows through to the subsequent case and the program executes the sequence under that case as well.

The program executes the sequence following default only if the condition does not match any of the case constants. The default case is optional and this keyword may be omitted. For example, the following code snippet compares the value of choice to 'A' or 'a' , 'B' or 'b' , and 'C' or 'c' until successful. If unsuccessful, the code snippet executes the statements under default. The conditional selection construct is shorthand for the alternative path construct. This ternary expression combines a condition and two sub-expressions using the?

If the condition is true, the expression evaluates to the operand between? If the condition is false, the expression evaluates to the operand following :. If the operands in a conditional expression are themselves expressions, the conditional expression only evaluates the operand identified by the condition. Three instructions control the execution of an iteration: an initialization, a test condition, a change statement.

The test condition may be simple or compound. The flow charts for the three constructs are shown below. If the change statement is missing or if the test condition is always satisfied, the iteration continues without terminating and the program can never terminate. We say that such an iteration is an infinite loop. The while construct executes its sequence as long as the test condition is true.

The do while construct executes its sequence at least once and continues executing it as long as the test condition is true. This code probably contains a semantic error: if the initial value was 5, the iteration would never end. The for construct groups the initialization, test condition and change together, separating them with semi-colons. Flagging is a method of coding iteration constructs within the single-entry single-exit rule of structured programming.

Consider the flow-chart on the left side in the figure below. This design contains a path that crosses another path. Flags are variables that determine whether an iteration continues or stops.

A flag is either true or false. Flags helps ensure that no paths cross one another. By introducing a flag, we avoid the jump and multiple exit, obtain a flow chart where no path crosses any other and hence an improved design. The following code snippet uses a flag to terminate the iteration prematurely. The test condition is compound due to the evaluation of the flag. Designing a program with jumps or intersecting paths makes it more difficult to read.



0コメント

  • 1000 / 1000