Conditional loop
Encyclopedia
In computer programming
, conditional loops or repetitive control structures are a way for computer program
s to repeat one or more various steps depending on conditions set either by the programmer
initially or real-time by the actual program.
A conditional loop has the potential to become an infinite loop
when nothing in the loop's body can affect the outcome of the loop's conditional statement
. However, infinite loops can sometimes be used purposely, often with an exit from the loop built into the loop'implementation for every computer language, but many share the same basic structure and/or concept. The While loop
and the For loop
are the two most common types of conditional loops in most programming language
s.
a is executed just once before the loop. b is the condition of the loop. c is executed at the end of every loop.
So for example, the following while loop:
Could be written as the following for loop:
. It allows a program to iterate through a data structure without having to keep track of an index. It is especially useful in Sets which do not have indices. An example is as follows:
While loop
. It continues looping while x does not equal 3, or in other words it only stops looping when x equals 3. However, since x is initialized to 0 and the value of x is never changed in the loop, the loop will never end (infinite loop
).
int x = 0;
while ( x != 3 )
{
/* code that doesn't change x */
}
The while loop below will execute the code in the loop 5 times. x is initialized to 0, and each time in the loop the value of x is incremented. The while loop is set up to stop when x is equal to 5.
int x = 0;
while ( x != 5 )
{
/* code here */
x = x + 1;
}
Computer programming
Computer programming is the process of designing, writing, testing, debugging, and maintaining the source code of computer programs. This source code is written in one or more programming languages. The purpose of programming is to create a program that performs specific operations or exhibits a...
, conditional loops or repetitive control structures are a way for computer program
Computer program
A computer program is a sequence of instructions written to perform a specified task with a computer. A computer requires programs to function, typically executing the program's instructions in a central processor. The program has an executable form that the computer can use directly to execute...
s to repeat one or more various steps depending on conditions set either by the programmer
Programmer
A programmer, computer programmer or coder is someone who writes computer software. The term computer programmer can refer to a specialist in one area of computer programming or to a generalist who writes code for many kinds of software. One who practices or professes a formal approach to...
initially or real-time by the actual program.
A conditional loop has the potential to become an infinite loop
Infinite loop
An infinite loop is a sequence of instructions in a computer program which loops endlessly, either due to the loop having no terminating condition, having one that can never be met, or one that causes the loop to start over...
when nothing in the loop's body can affect the outcome of the loop's conditional statement
Conditional statement
In computer science, conditional statements, conditional expressions and conditional constructs are features of a programming language which perform different computations or actions depending on whether a programmer-specified boolean condition evaluates to true or false...
. However, infinite loops can sometimes be used purposely, often with an exit from the loop built into the loop'implementation for every computer language, but many share the same basic structure and/or concept. The While loop
While loop
In most computer programming languages, a while loop is a control flow statement that allows code to be executed repeatedly based on a given boolean condition. The while loop can be thought of as a repeating if statement....
and the For loop
For loop
In computer science a for loop is a programming language statement which allows code to be repeatedly executed. A for loop is classified as an iteration statement....
are the two most common types of conditional loops in most programming language
Programming language
A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely....
s.
While loop
Checks condition for truthfulness before executing any of the code in the loop. If condition is initially false, the code inside the loop will never be executed.Do-While loop
Checks condition for truthfulness after executing the code in the loop. Therefore, the code inside the loop will always be executed at least once.For loop
A simplified way to create a while loop.a is executed just once before the loop. b is the condition of the loop. c is executed at the end of every loop.
So for example, the following while loop:
Could be written as the following for loop:
For-Each Loop
A for-each loop is essentially equivalent to an iteratorIterator
In computer programming, an iterator is an object that enables a programmer to traverse a container. Various types of iterators are often provided via a container's interface...
. It allows a program to iterate through a data structure without having to keep track of an index. It is especially useful in Sets which do not have indices. An example is as follows:
Examples
The following is a C-styleC (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....
While loop
While loop
In most computer programming languages, a while loop is a control flow statement that allows code to be executed repeatedly based on a given boolean condition. The while loop can be thought of as a repeating if statement....
. It continues looping while x does not equal 3, or in other words it only stops looping when x equals 3. However, since x is initialized to 0 and the value of x is never changed in the loop, the loop will never end (infinite loop
Infinite loop
An infinite loop is a sequence of instructions in a computer program which loops endlessly, either due to the loop having no terminating condition, having one that can never be met, or one that causes the loop to start over...
).
int x = 0;
while ( x != 3 )
{
/* code that doesn't change x */
}
The while loop below will execute the code in the loop 5 times. x is initialized to 0, and each time in the loop the value of x is incremented. The while loop is set up to stop when x is equal to 5.
int x = 0;
while ( x != 5 )
{
/* code here */
x = x + 1;
}