Iteration Statements – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Wed, 13 Mar 2024 18:12:19 +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 Iteration Statements – Programmerbay https://programmerbay.com 32 32 Difference between While and Do While loop in Tabular Form https://programmerbay.com/difference-between-while-and-do-while-loop/ https://programmerbay.com/difference-between-while-and-do-while-loop/#respond Mon, 11 Mar 2024 16:17:31 +0000 https://programmerbay.com/?p=6343 In this article we’ll be discussing the difference between while and do while loop and how they work.

Control statements provide an ability to execute a particular set of statements in a controlled manner. Basically, these statements are powerful statements that can mange the flow of a program.

Every programming language supports a basic form of Iteration statements that are known as loops, for example, While, Do-While and For loop.

 

While and do while loop

Iteration statements are categorised as the control statements. It allows us to iterate through a piece of code repeatedly.

The goal of all looping statements is to run a particular block of statements frequently until a specific condition is met. However, Do-While and While loop follows a completely different approach to looping around a statement. Both types of loops are different from each other when it comes to their order of execution and condition evaluation.

What is a loop?

A loop can be defined as a control statement that enables repetitive execution of a block of code until a given condition turns out to be false. It provides capability to write a block of code and execute it repeatedly for specific number of times. Suppose, if a block of code needs to be executed repeatedly, in such case, we must use loops, instead of writing the same code multiple times.

Normally a looping statement comprises of following components:

  • Control or counter variable initialization
  • Condition evaluation
  • Loop body execution
  • Counter variable updation

There are primarily three types of loops  :
1) while loop
2) do while loop
3) for loop

The main difference between While and Do-While loop is that one evaluates condition first and then executes the loop body, whereas, other one executes the loop body first and then checks for the condition.

Difference between While and Do While Loop in Tabular form

BasisWhile LoopDo-While Loop
DefinitionIn this, the given condition is evaluated first, and then loop body is executedIn this, the given loop body is executed first and then after the given condition is checked
Loop TypeIt is an entry-controlled loopIt is an exit-controlled loop
Loop ExecutionThe 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
Counter Variable Intialization It allows initialization of counter variable before entering loop bodyIt allows initialization of counter variable before and after entering loop body
SemicolonNo semicolon is used as a part of syntax,
while(condition)
Semi-colon is used as a part of syntax,

while(condition);
UsageIt is used when condition evaluation is required to be evaluated first, before executing loop bodyDo-While is used when one needs to enter into the loop body before evaluating condition. For example, Menu driven programs
SyntaxSyntax:

while( condition )
{
// loop body
}
do
{
// loop body
}while( condition );

While Loop

While Loop can be defined as the loop where the condition is evaluated first and then after the loop body is executed. It is an entry controlled loop.

Syntax:

while(condition)
{
// body
}

How while loop works?

  • Counter or control variable initialization
  • Then, condition would be checked
  • If it is evaluated as true, then
  • loop body would be executed
  • Otherwise, the loop gets terminated

Entry Controlled Loop

A Program example for While loop:

Example 1. C program to demonstrate working of while loop

Program

#include <stdio.h>

int main ()
{

  int i = 1;
  while (i < 5)
    {
      printf ("Print :: %d \n", i);
      i++;
    }
  return 0;
}

Output:

Print :: 1 
Print :: 2 
Print :: 3 
Print :: 4 

Explanation:

In the above example, we are simply initializing counter variable i with 1. And iterating using while loop until the condition  having i is less than 5 becomes false. In loop body, we are printing a message and incrementing counter variable on very next line.

Example 2. C program to show the approach difference of while loop with do-while

#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:

Since, condition would be evaluated as false at very first place , no output would be printed. In this, counter variable ‘i’ was holding value 10. And at the entry point of while loop, when condition got evaluated for ‘i< 10’, it turned out to be false. As a result, the loop terminated.

Do-While Loop

Completely opposite to While loop, Do-While is a looping statement in which condition is checked after the execution of the loop body. It is a type of exit controlled loop.

Syntax:

do
{
// loop body

}whie(condition);

How do-While works?

  • Counter or control variable initialization
  • Loop body gets executed first and then-after the condition is evaluated
  • If the condition is evaluated as true, then loop would proceed to next iteration
  • Otherwise, it gets terminated

Exit Controlled Loop

A  Program example of Do-While loop

Example 1. C program to demonstrate working of do while loop

Program:

#include <stdio.h>

int main ()
{
  int i = 1;
  do
    {
      printf ("Print :: %d \n", i);
      i++;
    }while (i < 5);
  return 0;
}

Output:

Print :: 1 
Print :: 2 
Print :: 3 
Print :: 4 

Explanation:

In the above example, we are initializing counter variable i with 1. And iterating using do while loop until i is less than 5. In loop body, we are printing a message and incrementing counter variable on very next line. Lastly, we’re checking the condition and iterate accordingly.

Example 2. C program to show the approach difference of do-while loop with while

#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:

Print statement would be executed at once, even though the condition is false. In this, counter variable ‘i’ was 10. In this, the loop body got executed first which resulted in print statement execution. And lastly, condition got evaluated for ‘i< 10’, it turned out to be false. As a result, the loop terminated.

Exit Controlled and Entry Controlled Loop

A loop that executes the loop body first and then checks for a condition is known as an exit controlled loop. For example Do-While loop.

On the other hand, a loop that checks for condition first and run the loop body can be categorized as entry controlled loop. For example, While loop, and For loop.

]]>
https://programmerbay.com/difference-between-while-and-do-while-loop/feed/ 0
What are Java Iteration Statements : Control Statements https://programmerbay.com/what-are-java-iteration-statements-control-statements/ https://programmerbay.com/what-are-java-iteration-statements-control-statements/#respond Sat, 14 Sep 2019 17:38:31 +0000 https://programmerbay.com/?p=5125 These are the statements that enable us to execute a particular block repeatedly until a given condition returns false. Java supports While, For and Do-While statements.

While

It runs a particular block until a control expression becomes false. As and when the condition gets false, control passes to the statement that is immediately after the loop.

Syntax:

while(condition)
{
//block
}

Do-while

In this, first loop body is executed and then after the condition is checked. It is a special loop in which control expression is checked after the execution of the block. The loop body runs at least once, no matter what condition is.

Syntax:

do{
//body
}while(condition)

For

It is a type of loop where the control variable, condition, and iteration are all put together at a single place.

A control variable acts as a counter and is executed once. After that, the condition is evaluated if it is true the loop body would be executed otherwise, it gets terminated. Lastly, the iteration portion is executed. In this  control variable is incremented or decremented. The incremented or decremented value is checked with the condition and if it returns true, the loop body would be executed. It keeps iterating until the condition returns false.

Syntax:

 for(control_variable;condition;increment or decrement)
{
//body
}

There is an advanced version of For which is referred to as For-each. However, it doesn’t require any keyword to declare this statement.

For-each is used to iterate through a collection of objects until all the elements are iterated.

for(type element_name:collection)
{
//body
}

Java program to demonstrate the working of the iteration statement

Program:

public class IterationStatement {

public static void main(String[] args) {

int i=0;
int control_variable;
int arr[] = {1,2,3,4};

while(i<5)
{
System.out.println("I am iterating using While Loop "+i+" time ");
i++;
}
// do while
i=0;
do{
System.out.println("I am iterating using Do-While Loop "+i+" time ");
i++;
}while(i<5);
// For loop

for(control_variable =0;control_variable<5;control_variable++)
{
System.out.println("I am iterating using For Loop "+control_variable+" time ");

}

// For-Each
for(int element:arr)
{
System.out.println("I am iterating using For-each Loop "+element+" time ");
}


}

}

Output:

iteration statement

]]>
https://programmerbay.com/what-are-java-iteration-statements-control-statements/feed/ 0