difference between while loop and for loop code example
Example 1: ?whats is the difference between while loop and for loop
//First Code WHILE loop
int x = 0;
while (x < 10)
{
//Do Something while x is less than 10
// if you don't want infinity loop increse x
x++;
}
//Second Code But Now Its FOR loop with the same int x with different different result
int x = 10
for (int i = 0; i < x; i++)
{
//Do Something while i is less than x
}
//in the while loop we executeing the code until all the conditions are true.
//in the for loop we are looping through the int that we created,
//in our loop the int is 'i', and every time we done execute the code in
//the loop we check if the condition that we gave in the loop in our is
//if 'i' < x and if it's false we are increasing 'i' and execute the code
//again until the condition is true.
Example 2: difference between while loop vs do while loop java
Main difference while loop executes while
condition is true. But do while executes
at least one time regardless.