Nested If & while statements


BH2114
 Share

Recommended Posts

I have a sequence as below which works:

	while(1)
private st = systime()
while(systime() - st < logInterval*60)
   delay(.2)
   if (logFlag==0)
      break
   endif
endwhile
 if (logFlag==0)
      break
      delay(.1)
   endif
FileHandle = File.Open(NewFileName,0,1,1,1)     
private string out= ChannelData 
File.Write(FileHandle,out)
File.Close(FileHandle)    
wait(.1)
endwhile
	

This sequence writes (channelData) to a file every (loggingInterval*60). When (logFlag =0), the while loop breaks and the sequence continues to other tasks.

I have one additional variable (step) that starts at 0 and increments by (1) to the value (8). when logFlag =0, step is reset to  =0

I would like to write (channelData) once each time (step) increments, in addition to the normal file writes that take place as a result of (logInterval*60).

Just cant figure out how to nest the if statement (if thats what should be used) and updating step in the same manner that (st) is being updated also does not seem to work.

Note: this appends to a file that was previously created.

Link to comment
Share on other sites

I would move the file writing part to a separate sequence function:

function writeLine()
   FileHandle = File.Open(NewFileName,0,1,1,1)     
   private string out= ChannelData 
   File.Write(FileHandle,out)
   File.Close(FileHandle) 

Then call it where you need it in your loops.  That way you can do your step check inside the inner loop and also log outside the loop.

BTW: watch your indentation.  3 spaces in for every block (while, if, etc), and 3 spaces out for end of block (endwhile, endif, etc.)

 

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share