Mathematica 9 is now available
Student Support Forum
-----
Student Support Forum: 'For loop and if statement not getting implemented' topicStudent Support Forum > General > "For loop and if statement not getting implemented"

< Previous CommentHelp | Reply To Comment | Reply To Topic
Author Comment/Response
Bill Simpson
09/04/12 00:18am

I inserted some Print[]; to see what was going on.

You have
...
For[i=1, i≤numofseg, i++
...
but numofseg==1 so your For will only execute one time. If you put Print["i is ", i]; just inside your For you can verify this

Inside your For you have
if[Mod[i, 2] == 0,thenpart,elsepart]
so with i==1 it seems like only the else part will execute. But when I put a Print inside the thenpart and elsepart I see both Print!

AHA! Look carefully at "if[Mod[i, 2] == 0," Notice that is not "If[Mod[i, 2] == 0,", see that lack of capitalized I in If!

I do see one error message about 1/0 and two error messages about part 2 and 3 of t does not exist. Those really should be tracked down.

Now why?!?! is the thenpart and elsepart both executing... AHA! Look carefully at "if[Mod[i, 2] == 0," Notice that is not "If[Mod[i, 2] == 0,", see that lack of capitalized I in If! When I correctly capitalize that If I see that only the elsepart now executes.

Perhaps someday Mathematica will include a "novice mode" that will point out incorrectly capitalized reserved words and other things that repeatedly bite new users and even some experienced people. It took me repeated tries before I noticed what the error was.

So fix that error and see if you still have problems.

URL: ,

Subject (listing for 'For loop and if statement not getting implemented')
Author Date Posted
For loop and if statement not getting implemented Akhilesh 09/01/12 03:56am
Re: For loop and if statement not getting imple... Bill Simpson 09/04/12 00:18am
< Previous CommentHelp | Reply To Comment | Reply To Topic