Solved

Syntax for While(Condition1 or Condition2)

Posted on 2013-11-14
2
309 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

740 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