A blog for computer science passionates.

Saturday, 2 May 2020

Java: flavors of for loop

Hello friends!!!
Welcome to passionforcs,

In this article, we are going to discuss different flavors of for loop, in the previous article we have already seen that what is looping and how many looping statements are there? and basic syntax of looping statements but we can use for loop differently and we can play with the same syntax of for loop let's understand How?

As we know the main syntax of for loop is something like,
for(init;condition;increment/decrement)
{
   //code block
}

If we want to make for loop infinite we can use the syntax below
for(;;)
{
    //code block
}

If we have a counter variable and is already initialized we can use the syntax below
for(;condition;increment/decrement)
{
    //code block
}

If we want to use for loop like while loop we can use the below syntax
for(;condition;)
{
    //code block
    //increment/decrement
}
when we use the above syntax the counter variable should have to be initialized and we have to add increment or decrement inside the loop

So, we can use for loop with different syntax. try to perform different program with these different syntaxes, Happy Coding... :)

No comments:

Post a Comment