Solved

Syntax for While(Condition1 or Condition2)

Posted on 2013-11-14
2
299 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

762 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now