Multiple conditions for the while loop?

This is a discussion on Multiple conditions for the while loop? within the C++ Programming forums, part of the General Programming Boards category; How do you make multiple conditions for the while loop? Example: loop while the variable a = 1 and the ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    39

    Multiple conditions for the while loop?

    How do you make multiple conditions for the while loop? Example: loop while the variable a = 1 and the variable b = 2?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Like this.
    Code:
    while (a==1 && b==2)
    {
       ...
    }
    The loop will continue as long as a is 1 and b is 2.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    39
    Thanks

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    39
    Oh and how about or? Such as a=1 or b=2?

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    To loop while a is 1 or b is 2 or both conditions are met would be
    Code:
    while (a==1 || b==2)
    {
       ...
    }
    There are some tutorials that cover these things at this site.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    How do you make multiple conditions for the while loop?
    If you have a C++ book, 'conditionals' are usually covered with if-statements, so you might look there.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Or "logical operators", because that's what && and || are.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can a "switch" be inside a loop?
    By gmk0351 in forum C Programming
    Replies: 5
    Last Post: 03-28-2008, 06:47 PM
  2. Rewriting a for loop as a while/do-while loop
    By Ashfury in forum C++ Programming
    Replies: 7
    Last Post: 04-27-2007, 03:20 PM
  3. Modeless Dialogs in Multiple threads
    By MrGrieves in forum Windows Programming
    Replies: 0
    Last Post: 06-22-2004, 02:33 PM
  4. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 04:45 PM
  5. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 09:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21