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...

Constructors in Java

Constructors : Initializing an Class Object in Java Programming A  constructor in Java  is a block of code similar to a method that's called when an instance of an object is created. Here are the key differences between a  constructor  and a method:  A  constructor  doesn't have a return type .   The name of the  constructor  must be the same as the name of the class. Some Rules of Using Constructor : Constructor  Initializes an Object . Constructor  cannot be called  like methods. Constructors   are called automatically  as soon as object gets created. Constructor  don’t have any return Type.  (even Void) Constructor name is same as that of “ Class Name “. Constructor  can accept parameter . Default Constructor : How Constructor Works ? class Box { //class name int height ; // variables int width ; Box ( ) ...