Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 8 Online
OpenStudy (anonymous):

I don't really understand the distinction between the for and while loops. When would you use one as opposed to the other? Additionally, when would you use the print vs return functions? Thank you!

OpenStudy (anonymous):

use a for loop to iterate over a sequence or to execute a finite number of times use a while loop to execute code 'while' a condition is true

OpenStudy (anonymous):

you can generate table, do some work untill die game is an example of loop. there are three types of loop for, do-while and while the difference between is only one which is in do-while loop this loop will execute once if the given condition is wrong other loop will not execute if the given confdition id wrong...

OpenStudy (queelius):

I like bwCA's answer. Both the for and while loop constructs are sufficient for any looping purpose, but the for is designed for iterating over sequences, and the while is for looping while a condition is true.

OpenStudy (anonymous):

While will run as long as the condition is met, for example if you say a program to run while 1>0, it will run indefinitely. A for loop runs once for every value of the range you assign. So if you say a program to run for a range of 100, it will run a hundred times. Or if you want a program to check all the letters in a word to see if there's an "a", then the program will run one time for each letter of the word and then continue. You will know when to use a for and a whole according to your program, if you're new just keep going. As for return and print, return is used to return values which won't show on screen, and print doesn't return anything just prints it on the screen. You can have a function that calculates your expenses and another that calculates you income. If you're making a program to calculate how much money you have left you don't need to print expenses and income just show the final number. For that, you return a value from both functions and use them to calculate a number which you will print.

OpenStudy (anonymous):

you can also use for loop for untile condition trur her is syntax for(; condition != 0 ; ){ //statements }

OpenStudy (anonymous):

Generally a for loop is used in cases where a number is somehow involved in the computation, more like a counter that can be used for the iteration part. You can also use a while loop here but you need to be careful to write the iteration statement at the end of the loop: Eg: for(int i=0; i<n; i++){ .....} v/s i=0; while(i<n){ ..... i++;} In other programming languages especially for loops play an important and convenient role is traversing collections, lists etc. print is used when you want to print something on the console. return is used to return a value from a procedure to the caller who may be interested in processing the value further and not necessarily print it. Eg. def func(): print "Hello World" AND def func(): return "Hello World" print func()+"Again!!!" In the second example you could return a string and modify it before printing it.

OpenStudy (anonymous):

the one thing I did to get a good grasp on the difference between the while and for loop was write some small code to see what each can do. i found out that the for loop is really powerful and versatile. Try this as an example: x = '123' for i in x: print (i) This will print 1 2 3 the cool thing about a for loop is that you don't have to define the variable 'i' before the for loop, you can just insert it in the for statement and 'i' will adopt the value of each character through the iteration

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!