I there any way to execute a statement for one time only , when all values in for loop or satisfying the condition. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I there any way to execute a statement for one time only , when all values in for loop or satisfying the condition.

for (int x=0 ; x《length.array ; x++ ) If (array [x] 》 array [x+1] ) Statement. out.println (array [x] +" is largest");

13th Nov 2017, 12:25 PM
Aamir Sultan
Aamir Sultan - avatar
1 Answer
+ 1
To create a loop that runs once only you need to set a condition upon which the loop should terminate, the following snippet shows a one iteration loop. I hope I got your question correctly, you didn't specify a language in the question tag, but looking at your code I just guessed it was Java. public class Program { public static void main(String[] args) { // This loop runs once only for(int i = 1; i <= 10; i++) { System.out.println("Iteration " + i); if(i < ++i) { System.out.println("Condition satisfied"); break; } } } } Hth, cmiiw
13th Nov 2017, 12:54 PM
Ipang