Results 1 to 5 of 5

Thread: Multiple condition in while

  1. #1
    Join Date
    Jun 2018
    Posts
    3
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Multiple condition in while

    I have this code:
    Qt Code:
    1. bool state=true;
    2. qDebug() << "Entering while...";
    3. i=0;
    4. while((state==true)||(i<3))
    5. {
    6. state=false;
    7. qDebug() << "Value of counter: "<<i;
    8. i++;
    9. }
    10. qDebug() << "End of while";
    To copy to clipboard, switch view to plain text mode 
    but the first condition seem not to work.
    The output is:

    Qt Code:
    1. Entering while...
    2. Value of counter: 0
    3. Value of counter: 1
    4. Value of counter: 2
    5. End of while
    To copy to clipboard, switch view to plain text mode 

    If I remove the second condition (i<3), I have

    Qt Code:
    1. ...
    2. while(state==true)
    3. ...
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //Output
    2.  
    3. Entering while...
    4. Value of counter: 0
    5. End of while
    To copy to clipboard, switch view to plain text mode 
    It seems that only one condition is allowed in while loop.
    Is it possible?

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,708
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Multiple condition in while

    I fail to see what is the problem.
    The output you are getting is exactly what it should be with that code.
    What is the output you expect?
    OR
    What do you want your while() condition to express?

    Do you want both conditions to apply or any of them?
    At the moment any of the conditions apply.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jun 2018
    Posts
    3
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Multiple condition in while

    My idea (ok, it's wrong...):
    while "state" is true OR i<3 -> cycle the code.

    So if "state" is false OR i> or = 3 the {code} would not be executed.
    I.E., when state is false while i==2,code isn't executed.
    This seems as a && (state is true AND i=3)...

  4. #4
    Join Date
    Jan 2017
    Posts
    58
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Multiple condition in while

    Yes, in the while condition have to be && and not ||. In the case of ||, the first condition state is false but the second condition is still true.

  5. #5
    Join Date
    Jun 2018
    Posts
    3
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Thumbs up SOLVED -- Re: Multiple condition in while

    Thanks!

Similar Threads

  1. Replies: 3
    Last Post: 30th September 2013, 11:10
  2. Using an If condition in a QML delegate?
    By technoViking in forum Qt Quick
    Replies: 8
    Last Post: 25th August 2011, 05:26
  3. how to set wait condition
    By hema in forum Newbie
    Replies: 5
    Last Post: 25th July 2011, 08:08
  4. don`t work Condition...
    By Resager in forum Newbie
    Replies: 1
    Last Post: 6th May 2011, 18:18
  5. Sleep condition in QT
    By santhoshv84 in forum Qt Programming
    Replies: 1
    Last Post: 18th July 2008, 12:07

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.