Loops | Sololearn: Learn to code for FREE!



Pls anyone can explain difference in for loop and while loop And how can that use in coding

11/12/2020 8:57:57 AM

Nandini Reddy

9 Answers

New Answer


Nandini Reddy , here is a brief description (python): In General the difference is: > for loop is mostly used when the number of iterations are known before starting iteration. This could be like: - for num in range(1,10): - for val in [1,3,7,2]: There is loop variable, that get one value in each iteration, starting with the first (index= 0) element of the iterable. for loops can have a break or a continue statement. > while loop is used mostly when the number of iterations is not clear when starting the loop. This could be: - while True: - while count <= 50: - while x <= len(inp_values): A loop or counter (or other variables) can be used, variables has to be incremented or decremented by code. while loops can have a break or a continue statement.


https://www.sololearn.com/Discuss/400480/?ref=app https://www.sololearn.com/Discuss/1522646/?ref=app


Nandini Reddy U can use the search bar feature in Sololearn to avoid duplicates and also ,u can find much detailed answers. I have posted some similar threads above .Go through it. Happy Coding......


In for loop, we know how many iterations should be done, but we don't know number of iterations in while loop. Like in modern programming languages, in for loop, we have range of numbers, so the number of iterations r fixed, and in while loop, we do the iteration unless the specific condition remains true. //for loop in kotlin: for (i in 1..5) // here, "i" will be 1 to 5 println(i) //while loop in kotlin: var i = 1 while ( i < 5 ) { println(i) i++ } here, the number of iterations r dependent upon the condition we r checking i.e., i < 5, and increment(i++), which can also increased as per ur wish, so we don't know the number of iterations here. I hope this helps u. happy coding☺👍


https://www.sololearn.com/learn/CPlusPlus/1616/ https://www.sololearn.com/learn/CPlusPlus/1614/ _______________________ https://code.sololearn.com/cpakDhbUAVml/?ref=app https://code.sololearn.com/czdPEdWgcfQ3/?ref=app


Bakhromjon khasanboyev ++ X //pre increment , it first increment value of x and assign to the variable . X++ //post increment,it takes current value of x and then increments. Let it explain by small example: Int a = 2; Int b= a++; Cout<<"a="<<a<<edl; Cout<<"b="<<b; //Output is a=3 b=2 Int a=2; Int b=++a; Cout<<"a="<<a<<endl; Cout<<"b="<<b; //Output is a=3 b=3


they r pretty the same , with some diff: in JS : =========================== while(condition is true){ //do something //you have to take care of the condition so it will not loop forever } =========================== for(let i= 0 ; i<5 ; i++){ //do something } =========================== //the for loop above will do something 5 times. ××××××××××××××××××××××× And you must google it , for better understanding


http://www.cplusplus.com/forum/beginner/24138/ This website has everything about cpp, literally, *everything*. BTW this link gives an answer to the for vs while loops.


I had a question about C ++. Can you explain the difference between ++x and x ++? Please!