comparison between while and for loop code example

Example: ?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.

Tags:

Java Example