Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi im learning to do loop in C#

have diagram like this

C#
if (File.Exists(cFiles)){
ReadCode
}
else {
Open Files
//back to check condition
}


i want to make else return to check files again.

What I have tried:

i can make it with
C#
else {
Open Files
ReadCode
}


but since Read code is too big i want to avoid this
i try to make a label then place goto but it bring error
C#
thislabel:
if (File.Exists(cFiles)){
ReadCode
}
else {
Open Files
goto thislabel:
}
Posted
Updated 19-Mar-17 19:30pm

1 solution

Hi Member 12230809,

Never use Goto, it is a forbidden material that is put on the shelf - you can just know about it, but never touch it :).

Just wrap your code with a WHILE loop. Essentially, it offers the same effect as the Goto.
Open Files
while(true)
{
   if (File.Exists(cFiles))
   {
      ReadCode
      break;
   }
   else
   {
      Open Files
      // Put a break here on a certain condition based on the scenario.
   }
}
However, since we don't know what your 'open files' does, you need to put some kind of condition at the else block. E.g.: quitting if number of files that are checked so far reaches the total files etc. Otherwise the loop will continue forever causing the program to crash.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900