Put "||" (or) in a while in C ++

3

I'm learning C ++. I was doing some experiments, including a program where there are 2 variables with a value of 100 and a number is randomly subtracted from these variables:

#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main(){
    int vida = 100, evida = 100, rnd, rnd2;
    srand(time(NULL));  
    while(vida >= 0 || evida >= 0){
        rnd = 1+rand()%(11-1);
        cout<<vida<<"  "<<evida<<endl;
        rnd2 = 1+rand()%(3-1);
        if(rnd2 == 2){
            evida -= rnd;
        }
        else{
            vida -= rnd;
        }
    }
        return 0;
    }

The idea is that when the variable vida or evida reach 0 stop while , but instead wait for the 2 variables are less than or equal to 0 . What can I do?

    
asked by binario_newbie 14.09.2018 в 06:53
source

3 answers

4
  

I'm learning C ++.

In that case, allow me to welcome you \ñ_ñ/ and give you some tips:

  • Headers <stdlib.h> and <time.h> are from not from . These headers have a version adapted to C ++ that has the prefix c and has no extension. If you really need to use the C headers (which will never be the case) you should use the C ++ equivalents <cstdlib> and <ctime> . Read this thread to find out why.
  • There is no obligation to use the using namespace std; clause since it is only an aid to the writing of code; If you decide to use this clause do not do it in the global scope, use it in the smallest possible scope. Read this thread to find out why.
  • The rand() and srand() functions belong to the C libraries, it is discouraged to use those C ++ utilities because they may not be portable and may offer questionable results and performance, therefore is being studied deprecarlo . From the C ++ 11 standard there is a complete library of generation of pseudo-random numbers that you should use instead. Read this thread to find out why.
  

The idea is that when the variable vida or evida reach 0 stop the while , but instead wait for the 2 variables are less than or equal to 0 .

That is the expected behavior; in a condition joined by an or ( || ) the expression will be true as long as any of the expressions is true and consequently it will be false when both expressions are true:

|            tabla de la verdad OR           |
+-------------+-------------+----+-----------+
| expresión 1 | expresión 2 | || | resultado |
+-------------+-------------+----+-----------+
|    falso    |    falso    | || |   falso   |
|    falso    |  verdadero  | || | verdadero |
|  verdadero  |    falso    | || | verdadero |
|  verdadero  |  verdadero  | || | verdadero |

You want a condition and ( && ), which will be true when all the expressions are true and therefore will be false in the other cases:

|           tabla de la verdad AND           |
+-------------+-------------+----+-----------+
| expresión 1 | expresión 2 | && | resultado |
+-------------+-------------+----+-----------+
|    falso    |    falso    | && |   falso   |
|    falso    |  verdadero  | && |   falso   |
|  verdadero  |    falso    | && |   falso   |
|  verdadero  |  verdadero  | && | verdadero |

Therefore your while , with and ( && ):

while(vida >= 0 && evida >= 0)

It could be read as " As long as vida is greater than or equal to 0 and evida is greater than or equal to 0 ".

    
answered by 14.09.2018 / 08:43
source
4

You want that when at least life or evida is less than or equal to zero, leave the while, that is:

vida <= 0 OR evida <= 0

That is the condition for it to come out so the condition for it to be maintained is the denial:

~(vida <= 0 OR evida <= 0)     =
~(vida <= 0) AND ~(evida <= 0) =
  vida > 0 AND evida > 0

So the correct condition is:

vida > 0 && evida > 0

Code:

#include <iostream>
#include <random>

int main(){
    int vida = 100, evida = 100, rnd, rnd2;
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<int> dis1(1, 11);
    std::uniform_int_distribution<int> dis2(1, 3);

    std::cout<<vida<<"  "<<evida<< "\n";
    while(vida > 0 && evida > 0){
        rnd = dis1(gen);
        rnd2 = dis2(gen);
        if(rnd2 == 2)
            evida -= rnd;
        else
            vida -= rnd;
        std::cout<<vida<<"  "<<evida<< "\n";
    }
    return 0;
}

Exit:

100  100
89  100
88  100
88  92
88  87
79  87
79  80
79  75
79  70
70  70
60  70
50  70
41  70
40  70
40  61
39  61
39  53
39  47
37  47
28  47
19  47
19  42
19  36
14  36
14  25
14  21
14  17
14  7
9  7
8  7
-1  7
    
answered by 14.09.2018 в 07:23
0

You have two options, or put a logical AND ( && ) instead of O ( || ) and a stricter ( > ) :

#include <iostream>
#include <stdlib.h>
#include <time.h>

int main() {
    int vida = 100, evida = 100, rnd, rnd2;
    srand(time(NULL));
    while (vida > 0 && evida > 0) {
        rnd = 1 + rand() % (11 - 1);
        std::cout << vida << "  " << evida << std::endl;
        rnd2 = 1 + rand() % (3 - 1);
        if (rnd2 == 2) {
            evida -= rnd;
        } else {
            vida -= rnd;
        }
    }
    return 0;
}

Or change the greater or equal ( >= ) by less or equal ( <= ) all denied ( ! ):

#include <iostream>
#include <stdlib.h>
#include <time.h>

int main() {
    int vida = 100, evida = 100, rnd, rnd2;
    srand(time(NULL));
    while (!(vida <= 0 || evida <= 0)) {
        rnd = 1 + rand() % (11 - 1);
        std::cout << vida << "  " << evida << std::endl;
        rnd2 = 1 + rand() % (3 - 1);
        if (rnd2 == 2) {
            evida -= rnd;
        } else {
            vida -= rnd;
        }
    }
    return 0;
}
    
answered by 14.09.2018 в 07:23