How do I write a While loop [closed]
-
09-06-2019 - |
Question
How do you write the syntax for a While loop?
C#
int i = 0;
while (i != 10)
{
Console.WriteLine(i);
i++;
}
VB.Net
Dim i As Integer = 0
While i <> 10
Console.WriteLine(i)
i += 1
End While
PHP
<?php
while(CONDITION)
{
//Do something here.
}
?>
<?php
//MySQL query stuff here
$result = mysql_query($sql, $link) or die("Opps");
while($row = mysql_fetch_assoc($result))
{
$_SESSION['fName'] = $row['fName'];
$_SESSION['lName'] = $row['lName'];
//...
}
?>
Python
i = 0
while i != 10:
print i
i += 1
Solution
In PHP a while loop will look like this:
<?php
while(CONDITION)
{
//Do something here.
}
?>
A real world example of this might look something like this
<?php
//MySQL query stuff here
$result = mysql_query($sql, $link) or die("Opps");
while($row = mysql_fetch_assoc($result))
{
$_SESSION['fName'] = $row['fName'];
$_SESSION['lName'] = $row['lName'];
//...
}
?>
OTHER TIPS
There may be a place for this type of question, but only if the answer is correct. While
isn't a keyword in C#. while
is. Also, the space between the !
and =
isn't valid. Try:
int i=0;
while (i != 10)
{
Console.WriteLine(i);
i++;
}
While I'm here, Python:
i = 0
while i != 10:
print i
i += 1
TCL
set i 0
while {$i != 10} {
puts $i
incr i
}
C++, C, JavaScript, Java and a myriad of other C-like languages all look exactly the same as C#, except in the way that they write the output to the console, or possibly the way you create the variable i
. Answering that would belong in some other question.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow