+ 5
Someone please explain me while loops
I am not getting while loops
7 Answers
+ 11
The while loop executes a block of code as long as the specified condition is true.
$i = 1;
while ($i < 5) {
echo " $i <br />";
$i++;
}
Output will be:
1
2
3
4
+ 5
While mood < 10:
eat('cookie')
#so until my mood is higher than 9, I'll eat cookies. If a cookie doesn't raise my mood, welp, looks like I'm gonna get fat eating cookies forever.
+ 1
in While loop always the condition is checked if it is true then statement inside while loop is excuted this process repeated until the while condition become false
0
The statement repeats untill the condition become false
Python
i= 5
While i > 0:
Print("i")
i = i - 1
0
There is always a condition in the while loop.
1. Check the condition
2. If its true the code inside the loop is executed.
3. Then again the condition is checked.
4. If its true, then again the code inside the loop is executed.
5. This above steps are continued till the condition is false.
//Example for while loop in c++ to get the addition of all the integers from 1 to 5
int k = 1, total = 0;
While (k <= 5)
{
total = total + k; //add k and total
k++; // increments the value of k by 1
}
cout<< " The total = " << total;
//Output will be " The total = 15 "
In this above code the value of k is increased by one till the condition of while loop become false. ( till the value of k is less than or equal to 5)