loop – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Sun, 21 Aug 2022 07:42:28 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg loop – Programmerbay https://programmerbay.com 32 32 Difference Between Entry Controlled Loop and Exit Controlled Loop https://programmerbay.com/difference-between-entry-controlled-loop-and-exit-controlled-loop/ https://programmerbay.com/difference-between-entry-controlled-loop-and-exit-controlled-loop/#respond Sun, 24 Jul 2022 08:47:42 +0000 https://www.programmerbay.com/?p=2229 In this article, we’ll be discussing the meaning of loop and also see the difference between Entry Controlled Loop and Exit Controlled Loop with program example.

Entry vs exit controlled loop

What is a Loop?

A loop empowers repetitive execution of a certain piece of code until a given condition turns out to be false.

In simple terms, it enables us to run a particular sequence of statements repeatedly based on a certain condition.

Generally a looping process involves:

  • Control variable initialization
  • Condition Evaluation
  • Loop body execution
  • Control Variable Updation

There are mainly three types of loops:

1) While Loop : loops through a piece of code as long as a specified expression is evaluated as true
2) Do While Loop : similar to while loop, except Do-While loop is guaranteed to execute at least one time
3) For Loop : executes loop body for specific number of times ( allows control over number of iterations)

In programming, these are considered as controlled statements that can regulate the flow of the program execution.

They use a conditional expression in order to decide to what extent a particular block should be repeated.

Every loop consists of two sections, loop body and control statement.

A control statement is a condition that dictates how long a loop would be iterated and the loop body is a block that contains the intended iterative statements.

In case of While Loop :

while(condition)    // Control Statement
  {
   // Loop Body
  }

In case of Do-While Loop :

do{
   // Loop Body      
  }while(condition);   // Control Statement

In case of For Loop :

for(controlVariableInitialization ;condition; increment/decrement)   // Control Statement
    {
     // Loop Body
    }

 

Based on the position of these two sections, loop execution can be handled in two ways that are at the entry-level and exit level.

So, loops can be categorized into two types:

Entry controlled loop: When a condition is evaluated at the beginning of the loop.
Exit controlled loop: When a condition is evaluated at the end of the loop.

Also Read: Difference between While and Do-While Loop

Entry Controlled Loop

An entry control loop checks condition at entry level (at beginning ), that’s why it is termed as entry control loop.

It is a type of loop in which the condition is checked first and then after the loop body executed. For loop and While loop fall in this category.

If the test condition is true, the loop body would be executed otherwise, the loop would be terminated.

 

Entry control loop flow chart

 

Execution Flow of Entry Control Loop

  1. The control variable is initialized first and acts as a base for comparison
  2. This variable is then evaluated with a conditional statement.
  3. If the condition is evaluated as false, the loop would be terminated, otherwise
  4. The loop body would be executed and go to the next iteration, where the variable would get updated with new incremented/ decremented value
  5. Repeat 2nd to 4th steps until the condition is said to be false.

Program to show the working of entry controlled loop:

#include<stdio.h>
void main()
{
int i=10;
while(i<10)
{
printf("I will not be executed as it is entry controlled loop");
i++;
}
getch();
}

Explanation:

In the above code, No output would be displayed as the condition is false. We used ‘i’ as a counter variable and assigned it 10.  Loop condition ‘i<10 ‘got evaluated as false . As a result, the compiler transferred the control to the next statement after the loop.

Exit Controlled Loop

An exit control loop checks condition at exit level (in the end ), that’s why it is termed as exit control loop. Oppose to Entry controlled loop, it is a loop in which condition is checked after the execution of the loop body. Do-While loop is the example.
The loop body would be executed at least once, no matter if the test condition is true or false.

Exit control loop flow chart

 

Execution Flow of Exit Control Loop

  1. The control variable is initialized first and acts as a base for comparison
  2. Then, the loop body is executed and the variable would get also updated with new incremented/ decremented value
  3. This variable is then evaluated with a conditional statement.
  4. If the condition is evaluated as false, the loop would be terminated, otherwise
  5. It repeats, 2nd to 4th steps until the condition is said to be false.

Program to show the working of exit controlled loop:

#include<stdio.h>
void main()
{
  int i=10;
  do
  {
  printf("I will be executed at once as it is exit controlled loop");
  i++;
  }while(i<10);
  getch();
}

Explanation:

In the above code, print statement would be executed at least once even though condition is false.

We used ‘i’ as counter variable and assigned it 10. In this, loop body got executed first as a result a message got printed on screen. After that, condition was checked which evaluated as false, this terminated the loop.

Difference between entry controlled loop and exit controlled loop in Tabular form ?

BasisENTRY CONTROLLED LOOPEXIT CONTROLLED LOOP
Execution FlowA loop in which the given condition is checked first, before entering the loop bodyA loop in which the loop body is executed first and then after the given condition is checked
Condition EvaluationThe loop body would be executed, only if the given condition is trueThe loop body would be executed at least once, even if the given condition is evaluated as false
ExampleFor Loop and While Loop are examples of this type of loopDo While Loop is an example of exit controlled loop
UseIt is used when condition evaluation is mandatory before executing loop bodyIt is used when one requires to iterate through loop body at least once before condition evaluation

Key Differences

  1. Exit control loop always executes at least once, regardless of condition. But, the entry control loop only executes if and only if the condition is evaluated as true.
  2. In this type of loop, body execution comes first, whereas in, entry control loop condition expression always comes first.
  3. Do-While falls under the exit control loop, whereas For loop and While loop falls under entry control loop

]]>
https://programmerbay.com/difference-between-entry-controlled-loop-and-exit-controlled-loop/feed/ 0
Difference Between Continue And Break Statement in C++ in Tabular Form https://programmerbay.com/break-and-continue-statement-in-c/ https://programmerbay.com/break-and-continue-statement-in-c/#respond Sun, 22 Mar 2020 09:14:07 +0000 https://www.programmerbay.com/?p=2627 In C or C++, break and continue statements are the control statements that can change the flow of program execution.

The main difference between break and continue is, break statement is used to break the loop execution based on some conditional expression, whereas the continue statement skips the code that comes after it in a loop and begins a new iteration based on a certain condition.

Difference Between Continue And Break Statement in C++ in Tabular Form

BasisBreak StatementContinue Statement
DefinitionIt is a control statement that is responsible for immediate termination of current enclosing loop (innermost loop in case of nested loop statements)It is a control statement that is responsible for termination of current iteration of the loop and start next iteration again, without processing statements that come after it within the enclosing loop body
UseIt is widely used in switch and loopsIt is used in loops only
Functionality It terminates iteration of the loop or switch statement( in case of loops, it forces to exit from the loop and execute the code which immediate loop, in case of Switch it terminates the statement)It skips the statements that come after it and goes for the next iteration of the loop
BehaviourWhen the break statement is encountered within the loop or switch statement, the execution is stopped immediatelyWhen the continue statement is encountered within the loop during program execution, the statements after it is skipped and the control of loop jumps to the next iteration
TerminationIt causes early termination of the current loopIt causes an early execution of the next iteration of the loop
Program flowIt typically stops the continuation of the current loop of the programIt only stops the current loop iteration but does not hampers the continuation of the loop in the program

Break Statement

A break statement is a control statement that terminates the execution of a loop or a block in which it occurs. Therefore, it breaks the flow of a program.

In C++, the break statement is mainly used in these two cases :

a) Within a loop: When a break is triggered in a loop, it stops the loop execution immediately and transfers the control to the very next statement after it.

Figure: Break Statement Working inside a loop
Figure: Break Statement Working inside a loop

In the case of a nested loop, if a break is present in an inner loop, then it would terminate only the execution of that particular loop only.

b) In Switch Statement: In order to prevent consecutive execution of cases, every case in the switch statement comes with a break statement.

Syntax :

break ;

C++ program to show the working of Break Statement : 

#include<iostream> 
using namespace std; 
int main() {
    for(int a=0;a<5;a++) 
    {if(a==3) 
    break; 
    cout<<a<<" ";
    } 
    return 0; 
}

Output :

0 1 2

Continue Statement

It only terminates the current iteration of the loop and starts the next iteration again, without processing statements that come after it within the enclosing loop body. It is used with looping statements only.

continue statement 1
Figure: Continue Statement Working inside a loop

 

In simple words, it skips the current iteration and jumps to the next loop iteration without executing statements in the loop body that comes after it.

Syntax : 

continue ;

C++ program to show the working of Continue Statement : 

#include<iostream> 
using namespace std; 
int main() 
{ 
    for(int a=0;a<5;a++) 
    { 
        if(a==3) 
        continue; 
        cout<<a<<" "; 
    } 
    return 0; 
}

Output : 

0 1 2 4

]]>
https://programmerbay.com/break-and-continue-statement-in-c/feed/ 0