English, asked by mrukmini41, 10 months ago

explain for loop with syntax and example 5 points​

Answers

Answered by Khansarah123
1

Loops are used to execute a set of statements repeatedly until a particular condition is satisfied. In Java we have three types of basic loops: for, while and do-while. In this tutorial we will learn how to use “for loop” in Java.

Syntax of for loop:

for(initialization; condition ; increment/decrement)

{

  statement(s);

}

In the following pic, there is a flowchart use that foe the below explanation.

First step: In for loop, initialization happens first and only one time, which means that the initialization part of for loop only executes once.

Second step: Condition in for loop is evaluated on each iteration, if the condition is true then the statements inside for loop body gets executed. Once the condition returns false, the statements in for loop does not execute and the control gets transferred to the next statement in the program after for loop.

Third step: After every execution of for loop’s body, the increment/decrement part of for loop executes that updates the loop counter.

Fourth step: After third step, the control jumps to second step and condition is re-evaluated.

Example of Simple For loop

class ForLoopExample {

   public static void main(String args[]){

        for(int i=10; i>1; i--){

             System.out.println("The value of i is: "+i);

        }

   }

}

The output of this program is:

The value of i is: 10

The value of i is: 9

The value of i is: 8

The value of i is: 7

The value of i is: 6

The value of i is: 5

The value of i is: 4

The value of i is: 3

The value of i is: 2

In the above program:

int i=1 is initialization expression

i>1 is condition(Boolean expression)

i– Decrement operation

Attachments:
Answered by pra9335170007
0

This is the answer of this question pls mark me as a brainlist.

Attachments:
Similar questions