Skip to main content

For Loop

Flow of For Loop in Java Programming

for-Loop
  1. For Loop is one of the looping statement in java programming.
  2. For Loop is used to execute set of statements repeatedly until the condition is true.
  3. For Loop checks the contrition and executes the set of the statements , It is loop control statement in java.
  4. For Loop contain the following statements such as “Initialization” , “Condition” and “Increment/Decrement” statement.

Syntax For Loop : Java Programming

for (initialization ; condition ; increment) {
    Statement1
    Statement2
    .
    .
    StatementN
}

Live Example : For Loop Statement

class ForExample {
    public static void main(String[] args){
         for(int i=1; i<10; i++){
              System.out.println("Count is : " + i);
         }
    }
}

Output :

Count is: 1
Count is: 2
Count is: 3
Count is: 4
Count is: 5
Count is: 6
Count is: 7
Count is: 8
Count is: 9

Explanation :

1. Initialization

  1. Consider above program , in this program we are able to see that for loop statement contain 3 statements first one is “Initialization”.
  2. ‘i’ is loop control variable used to control the complete loop.
  3. Its value is initialized with 1.
  4. Initialization statement gets executed once.

2. Condition

  1. Inside first step value of   i  is initialized .
  2. After initialization the value of control variable is checked against the condition present inside “Condition”.
  3. i < 11 is true condition so that body part of the loop gets executed and it print the Count.
  4. As soon as it finishes the execution of the body part it will jump to the “Increment/Decrement” statement.

3. Increment / Decrement

  1. It will alter the value of Control variable by incrementing it or by decrementing it.
  2. After alteration it again executes “Condition” of the loop.
  3. Again if the condition evaluates to be true then it will again executes the body part as long as condition remains true.

Keep in mind :

  1. Initialization expression initializes the loop
  2. Initialization expression executed once when the loop begins.
  3. When the Condition expression evaluates to false, the loop terminates.
  4. The Increment expression is invoked after each iteration through the loop; it is perfectly acceptable for this expression to increment or decrement a value.
Consider Following Program :
class Sample {
    public static void main(String[] args){
         for(int i=1; i ; i++){  // need to use valid condition
              System.out.println("Count is : " + i);
         }
    }
}

In Java Integer is not an True Expression :

  1. It will throw error since In Java we cannot consider Positive integer as “True”.
  2. We must use comparison operator in order to make expression True.
  3. We can Use boolean Operators instead.
C:Priteshjava>javac Sample.java
Sample.java:3: incompatible types
found   : int
required: boolean
         for(int i=1; i ; i++){        // need to use valid condition
                      ^
1 error

Legal and Illegal Ways of For Loop :

Example 1 : Compile Error

public static void main(String[] args){
         int exp;
         for(int i=1; exp ; i++){        // need to use valid condition
              System.out.println("Count is : " + i);
         }
    }

Example 2 : Error Free

public static void main(String[] args){
         boolean exp;
         for(int i=1; exp ; i++){
              System.out.println("Count is : " + i);
         }
    }

Comments

Popular posts from this blog

Switch Case

Syntax : Switch Case in Java Programming It is alternative to else-if ladder. Switch Case Syntax is similar to – C/C++  Switch. Switch allows you to choose a block of statements to run from a selection of code, based on the return value of an expression. The expression used in the switch statement must return an  int, a String, or an enumerated value . switch (selection) { // value case value1 : // checking value 1 statement ( s ) ; break ; // use to break switch flow if condition match case value2 : // checking value 2 statement ( s ) ; break ; . . case value_n : statement ( s ) ; break ; default : statement ( s ) ; } Different Ways of Using Switch Case : Switch Case Using Integer Case int i=3; switch (i) { case 1 : System . out . println ( "One player is playing this game." ) ; break ; case 2 : System . out . println ( "Two players are playing ...

Inheritance in Java

Inheritance in Java Inheritance  is one of the feature of Object-Oriented Programming (OOPs). Inheritance allows a class to use the properties and methods of another class. In other words, the derived class inherits the states and behaviors from the base class. The derived class is also called subclass and the base class is also known as super-class . The derived class can add its own additional variables and methods. These additional variable and methods differentiates the derived class from the base class. Inheritance is a  compile-time  mechanism. A super-class can have any number of subclasses . But a subclass can have only one superclass. This is because Java does not support multiple inheritance. The superclass and subclass have  “is-a”  relationship between them. Let’s have a look at the example below. Inheritance  Example Let’s consider a superclass  Vehicle . Different vehicles have different features and properties howeve...

Multilevel inheritance

Multilevel inheritance We discussed a bit about  Multilevel inheritance  in types of inheritance in java. In this tutorial we will explain multilevel inheritance with the help of   diagram  and  example program . It’s pretty clear with the diagram that in Multilevel inheritance there is a concept of grand parent class. If we take the example of above diagram then class C inherits class B and class B inherits class A which means B is a parent class of C and A is a parent class of B. So in this case class C is implicitly inheriting the properties and method of class A along with B that’s what is called multilevel inheritance. Example : In this example we have three classes –  Car, Maruti and Maruti800. We have done a setup – class Maruti extends Car and class Maurit800 extends Maurti. With the help of this Multilevel hierarchy setup our Maurti800 class is able to use the methods of both the classes (Car and Maruti). class Car { ...