CPP - Loops - For loop

in for loop video,it is saying that we must use for loop when we know for how many time the loop will run and use while loop when we don’t know for how many times the loop will run like to check a number is prime or not we will use while loop but we can do this program using for loop also. so what is the difference between while and for loop.

Hello @Sheenagoyal21,

You have taken a wrong example to understand this.
In your example, you can use any of the loop.

Now, suppose you are required to write a program that takes input until -1 is not received as input.
int n;
while(true){
cin>>n;
if(n==-1){
break;
}
}

Thus, it is preferred to use while loop in such condition.

Hope, this would help.
Give a like, if you are satisfied.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.