Javatpoint Logo
Javatpoint Logo

What is iteration in data structure?

Iteration refers to repeating a certain number of steps continuously until a particular condition is met successfully. The iterations can be an infinite number of times or an infinite number of times. It all depends on the program in which we are performing the iterations.

Iterations play a very important role in performing the same instructions and for repeating the same lines of cone codes one over another. Sometimes iteration is worthless and creates a deadlock when the condition falls in the infinite loop.

  • In the operating system, we have read about the term critical section, in which we have multiple numbers of processes wanted to access the critical section. Still, as per the criteria of the critical section, one process at a single time can only access it until further another process must wait.
  • So to solve the problem of the critical section, we will use the semaphores. Based on some conditions, if the value of the semaphore is 1, then the process may enter into the critical section and down the semaphore immediately by decrementing it with 0 so that in the presence of the current process in the critical section, another process is unable to enter into the critical section.
  • If the process arrives and sees the value of the semaphore is 0, then it will ultimately be stuck into the infinite loop, and in this case, infinite iterations solve the problem and prove to be helpful.
  • Now let us try to understand it in more detail by taking some other examples as, if we want to print the hello statement 10 times, we will have two ways to do so.
  • One way is to directly type the print statement to print the hello 10 times, one after the other.
  • But the above case will ultimately waste time and an unnecessary increase in memory space and code lines. If we have to print 10,000 times, don't we even try to attempt this method because it does not make the code efficient.
  • The second way is to run a loop and iterate the hello statement 10 times and save our time, reduce the unnecessary lines of code, and make the code much more efficient than the previous method. We will choose the second method and move towards the efficient method.
  • So think how much the iterations play an important role in running any program efficiently and saving the programmer's time.

Program to print the " Hello world " 10 times

The output of the above program

Hello, world!
  Hello, world! 
  Hello, world!
  Hello, world!
  Hello, world!
  Hello, world!
  Hello, world!
  Hello, world!
  Hello, world!
  Hello, world!

The output of the above program

Hello, world!
  Hello, world!
  Hello, world!
  Hello, world!
  Hello, world!
  Hello, world!
  Hello, world!
  Hello, world!
  Hello, world!
  Hello, world!

As we analyze the above cases, we will easily get to know the importance of the iterations in the program and how it will reduce the extra efforts of the coder. It ultimately saves time and makes the code more efficient and readable.

Methods to perform the iterations into the program

There are various methods to perform the iterations in any program are as follows:

1. Using various loops in the program:

  • Using For loop
  • Using while loop
  • Using do-while loop

2. Using recursion

Let us discuss these methods one by one in more detail, with the help of examples. Here we are taking the reference of C and C++ language.

Performing iterations using various types of loops in the program

1. Using various loops in the program

1. Using For loop

Implementing the for loop in C and C++ language is very simple, and it is bound up with the basic syntax are as follows:

In expression 1, we will do the initialization of variables here, and we can do multiple initializations.

In expression 2, we will write the conditional expression, upto which the loop will run.

In expression 3, we will perform the increment and decrement operation, depending on the condition and the programmer's need.

There is a certain procedure for implementing the 'for' loop. First, expression 1, i.e., initialization of the variable, will run, and the variables will initialize as per the mentioned statement inside the for a loop. After initialization, expression 2 will execute, i.e., the checking of the condition will take place, if it fulfills the condition, then the pointer will directly jump inside the loop and execute the number of statements mentioned inside the loop, after executing the statements, the pointer will jump on the expression 3 and perform the incrementation or decrementation on the variable after then it will jump on the expression 2 and again check the condition if it is true. The pointer will move out of the block. It will jump inside the loop and execute the number of statements inside the loop, and again the pointer move on the expression 3, performing incrementation or decrementation on the variable. After that, it will check the condition……… and the whole process will repeat until the condition met false.

In the working of the for loop, we can observe that the expression 1, that is, initialization of the variable, is executed only once, in the starting, after then the pointer will never jump on the initialization part. It will revolve around the expression 2 and expression 3 and inside the loop until the condition meets false.

Let us understand the working of for loop with the help of an example:

The output of the above program

  Value of i is: 1
  Value of i is: 2
  Value of i is: 3
  Value of i is: 4
  Value of i is: 5
  Value of i is: 6
  Value of i is: 7
  Value of i is: 8
  Value of i is: 9
  Value of i is: 10

The output of the above program

Enter terms of fibonacci series
 20
 Fibonacci series is :
  0  1  1  2  3  5  8  13  21  34  55  89  144  233  377  610  987  1597  2584  4181
 0.000000 secs

2. Using While loop:

The working of the while loop in the C/C++ language is the same as the working of the for loop in the C/C++ language. The syntax of the while loop is quite different from the for loop, and syntax is as follows:

In expression 1, we will do the initialization of variables here, and we can do multiple initializations.

In expression 2, we will write the conditional expression, upto which the loop will run.

In expression 3, we will perform the increment and decrement operation, depending on the condition and the programmer's need.

There is a certain procedure for implementing the while loop. First, expression 1, i.e., initialization of the variable, will run, and the variables will initialize as per the mentioned statement inside the while loop. After initialization, expression 2 will execute, i.e., the checking of the condition will take place, if it fulfills the condition, then the pointer will directly jump inside the loop and execute the number of statements mentioned inside the loop, after executing the statements, the pointer will jump on the expression 3 and perform the incrementation or decrementation on the variable. It will jump on the expression 2 and again check the condition if it is true. The pointer will move out of the block. It will jump inside the loop and execute the number of statements inside the loop, and again the pointer move on the expression 3, performing incrementation or decrementation on the variable. After that, it will check the condition……… and the whole process will repeat until the condition met false.

As we observe, the working procedure of both for loop and while loop is the same; in for loop, the initialization statement is executed once a time similarly in this while the initialization statement is executed once before entering in the while loop block.

Let us understand it with the help of an example:

The output of the above program

  Value of i is: 1
  Value of i is: 2
  Value of i is: 3
  Value of i is: 4
  Value of i is: 5
  Value of i is: 6
  Value of i is: 7
  Value of i is: 8
  Value of i is: 9
  Value of i is: 10

The output of the above program

Enter terms of fibonacci series
 20
 Fibonacci series is :
  0  1  1  2  3  5  8  13  21  34  55  89  144  233  377  610  987  1597  2584  4181
 0.000000 secs

3. Using do-while loop:

The procedure to use a do-while loop is different from the procedure we follow in for loop and while loop. As we have already seen the working of the while loop and for loop, in both of the above loops, we first initialize the variable's value, check the condition, and enter inside the loop's block. But here in this do-while loop, the procedure is quite different from the above two procedures; in this loop, we will first initialize the value of a variable, then we will directly enter into the loop without checking any condition, execute the number of statements mentioned into the do-while block. We will update the value of the variable by incrementing or decrementing depending on the condition, and then finally, we will check the condition.

Here is the syntax of the do-while loop is mentioned below as follows:

In expression 1, we will do the initialization of variables here, and we can do multiple initializations.

In expression 2, we will perform the increment and decrement operation, depending on the condition and the programmer's need.

In expression 3, we will write the conditional expression, upto which the loop will run.

There is a procedure for implementing the do-while loop, first of the expression 1, i.e., initialization of the variable will run, and the variables will initialize as per the mentioned statement inside the do-while loop. After initialization, the pointer will directly jump inside the loop and execute the number of statements mentioned inside the loop; after executing the statements, the pointer will jump on the expression 2, i.e., the pointer will perform the incrementation or decrementation on the variable, after then it will jump on the expression 3, i.e., check the condition that is mentioned av=ccording to the programmers need, once the condition is fulfilled then the pointer will again repeat the same process and directly jump inside the loop and execute the number of statements inside the loop. The pointer again moves on the expression 2, perform incrementation or decrementation on the variable, after checks the condition……… and the whole process will going to repeat until the condition met false. The pointer will move out of the block.

As we observe, the working procedure of both for loop and while loop is the same; in for loop, the initialization statement is executed once a time similarly in this while the initialization statement is executed once before entering in the while loop block.

Let us understand it with the help of an example:

The output of the above program

  Value of i is: 1
  Value of i is: 2
  Value of i is: 3
  Value of i is: 4
  Value of i is: 5
  Value of i is: 6
  Value of i is: 7
  Value of i is: 8
  Value of i is: 9
  Value of i is: 10

The output of the above program

Enter terms of fibonacci series
 20
 Fibonacci series is :
  0  1  1  2  3  5  8  13  21  34  55  89  144  233  377  610  987  1597  2584  4181
 0.000000 secs

ii. Using Recursion

The iteration can be easily performed using the recursion, and the term recursion is nothing. Still, it refers to the function that calls itself repeatedly until it undergoes the false statement. The recursive function doesn't need to be always called from the main function, it can be called from other functions, and various variations can be seen in calling the recursive functions. Sometimes when the function calls itself stuck into the infinite loop because no condition met successfully, and it cannot stop calling itself, such recursions are also known as infinite recursion.

We do not have the particular syntax of the recursion, but here below are mentioned some of the cases through which recursion takes place:

Case 1: When we will call any function from the main function using single-level recursive calling:

In the above case, if we observe that we have the main function, and before the body of the main function, the function Fun is declared before so that it will not generate any kind of error. From the main function, we have called the function Fun by passing actual parameters; after the function call will move to the function Fun directly and start executing the body Fun, in the Fun function, we have formal parameters, taken as temporary valid in the body of the Fun function. In the Fun function, if we notice we have some if condition and in the if condition, we have a certain number of statements that are needed to be executed one by one if the condition goes to be proven. Once the condition becomes true, the Fun will stop calling itself; otherwise, it will move towards the other condition and start calling itself until it meets the false condition and breaks the iterations. Once the Fun stops calling itself, the pointer will move in the main function and start executing it after it and stop once it executes the whole main function.

Case 2: When we will call any function from main function using multilevel recursive calling :

In the above case, if we observe that we have the main function, and before the body of the main function, the functions Fun1 and Fun2 are declared before so that it will not generate any kind of error. From the main function, we have called the function Fun1 by passing actual parameters; after the function call will move to the function Fun1 directly and start executing the body Fun1, in the Fun1 function, we have formal parameters, taken as temporary valid in the body of the Fun1 function. In the Fun1 function, if we notice we have some if condition and in the if condition, we have a certain number of statements that are needed to be executed one by one if the condition goes to be proven. In between the number of statements, we have some other calling of Fun2 function; after calling Fun2 function, the pointer will directly jump on the body of the Fun2 function. In the Fun2 function, we have the same procedure, having one if condition and else part; in case the if the condition is true, then it will stop calling itself; otherwise, it will repeatedly call itself until the condition met false. Once the condition is false, the pointer will come to the position where it is left and execute the remaining statements in the Fun1 function. Once the condition becomes true, the Fun1 will stop calling itself; otherwise, it will move towards the other condition and start calling itself until it meets the false condition and breaks the iterations. Once the Fun1 stops calling itself, the pointer will move in the main function and start executing the main function after it and will stop once it executes the whole main function.

From the above two cases, we have analyzed the working of the recursive functions and how recursive procedures will execute behind the screen. It all executes with the help of a stack; the current calling is pushed into the stack one by one and popped out from the stack if the execution is completed. The popping is done based on the LIFO approach; the recently called function will execute first and will be popped first; similarly, in this, the whole procedure goes on. The main function was first pushed into the stack, and at last, it will be popped out from the stack, implying the successful completion of the program.

Case 3: When we will call any function from other function using single level recursive calling:

In the above case, if we observe that we have the main function, and before the body of the main function, the function Fun is declared before so that it will not generate any kind of error. From the main function, we have called the function Fun by passing actual parameters, after the function call will move to the function Fun directly and start executing the body Fun, in the Fun function, we have formal parameters, taken as temporary valid in the body of the Fun function. When we enter the Fun function, we will call the New function, and then the pointer will jump on the body of the New function. In the New function, if we notice we have some if condition and in the if condition, we have a certain number of statements that are needed to be executed one by one if the condition goes to be proven. Once the condition becomes true, the New will stop calling itself; otherwise, it will move towards the other condition and start calling itself until it meets the false condition and breaks the iterations. Once the New stop calls itself, the pointer will move in the Fun function, and then after its completion, the pointer will point to the body of the main function and start executing the main function after it and will stop once it executes the whole main function. The backtracking of the calls to the function is implemented while calling the function.

Let us understand it with the help of an example:

The output of the above program

  Value of i is: 1
  Value of i is: 2
  Value of i is: 3
  Value of i is: 4
  Value of i is: 5
  Value of i is: 6
  Value of i is: 7
  Value of i is: 8
  Value of i is: 9
  Value of i is: 10

The output of the above program

Enter terms of fibonacci series
 20
 Fibonacci series is :
  0  1  1  2  3  5  8  13  21  34  55  89  144  233  377  610  987  1597  2584  4181

The output of the above program

Enter the size of an array: 10
Enter Array
 12
13
15
8
7
9
6
11
10
4
 Enter the value to search: 7
 Value present at index: 4

The output of the above program

Enter the size of the array
 12
Enter Array
 14
12
7
8
9
11
3
1
10
5
 18
13
Enter a value for search: 11
Value present at index: 5  






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA