-1

I'm new to programming, and I want to write some code like

while(condition_A and condition_B):  
    #Do something

But each time I run the while loop, I want to check condition A first, and if condition A works then check condition B. For example, condition A checks if condition B will get an array out of bounds error or something. And finally if both conditions are true stay in the while loop. How should I do this? I was thinking of something like

 def some_While_Loop:
      if condition_A == False:
          return
      while (condition_B):
         #Do something
         if condition_A == False:
               return

But then the while loop has to be the last thing a function does. Is there a nicer/better way?

Irfan434
  • 1,463
  • 14
  • 19
Chris Z
  • 139
  • 5
  • It would help a lot if you told us which programming language you're doing this in as many languages do this out of the box with the right syntax. – Lasse V. Karlsen Aug 11 '16 at 13:26
  • Your question is not clear to me can you explain a bit... – Rupsingh Aug 11 '16 at 13:27
  • The syntax looks like Python, in which case your first example is already correct but is then a duplicate of this question - http://stackoverflow.com/questions/2580136/does-python-support-short-circuiting - this feature is called "short circuiting logical operators". – Lasse V. Karlsen Aug 11 '16 at 13:29

1 Answers1

1

It looks like you're using python. You actually had the answer yourself:

while(condition_A and condition_B):  
    #Do something
Irfan434
  • 1,463
  • 14
  • 19