Re: strange behavior
- To: mathgroup at smc.vnet.net
- Subject: [mg104837] Re: strange behavior
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Wed, 11 Nov 2009 04:28:36 -0500 (EST)
On 11/10/09 at 5:58 AM, Jason_member at nntpjunkie.com (Jason Winters) wrote: >Hi! I am wondering why these two modules behave differently >Module[{ans = ""}, >For[i = 1, i < 10, i++, >ans = InputString["Enter a String"]; >Print[ans] >] >] >and >Module[{ans = ""}, >For[i = 1, i < 10, i++, >ans = InputString["Enter a String"] >Print[ans] >] >] >One prints out a bunch of extraneous Null's The difference is the ; following the For loop. With the semicolon present, you have a compound expression. When Mathematica encounters a compound expression, the output is the result of evaluating the portion following the last semicolon. In this case, a Print statement. Without the semicolon, Mathematica does a multiply operation. That is For[...] Print[...] is seen as a directive to multiply the results of evaluating For[...] by the results of Print[...]. The output of a For loop is Null which explains the results you got.