1

I read that while loop is used when number of iterations are unknown and for loop is used when number of iterations are known ... such as when I have to print a statement 5 times I have to initialize the variable in both while and for loop and also I need to give iteration condition in both (i++) and i also have to mention condition of iteration so that it loops only 5 times ... then whats the difference ? can someone pls tell with an example

for(int i=0;i<5;i++){
    cout<<"Hello";
    }
int i=0;
while(i<5){
    cout<<"hello";
    i++;
    }
  • Your example is quite good. With the for loop you join three lines in one, and at the same time you difference three sections: a pre loop block initialization, a pre loop check and a post loop update. Use whichever is clearer or simpler for you. – rturrado Jul 18 '20 at 23:40
  • "I read that while loop is used when number of iterations are unknown and for loop is used when number of iterations are known" - That's completely false. You use for loops when what you're doing matches the form of for loops. Otherwise, you use while loops. – eesiraed Jul 19 '20 at 03:42

3 Answers3

4

There is no real difference between while and for.

{
  int i = 0;
  while(i < 5){
    //stuff
    i++;
  }
}

is the same as

for(int i = 0; i < 5; i++){
   //stuff
}

But as you can see, the for is easier to use when you have a known condition.

For example, suppose you are running an algorithm that finishes when a list is empty and there is no counter variable, such as i. It's easier to understand it using a while:

while(!list.empty()){ /* stuff */ }

But you can use it the same way with a for:

for( ; !list.empty(); ){ /* stuff */ }

You choose one of them by simplicity and readability, according to each situation or to your personal preference.

Daniel
  • 7,357
  • 7
  • 32
  • 84
  • I am not getting what actually is "known" condition coz if a condition isn't known ... wont both the loops iterate infinitely? Edit:So there's no major difference ... it just about user readibility? –  Jul 18 '20 at 23:30
  • @Electrocuted known before the loop even starts – Caleth Jul 18 '20 at 23:48
  • An unknown condition doesn't mean it will loop eternally, just mean you don't know the real value at that moment, but you know that it will end some time. – Daniel Jul 19 '20 at 01:03
1

Both a while loop and for loop is a pretested loop, meaning it tests its expression before each iteration. An important characteristic of a for loop and a while loop is that it will never iterate if the test expression is false to begin with.

Now you should use the for loop instead of the while loop in any situation that needs an initialization, except when initializations of different types are needed.

#include <iostream>

int main()
{
    for (int i = 0; i < 5; i++) {
        std::cout << "I am a for loop." << std::endl;;
    }
    int i = 0;
    while (i < 5) {
        std::cout << "I am a while loop" << std::endl;
        i++;
    }
}

I also suggest you to not use using namespace std;. You can read more about it Here.

Geno C
  • 1,401
  • 3
  • 11
  • 26
0

While coding for a 'for' loop one should know the iterations first because it is being put before starting the loop and after putting the iterations the statements are being put; at the same time if we are coding for a 'while' loop we can put n number of iteration anywhere inside the loop (whenever required) . That's why it is said that 'for' loop is used when number of iterations known and 'while' loop is used when number of iterations are unknown. Although after coding if anyone looks the code it seems like in both cases number of iterations are known ; But that statement is given for a coder while performing a code.