Difference between revisions of "While loop"

From CodeCodex

(add Ruby)
Line 2: Line 2:
  
 
==Implementations==
 
==Implementations==
===Java===
+
===C / C++===
 
<pre>
 
<pre>
 
int x = 0;
 
int x = 0;
Line 12: Line 12:
 
</pre>
 
</pre>
  
===C / C++===
+
===Java===
 
<pre>
 
<pre>
 
int x = 0;
 
int x = 0;
Line 48: Line 48:
 
break;
 
break;
 
}
 
}
 +
</pre>
 +
 +
===Ruby===
 +
<pre>
 +
x = 10
 +
while x > 0
 +
  # do something
 +
  x -= 1
 +
end
 
</pre>
 
</pre>
  
Line 66: Line 75:
 
[[Category:Iterations]]
 
[[Category:Iterations]]
  
[[Category:Visual Basic]]
 
[[Category:Pascal]]
 
 
[[Category:C]]
 
[[Category:C]]
 
[[Category:C++]]
 
[[Category:C++]]
 +
[[Category:Pascal]]
 
[[Category:PHP]]
 
[[Category:PHP]]
 +
[[Category:Ruby]]
 +
[[Category:Visual Basic]]

Revision as of 14:23, 3 July 2010

The while loop is a common looping mechanism. It is comparable to the for loop.

Implementations

C / C++

int x = 0;
int y = 10;

  while(x < y){
    x++;
  }

Java

int x = 0;
int y = 10;

  while(x < y){
    x++;
  }

Pascal

while (x < y) do
begin
   {Do something.}
end;

PHP

$i=0;

while ($i<10) {
	// do something
	$i++;
}

Or:

while (true) {
	// do something
	if ($a==$b)
		break;
}

Ruby

x = 10
while x > 0
  # do something
  x -= 1
end

Visual Basic

Do While x < y
    ' Do something
Loop

Or:

While x < y
    ' Do something
End While