To install click the Add extension button. That's it.

The source code for the WIKI 2 extension is being checked by specialists of the Mozilla Foundation, Google, and Apple. You could also do it yourself at any point in time.

4,5
Kelly Slayton
Congratulations on this excellent venture… what a great idea!
Alexander Grigorievskiy
I use WIKI 2 every day and almost forgot how the original Wikipedia looks like.
Live Statistics
English Articles
Improved in 24 Hours
Added in 24 Hours
What we do. Every page goes through several hundred of perfecting techniques; in live mode. Quite the same Wikipedia. Just better.
.
Leo
Newton
Brights
Milds

Conditional loop

From Wikipedia, the free encyclopedia

In computer programming, conditional loops or repetitive control structures are a way for computer programs 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 languages.

YouTube Encyclopedic

  • 1/3
    Views:
    4 257
    134 097
    7 835
  • Programming Basics: Conditional Statements And Loops
  • While Loops - Intro to Computer Science
  • Conditional Constructs & for Loop | Python Tutorial #5 | TalentSprint

Transcription

Types

The following types are written in C++, but apply to multiple languages.

While loop

Checks condition for truthfulness before executing any of the code in the loop.[1] If condition is initially false, the code inside the loop will never be executed. In PL/I this is a DO WHILE... statement.

while (condition) {
    // code
}

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. PL/I implements this as a DO UNTIL... statement.

do {
    // code
} while (condition);

For loop

A simplified way to create a while loop.[2]

for (initialization; condition; statement) {
    // code
}

Initialization is executed just once before the loop. Condition evaluates the boolean expression of the loop. Statement is executed at the end of every loop.

So for example, the following while loop:

int i = 0;

while (i < 10) {
    // code
    
    i += 1;
}

Could be written as the following for loop:

for (int i = 0; i < 10; ++i) {
    // code
}

For-Each loop

A for-each loop is essentially equivalent to an iterator. 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:

std::vector<std::string> range = { "apple", "banana", "orange" };

for (auto item: range) {
    // code
}

Examples

The following is a C-style 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
    
    x += 1;
}

Frequent bugs

Conditional loops are often the source of an Off by one error.

References

  1. ^ "while loop - cppreference.com". en.cppreference.com. Retrieved 2023-11-07.
  2. ^ "for loop - cppreference.com". en.cppreference.com. Retrieved 2023-11-07.
This page was last edited on 7 November 2023, at 20:30
Basis of this page is in Wikipedia. Text is available under the CC BY-SA 3.0 Unported License. Non-text media are available under their specified licenses. Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc. WIKI 2 is an independent company and has no affiliation with Wikimedia Foundation.