Computer Science, asked by asmitatheposhed, 1 year ago

Give an example of converting do-while to for.
Give an example of converting while into for

Answers

Answered by shreyamahajan
1
Here this is your answer hope it helps....... ☺☺
Do while loop
int i= 1;
int d=5;
do{
d=d*2;
System.out.println(d);
i++;} while (i<=5);

From do while to For loop
int i=1;
int d=5;
for(i=1;i<=5;i++)
{
d=d*2;
System.out.println (d) ;
}

While loop
int i=1;int d=5;
while(i<=5)
{
d=d*2;
System.out.println(d);
i++;
}

While loop to for loop
int i=1;
int d=5;
for(i=1;i<=5;i++)
{
d=d*2;
System.out.println(d);
}
Answered by Anonymous
14
hey!!

here is ur answer!!


_______________________

1) for converting do while to for....


ques.... To print numbers from 1 to 10....


its in do while loop..



class P20

{

public static void main (String [] args)

{

int i =100 (semicolon)

do

{

System. out. println(i)(semicolon)
i++ (semicolon )

}
while( i<= 10)(semicolon)
}

}



Now for loop....





class P19

{
public static void main(String args[])

{
for ( int i = 1 (semicolon) i<=10(semicolon)i++)

{
System. out. println (i) semicolon
}
}

}

_________________________________

for converting a while to for.... i m writing a syntax u will understand easily with the above program...

the for loop...

for (initialization expression (semicolon) test expression (semicolon) increment /decrement)
{

statement semicolon

}

the while loop

initialization expression semicolon
while (test expression)

{

statement semicolon

increment /decrement expression

}

As the for loop and while loop essentially equivalent...

the for loop is used for counting loops ......

the while loop is generally used when you do not know in advance how many times the loop will execute.........

|| instead of putting semicolon i have written semicolon as in phone putting semicolon is not possible....

____________________________

HOPE THIS HELPS U!!!

ria113: nice gunguniya
Anonymous: thank u roya.....
Kshitijxingh: osm gungunii...☺
ria113: welcm
Anonymous: thank u Kshitij... i will give a nick name soon
Anonymous: nice explanation.....
Anonymous: thnk u roshan
Similar questions