+ 4
What is difference between "for loop" and "while loop" in C++?
how would you write a code which alters a number from binary system to decimal notation system using a while loop? for ex: input: write a number in binary sytem : 100 Output: 100 in binary system equals to 4 in decimal notation system
3 Respostas
+ 8
FOR loop is just a simplified version of the WHILE loop.
WHILE LOOP:
int i = 0;
while(i < 4){
// some code
++i;
}
FOR LOOP:
for(int i = 0; i < 4; ++i) {
// some code
}
^As you can see, the FOR loop is a simplified version of the WHILE loop. It combines the counter variable, loop condition, and increment all in the same spot instead of all over the place.
When there is a means of "knowing" the amount of iterations, then use a FOR loop. If there is no telling when it will end, use a while loop.
+ 5
for(initialization; condition ; updation)
{
body of the loop;
}
*************************""
initialization;
while(condition)
{
body of the loop;
updation;
}