Hello friends how are you, today in this blog i will teach you what is for loop, syntax of for loop , how for loop works and many programs using for loop in a very simple way.
Let's start
- In for loop there are three part initialization, condition and increment/decrement.
- Initialization part executes only once.
- Condition part defines the condition for the execution of code block.
- All the three part of for loop are optional means we can skip any or all of its parameters.
class Easy { public static void main(String[] args) { for (int i = 1; i <5; i++) { System.out.println(i); } } } /* ### Output ### 1 2 3 4 */
In the above example initialization part initialize the variable i with 1, condition is i less than 5 and at the increment place there is an increment by 1 ,so the output will be 1 to 4.
Here in the below example we are going to write initialization part before the for loop, condition part in its place and increment part inside the body of loop and after running this program we will get same output like above program .
/* Example 1 can also be written like the code below */ class Easy { public static void main(String[] args) { int i = 1; for (; i <5; ) { System.out.println(i); i++; } } } /* ### Output ### 1 2 3 4 */
Here in this program we have skipped all the three parts of loop and we are printing Hello inside the body of loop so this loop will print infinite times Hello because there is no condition or break to terminate this loop.
Here in the below program many learner will think that there is an error in this program if they don't know the concept of for loop completely but here there is no error in this program.
class Easy { public static void main(String[] args) { //all parts of for loop are optional for(;;) System.out.println("Hello"); } } /* ### Output ### Hello Hello Hello Hello ----- ----- Infinite time Hello */
This program is the most asked question of loop. To make this program we will take three variable no to store number, f for storing multiplication result and last one i for running loop. After taking user input we will execute loop from 1 to no and multiply all the number between 1 to no and store the result in f and after coming outside of loop we will print the result.
//factorial of 5=1x2x3x4x5 //factorial of 6=1x2x3x4x5x6 //factorial of N=1x2x3x....xN import java.util.Scanner; class Easy { public static void main(String[] args) { Scanner in=new Scanner(System.in); int no,f=1; System.out.println("Enter any number"); no=in.nextInt(); for (int i = 1; i <=no; i++) { f=f*i; } System.out.println("The factorial of "+no+" is "+f); } } /* ### Output ### Enter any number 6 The factorial of 6 is 720 */
Request:-If you found this post helpful then let me know by your comment and share it with your friend.
If you want to ask a question or want to suggest then type your question or suggestion in comment box so that we could do something new for you all.
If you have not subscribed my website then please subscribe my website.
Try to learn something new and teach something new to other.
Thanks.😊
0 Comments