<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.sarg.dev/index.php?action=history&amp;feed=atom&amp;title=Conditional_loop</id>
	<title>Conditional loop - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.sarg.dev/index.php?action=history&amp;feed=atom&amp;title=Conditional_loop"/>
	<link rel="alternate" type="text/html" href="https://wiki.sarg.dev/index.php?title=Conditional_loop&amp;action=history"/>
	<updated>2026-06-24T09:33:30Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.44.2</generator>
	<entry>
		<id>https://wiki.sarg.dev/index.php?title=Conditional_loop&amp;diff=448752&amp;oldid=prev</id>
		<title>imported&gt;Ggga21: /* growthexperiments-addlink-summary-summary:2|0|0 */</title>
		<link rel="alternate" type="text/html" href="https://wiki.sarg.dev/index.php?title=Conditional_loop&amp;diff=448752&amp;oldid=prev"/>
		<updated>2025-06-29T01:52:51Z</updated>

		<summary type="html">&lt;p&gt;&lt;span class=&quot;autocomment&quot;&gt;growthexperiments-addlink-summary-summary:2|0|0&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{refimprove|date=November 2023}}&lt;br /&gt;
&lt;br /&gt;
In [[computer programming]], &amp;#039;&amp;#039;&amp;#039;conditional loops&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;repetitive control structures&amp;#039;&amp;#039;&amp;#039; 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.&lt;br /&gt;
&lt;br /&gt;
A conditional loop has the potential to become an [[infinite loop]] when nothing in the loop&amp;#039;s body can affect the outcome of the loop&amp;#039;s [[Conditional (programming)|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.&lt;br /&gt;
&lt;br /&gt;
==Types==&lt;br /&gt;
The following types are written in [[C++]], but apply to multiple languages.&lt;br /&gt;
&lt;br /&gt;
===While loop===&lt;br /&gt;
Checks condition for truthfulness before executing any of the code in the loop.&amp;lt;ref&amp;gt;{{Cite web |title=while loop - cppreference.com |url=https://en.cppreference.com/w/cpp/language/while |access-date=2023-11-07 |website=en.cppreference.com}}&amp;lt;/ref&amp;gt; If condition is initially false, the code inside the loop will never be executed. In [[PL/I]] this is a &amp;lt;code&amp;gt;DO WHILE...&amp;lt;/code&amp;gt; statement.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
while (condition) {&lt;br /&gt;
    // code&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Do-While loop===&lt;br /&gt;
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 &amp;lt;code&amp;gt;DO UNTIL...&amp;lt;/code&amp;gt; statement.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
do {&lt;br /&gt;
    // code&lt;br /&gt;
} while (condition);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===For loop===&lt;br /&gt;
A simplified way to create a while loop.&amp;lt;ref&amp;gt;{{Cite web |title=for loop - cppreference.com |url=https://en.cppreference.com/w/cpp/language/for |access-date=2023-11-07 |website=en.cppreference.com}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
for (initialization; condition; statement) {&lt;br /&gt;
    // code&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Initialization&amp;#039;&amp;#039;&amp;#039; is executed just once before the loop. &amp;#039;&amp;#039;&amp;#039;Condition&amp;#039;&amp;#039;&amp;#039; evaluates the [[boolean expression]] of the loop. &amp;#039;&amp;#039;&amp;#039;Statement&amp;#039;&amp;#039;&amp;#039; is executed at the end of every loop.&lt;br /&gt;
&lt;br /&gt;
So for example, the following while loop:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
int i = 0;&lt;br /&gt;
&lt;br /&gt;
while (i &amp;lt; 10) {&lt;br /&gt;
    // code&lt;br /&gt;
    &lt;br /&gt;
    i += 1;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Could be written as the following for loop:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 0; i &amp;lt; 10; ++i) {&lt;br /&gt;
    // code&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===For-Each loop===&lt;br /&gt;
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:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
std::vector&amp;lt;std::string&amp;gt; range = { &amp;quot;apple&amp;quot;, &amp;quot;banana&amp;quot;, &amp;quot;orange&amp;quot; };&lt;br /&gt;
&lt;br /&gt;
for (auto item: range) {&lt;br /&gt;
    // code&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
The following is a [[C (programming language)|C-style]] [[While loop]]. It continues looping while &amp;#039;&amp;#039;&amp;#039;x&amp;#039;&amp;#039;&amp;#039; does not equal &amp;#039;&amp;#039;&amp;#039;3&amp;#039;&amp;#039;&amp;#039;, or in other words it only stops looping when &amp;#039;&amp;#039;&amp;#039;x&amp;#039;&amp;#039;&amp;#039; equals &amp;#039;&amp;#039;&amp;#039;3&amp;#039;&amp;#039;&amp;#039;. However, since &amp;#039;&amp;#039;&amp;#039;x&amp;#039;&amp;#039;&amp;#039; is initialized to &amp;#039;&amp;#039;&amp;#039;0&amp;#039;&amp;#039;&amp;#039; and the value of &amp;#039;&amp;#039;&amp;#039;x&amp;#039;&amp;#039;&amp;#039; is never changed in the loop, the loop will never end ([[infinite loop]]).&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
int x = 0;&lt;br /&gt;
&lt;br /&gt;
while (x != 3) {&lt;br /&gt;
    // code that doesn&amp;#039;t change x&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
The while loop below will execute the code in the loop 5 times. &amp;#039;&amp;#039;&amp;#039;x&amp;#039;&amp;#039;&amp;#039; is initialized to 0, and each time in the loop the value of &amp;#039;&amp;#039;&amp;#039;x&amp;#039;&amp;#039;&amp;#039; is incremented. The while loop is set up to stop when &amp;#039;&amp;#039;&amp;#039;x&amp;#039;&amp;#039;&amp;#039; is equal to 5.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
int x = 0;&lt;br /&gt;
&lt;br /&gt;
while (x != 5) {&lt;br /&gt;
    // code&lt;br /&gt;
    &lt;br /&gt;
    x += 1;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Frequent bugs==&lt;br /&gt;
Conditional loops are often the source of an [[Off by one error]].&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
{{Reflist}}&lt;br /&gt;
&lt;br /&gt;
{{DEFAULTSORT:Conditional Loop}}&lt;br /&gt;
[[Category:Control flow]]&lt;/div&gt;</summary>
		<author><name>imported&gt;Ggga21</name></author>
	</entry>
</feed>