0

Loops

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

12th Nov 2020, 8:57 AM
Nandini Reddy
7 Réponses
+ 10
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.
12th Nov 2020, 9:15 AM
Lothar
Lothar - avatar
+ 5
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......
12th Nov 2020, 9:15 AM
Alphin K Sajan
Alphin K Sajan - avatar
+ 4
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☺👍
13th Nov 2020, 2:39 AM
Mohan
Mohan - avatar
+ 2
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
12th Nov 2020, 5:17 PM
Nandini Reddy
+ 1
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
12th Nov 2020, 9:19 AM
Med Amine Fh
Med Amine Fh - avatar
0
I had a question about C ++. Can you explain the difference between ++x and x ++? Please!
12th Nov 2020, 4:22 PM
Begzodbek Khasanboyev
Begzodbek Khasanboyev - avatar