Can you convert a for loop to a while loop list the advantages of using for loops

Overview

This article explores some of the basic functions and uses of For Loops and While Loops in LabVIEW and the appropriate times to use them. Tutorials for both the For Loop and While Loop structures are available if you are a beginner programmer or someone looking for more instruction on configuring these loops.

A While Loop is a structure you use to execute a block of LabVIEW code repeatedly until a given condition is met. When the VI runs, the code inside the While Loop executes, and then the terminal condition is evaluated. The While Loop will be a familiar concept for experienced programmers as it operates similarly in other computer languages.

Can you convert a for loop to a while loop list the advantages of using for loops

LabVIEW While Loop flowchart

Unlike a For Loop, While Loop execution does not depend on iteration count; thus, a While Loop executes indefinitely if the condition never occurs.

For more information on what a While Loop is, including its components and configuration options, look into While Loops in LabVIEW Help.

A For Loop is a structure you use to execute a block of code a set number of times. When the VI runs, the iteration count is evaluated, and then the code is executed.

LabVIEW For Loop flowchart

A For Loop can be configured to conditionally stop code execution in addition to its iteration-based exit. In these cases, the code will execute until the count terminal setting is reached or the condition is met – whichever happens first.

While Loops should be used when:

  • You want your code to run indefinitely
  • You want to run code until a condition is met
  • You want user control over when code should stop
     

For Loops should be used when:

  • You need to run code for a certain number of times
  • You want to write a set number of measurements to a file
  • You want to take a certain number of data points
  • You want to run code until a condition is met OR a set number of iterations, whichever comes first

Was this information helpful?

The while loop is used to repeat a section of code an unknown number of times until a specific condition is met. For example, say we want to know how many times a given number can be divided by 2 before it is less than or equal to 1. If we know a specific number, such as 32, we can say 5 times, but for a given symbolic variable "NUMBER" which represents any number in the world, how many times is not known a priori (before hand). In this case, we could use a while loop to determine that answer:

The "pseudocode" for such an algorithm is: while the number is bigger than one keep dividing it by two. additionally, keep a count of how many times we do the division.

Pseudocode

            
                get our number
                set our initial count to 0
                while our number is greater than 1
                  divide the number by 2
                  increase our count by 1
                end
            
          

Matlab

            
                count = 0;
                while (number > 1)
                  number = number / 2; %  must "move" toward end of loop
                  count  = count + 1; 
                end
            
          

C, C++, or Java

            
                int count = 0;
                while (number > 1)
                {
                  number = number / 2;
                  count++; // the same as count = count + 1;
                }
            
          

Actionscript

            
                var count:int = 0;
                while (number > 1)
                {
                  number = number / 2;
                  count++; // the same as count = count + 1;
                }
            
          


Why While Loops?

  1. Like all loops, "while loops" execute blocks of code over and over again.

  2. The advantage to a while loop is that it will go (repeat) as often as necessary to accomplish its goal.

  3. Generic Syntax:

                
      while ( condition is true )
        do something
        % Note: the "something" should eventually result
        % in the condition being false
      end
                
              
  4. Infinite loops:

    If the action inside the loop does not modify the variables being tested in the loops condition, the loop will "run" forever. For example:

                
      while ( y < 10 )
        x = x + 1;
      end
                
              
                
      while (  true )
        printf('hello');
      end
                
              

Example 1: How to assure proper input

  1. Ask the user to input a value.
  2. while the input is incorrect.
  3. ask the user to input another value.
  4. go back to line 2 (the while)

Matlab

            
% MATLAB
%
% Using a while loop to ask the user to input a number
% between 1 and 10 (inclusive).
%
%   Variables:
%        value  : variable to store the input 
%

value = input ('Please Enter a Number between 1 and 10 (1-10)');

while ( value < 1 || value > 10)

  fprintf('Incorrect input, please try again.\n');
  value = input ('Enter a Number between 1 and 10 (1-10)');

end % while
            
          

C

            
/*
 * C
 * Using a while loop to ask the user to input a number
 * between 1 and 10 (inclusive).
 *
 *   Variables:
 *        value  : variable to store the input 
 */
printf("Please Enter a Number between 1 and 10 (1-10): ");
scanf("%d", &value);

while ( value < 1 || value > 10)
  {

    printf("Incorrect input, please try again.\n");
    printf("Enter a Number between 1 and 10 (1-10): ");
    scanf("%d", &value);

  }

            
          


Design Pattern:

A design pattern is the syntax that you have to memorize in order to do well in programming and on tests.

The design pattern for a while loop is:

Matlab

            
            while ( some condition is true )
              % Do this code 
              % Something here should modify the condition above 
            end
            
          

C, Java, or Actionscript

            
          while ( some condition is true )
            {
              // Do this code 
              // Something here should modify the condition above 
            }
            
          


Back to Topics List

What are the advantages of a for loop over a while loop?

The main advantage of a for loop over a while loop is readability. A For loop is a lot cleaner and a lot nicer to look at. It's also much easier to get stuck in an infinite loop with a while loop.

Can we convert for loop to do

To convert a for loop to while loop we need to simply add the initialization statement before the while loop.

What is the advantage of using for loop in your program?

Why For Loops? Like all loops, "for loops" execute blocks of code over and over again. The advantage to a for loop is we know exactly how many times the loop will execute before the loop starts.

Why while loop is better than for loop?

Use a for loop when you know the loop should execute n times. Use a while loop for reading a file into a variable. Use a while loop when asking for user input. Use a while loop when the increment value is nonstandard.