Computer Science, asked by Ritammukherjee, 1 year ago

Can anyone please give me an example of input inside the loop in java program?

Answers

Answered by GeN21
1
import java.io.*;
class Loop
{
public static void main( )throws IOException
{
BufferedReader br=new BufferedReader (new InputStreamReader (System.in));
System.out.println("enter value");
for(int I=0;I<5;I++)
{
int n=Integer.parseInt(br.readLine( ));
}
}
}

Ritammukherjee: Ok sir no problem
Ritammukherjee: i dont have any question
Ritammukherjee: but
GeN21: wait
Ritammukherjee: can you do this in input inside the loop
GeN21: i will edit my answer
Ritammukherjee: Input a number and print square of even number and cube of odd number up to that number
GeN21: We cant input the number in the loop because the number will get a new value every time and the square will be different from before
GeN21: wait
GeN21: i will send the ans after 15 min
Answered by Anonymous
19

Loop used in JAVA


A loop is a repetition of a function .

For example :


class random

{

void main()

{

for ( int i = 0 ; i <=5 ; i++ )

{

System.out.println ( "Help ") ;

}

}

}


OUTPUT :

Help

Help

Help

Help

Help


You can clearly understand that loop is used to repeat a task several times .

Here I have printed "Help" several times thats it !


Now I will give an example of while-loop :


class jishnu

{

void main()

{

int n=2;

while(n!=0)

{

System.out.println(n);

n--;

}

}

}


OUTPUT :

2

1


The basic thing in a while loop is that it checks the condition and if the condition is satisfied then only the loop will be continued .

Similar questions