Looping Statements
while, do-while statements – The while statement executes a block while the condition is true.
Ex.
import java.lang.*;
public class MovingCar{
static boolean ismoving = true;
static boolean breaks = false;
public static void main(String[] args){
while(ismoving){
if(breaks){
ismoving = false;
System.out.println("breaks is applied...stopped.");
}
else{
System.out.println("The car is moving.");
}
breaks = true;
}
}
}
This code prints “The car is moving.” once and breaks is applied and goes out of the while loop.
Do-while statements' difference is hat the condition is placed at the bottom and executes the block of code once.
Ex.
do{
//statements
}while (condition);
For Loop
Ex. for (x=0; x<10;>
//increment or decrement (x--)using unary operator
//initialization and executes once.
//Condition when met, or when it evaluates to false, it terminates.
System.out.println(x);
}
to be continued...
No comments:
Post a Comment