Solved

Syntax for While(Condition1 or Condition2)

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to remove duplicate code from my project 5 48
How can I do Unit Testing with Session ? 1 23
Problem to copy file 14 50
asp.net repeater 2 5
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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

756 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