₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,325,368 members, 8,421,585 topics. Date: Saturday, 06 June 2026 at 04:55 PM

Toggle theme

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

Nairaland ForumScience/TechnologyProgrammingHelp Convert This WHILE Loop Into A FOR Loop (2180 Views)

1 Reply (Go Down)

Help Convert This WHILE Loop Into A FOR Loop by Talk2bryant(op): 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. . . .
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

Javascript - For LoopHelp With Python Program (simple While Loop)Programming Challenge: Convert String to Json with a Loop234

I'll Help With Any Ms Excel Issues HereColdfusion Vs Php Code BattleYoung Programmers in Abuja