Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,152,962 members, 7,817,821 topics. Date: Saturday, 04 May 2024 at 08:28 PM

Help Convert This WHILE Loop Into A FOR Loop - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Help Convert This WHILE Loop Into A FOR Loop (1931 Views)

Javascript - For Loop / Help With Python Program (simple While Loop) / Programming Challenge: Convert String to Json with a Loop (2) (3) (4)

(1) (Reply) (Go Down)

Help Convert This WHILE Loop Into A FOR Loop by Talk2bryant(m): 8:51pm On Jul 11, 2015
Int i= 1;
Int sum = 0;

while (sum < 10) {
sum = sum + i;
i++;
}
Re: Help Convert This WHILE Loop Into A FOR Loop by omoelu1(m): 10:43pm On Jul 11, 2015

public class ReWrite{
public static void main(String[] args){
int i = 1;
int sum = 0;

for(sum = 0; sum < 10; i++){
sum = sum + i;
System.out.println(sum);
}

}
}
Goodluck!!!
Re: Help Convert This WHILE Loop Into A FOR Loop by hisabimbola(m): 10:51pm On Jul 11, 2015
This question sounds funny, guess that's why you got no response, but either ways


for (; sum < 10; sum=sum+i) {
i++;
}


Wrote it in JS, but you should be able to convert it
Re: Help Convert This WHILE Loop Into A FOR Loop by Nobody: 11:26pm On Jul 11, 2015
My own simple answer(after girraffing some of the codes above) is:

In Java:
Int sum=0;

for(Int i = 0; i < 10; i++){
sum = sum + i;
System.out.println(sum);
}

In JavaScript:
sum=0;

for(i = 0; i < 10; i++){
sum = sum + i;
document.write(sum);
}

In PHP:
$sum=0;

for($i = 0; $i < 10; $i++){
$sum = $sum + $i;
echo($sum);
}




I have already used my eyes to debug it - it will work. . . .

1 Like

Re: Help Convert This WHILE Loop Into A FOR Loop by fattbabakay(m): 9:25am On Jul 12, 2015
dhtml18:
My own simple answer(after girraffing some of the codes above) is:

In Java:


In JavaScript:


In PHP:





I have already used my eyes to debug it - it will work. . . .
Guruuuuuuuu!!!
Re: Help Convert This WHILE Loop Into A FOR Loop by Damey2011(m): 9:14am On Jul 13, 2015
dhtml18:
My own simple answer(after girraffing some of the codes above) is:

In Java:


In JavaScript:


In PHP:





I have already used my eyes to debug it - it will work. . . .

The Increment is applicable to the integer i.. so the for line is

for(sum = 0; sum < 10 ; sum ++){
//do the print line
}
I'm lazy i cant type.
What I see about the code is just printing sum from 0-9.. but the essence of the int = 1 is to make is start from 1- 10 instead cos the computer is programmed to start counting from 0 for a reason I dont know
Re: Help Convert This WHILE Loop Into A FOR Loop by eDoc: 10:41am On Jul 13, 2015
if i understand your code well,this is in python.
i had to study other code above,
As i is incrementing from 1 to whatever,sum is adding it's self to the present state of i.

(1) (Reply)

Studying Computer Science At 40? / Web Developer And Designer Needed For Tech Startup / The Future Of Programming Language

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 10
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.