Solved

Syntax for While(Condition1 or Condition2)

Posted on 2013-11-14
2
302 Views
Last Modified: 2013-11-15
I have a while loop that should continue while either condition 1 or condition 2 is true.

Condition 1: ((intLinesProcessed) <= intMaxtoProcess)  // Lines processed are less than the max lines to process.  
Condition 2:  sr.ReadLine()) != null) // Reads to the eof

while ((intLinesProcessed) <= intMaxtoProcess)  ||  (sr.ReadLine()) != null)

What is the proper syntax?

Thanks,
0
Comment
Question by:Dovberman
2 Comments
 
LVL 5

Accepted Solution

by:
Dontmilkthis earned 300 total points
ID: 39649673
I think your code is almost syntactically correct, although you've got an extra parenthesis. also you don't strictly need them around each condition

while (intLinesProcessed <= intMaxtoProcess  ||  sr.ReadLine() != null) 
{
  //do stuff
  intLinesProcessed++;
}

Open in new window


also, by using the ReadLine in the if clause, you're consuming a line from the stream reader. if you want to use the line it should be set to a variable.

so i'd change the second condition to use the end of stream flag, so you can actually do stuff with the line

while (intLinesProcessed <= intMaxtoProcess  || !sr.EndOfStream) 
{
  line = sr.ReadLine();
  //do stuff
  intLinesProcessed++;
}

Open in new window


This will still cause a problem where you've set intMaxtoProcess to a higher number than the actual number of lines.
eg:
intMaxtoProcess = 10
intLinesProcess = 2

your condition will resolve to true and will try to execute, when it attempts a ReadLine it will throw an exception.

I think what you really want is an AND not an OR. this way, your loop will execute until you've processed the desired amount of lines, or you've reached the end of the stream.

while (intLinesProcessed <= intMaxtoProcess  && !sr.EndOfStream) 
{
  line = sr.ReadLine();
  //do stuff
  intLinesProcessed++;
}

Open in new window

0
 

Author Closing Comment

by:Dovberman
ID: 39651590
Thank you for the complete detailed description.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
.net and XML report from SCCM 5 25
asp day pilot 3 19
application restarter  - for an exe that hangs 9 27
Calculate number of nights between two dates 5 25
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question