What is the difference between looping structure and conditional structure




















It is different in do while loop which we will see shortly. The above program illustrates the use of while loop. In the above program, we have printed series of numbers from 1 to 10 using a while loop.

A do…while loop in C is similar to the while loop except that the condition is always executed after the body of a loop. It is also called an exit-controlled loop. As we saw in a while loop, the body is executed if and only if the condition is true. In some cases, we have to execute a body of the loop at least once even if the condition is false.

This type of operation can be achieved by using a do-while loop. In the do-while loop, the body of a loop is always executed at least once. After the body is executed, then it checks the condition.

If the condition is true, then it will again execute the body of a loop otherwise control is transferred out of the loop. Similar to the while loop, once the control goes out of the loop the statements which are immediately after the loop is executed.

The critical difference between the while and do-while loop is that in while loop the while is written at the beginning. In do-while loop, the while condition is written at the end and terminates with a semi-colon ;.

In the above example, we have printed multiplication table of 2 using a do-while loop. The general structure of for loop syntax in C is as follows:. Notice that loops can also be nested where there is an outer loop and an inner loop. For each iteration of the outer loop, the inner loop repeats its entire cycle. Consider the following example with multiple conditions in for loop, that uses nested for loop in C programming to output a multiplication table:.

The nesting of for loops can be done up-to any level. The nested loops should be adequately indented to make code readable. The break statement is used mainly in in the switch statement. It is also useful for immediately stopping a loop.

For more information, see Do Loop Statement. The For Next construction performs the loop a set number of times.

It uses a loop control variable, also called a counter , to keep track of the repetitions. You specify the starting and ending values for this counter, and you can optionally specify the amount by which it increases from one repetition to the next.

For more information, see For Next Statement. The For Each Next construction runs a set of statements once for each element in a collection. You specify the loop control variable, but you do not have to determine starting or ending values for it. You must be logged in to post a comment.

Control Structures vs Control Statements Summary: Difference Between Control Structures and Control Statements is that when programmers are required to design the logic of a program, they typically use control structures to describe the tasks a program is to perform.

Control Structures When programmers are required to design the logic of a program, they typically use control structures to describe the tasks a program is to perform. Sequence Control Structure A sequence control structure shows one or more actions following each other in order. Selection Control Structure A selection control structure tells the program which action to take, based on a certain condition.

Repetition Control Structure The repetition control structure enables a program to perform one or more actions repeatedly as long as a certain condition is met. Control Statements A Control Statement is a statement that control the execution of other statements. There are different types of control statements: if Statement helps program to understand whether statement 1 is going to be executed or statement 2.

Three types of loops are: While loop tests whether a condition is true before executing the controlled statement.

You may also like. Leave a Comment X You must be logged in to post a comment.



0コメント

  • 1000 / 1000