Java Is A Language of 50 Keywords ‘for’

As we have talked about in the previous article that we will be talking about 50 important keywords of Java. We have about the abstract keyword last time. This time we will discuss ‘for’ keyword.

For

The ‘for‘ a keyword is a loop keyword among several other loopings keywords. The other looping keywords are while loop, do-while loop. In programming whenever we want to repeat the code multiple times we need to use for loop. The people who are new to the programming they repeat the code whenever they need to print something multiple times but repeating code is a huge mistake in programming because it gives more work to compilers that leads to more time in the execution of the program.

A for loop is actually a predefined function by the developers of Java. This function takes 3 parameters and based on the parameters it repeats the code.

Syntax of for

package forKeyword;

public class ForKeyword {

        public static void main(String[] args) {
                
                
                for (int i = 0; i < args.length; i++) {
                        
                }

        }
        
}

Syntax Explanation

First Parameter – The loop usually starts with 0 this is why 0 is the first parameter which is defined by default. You can change the initial point of the loop by changing the value of ‘i’ from 0 to your desired point.

Second Parameter – The second parameter is to tell the loop when it should stop. You can hard code the value by giving it a value of your choice in case you are sure about how many times this code should be repeated. But there will be times when you would have no idea how many times the code should be repeated. In the case of printing the values from an Array. When you receive information in the form of API. You will receive the information from API in the form of Array. Do not worry if you don’t know what is API. We will learn about it in the future but this point you can think, it is an array.

Third Parameter – The third parameter is to tell the loop how the loop should count. If you provide this parameter as ‘i++’, it means you want this to count in an incremental way like 1,2,3,4,5,6,7,8,9,10 If you provide ‘i–‘ it means you want the loop to count in a decremental way like 10,9,8,7,6,5,4,3,2,1. If you choose to loop as a decremental way then you will have to change the starting point in the first parameter from 0 to 10.

Example Program

package forKeyword;

public class ForKeyword {

        public static void main(String[] args) {
                
                int iterator = 10;
                
                for (int i = 0; i < iterator; i++) {
                        
                        System.out.println("Hello World");
                }
                
        }
        
}

Wrong-Way of Printing Repetitive Statements

package forKeyword;

public class ForKeyword {

        public static void main(String[] args) {
                
                
                System.out.println("Hello World");
                System.out.println("Hello World");
                System.out.println("Hello World");
                System.out.println("Hello World");
                System.out.println("Hello World");
                System.out.println("Hello World");
                System.out.println("Hello World");
                System.out.println("Hello World");
                System.out.println("Hello World");
                System.out.println("Hello World");

        }
        
}

Continue

The keyword ‘continue‘ is used with all types of loops. This keyword gives the command to the loop that once ‘continue‘ keyword has occurred in the code, the loop should skip that particular iteration and move to the next iteration. For example, you are writing a loop program to print the value from 1 to 10 and you want the program to skip when the value of ‘i’ is 7. It means you do not want the loop to print number 7.

Example Program

public class ForKeyword {

        public static void main(String[] args) {
                
                int iterator = 10;
                
                for (int i = 0; i < iterator; i++) {
                        
                        if (i == 7) {
                                
                                continue;
                                
                        }
                        
                        System.out.println(i);
                }
                
        }
        
}

Result of this program

Result
Result

As you can see in the program, we have added an if condition and said if the value of i is equal to 7 then the loop will hit continue and the whole iteration of this loop will be skipped. The number 7 will not be printed.

Final Words – We have understood for and continue in this article. We will continue this journey in the next article and learn about the remaining keywords in the future.

The post Java Is A Language of 50 Keywords ‘for’ appeared first on LinuxAndUbuntu.



from LinuxAndUbuntu https://ift.tt/2SLSdJ3

Post a Comment

0 Comments