Looping Statements
Looping Statements or Iterative Statements
- These are used to run the set of statements repeatedly.
- Here the statements run repeatedly until the given condition is false.
- We need to write a proper condition so that the loop runs finite number of times.
Types of loops:
- while
- do-while
- for
- foreach
1)while loop:
- Here the loop executes until the given condition is false.
- The Initialization / declaration can be done at anywhere before the loop.
Syntax:
while(condition){
//statements which are needed to be execute
Increment/Decrement;
}
2)do-while loop:
- Here the statements are executes atleast one time , even the given condition is false.
- It is exit control loop whereas for,while are entry control loops.
Syntax:
do{
//statements which are needed to be execute
}while(conditon);
3)for loop:
- Here the number of Iterations can be known.
- Here the initialization , Declaration , Condition , Increment / Decrement are present a single place.
- We can place multiple Declarations , Increments / Decrements by palcing , (camma).
Syntax:
for(initialization/Declaration ; condition ; Increment/Decrement){
//statements which are needed to be execute repeated times
}
4)foreach loop:
- It is used on objects.
- Here the values contain all values inside the object for each iteration.
- This can be used in arrays,lists...
Syntax:
for(variableName : objectName){
//statements which are need to be execute
}
Comments
Post a Comment