dimanche 26 novembre 2017

I want the loop running until numbers are: 123

I have 2 different codes that to me look the same, but they aren't. The first one works as I want, the second doesn't. I don't understand why.

#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;

int main()
{
    int num1, num2, num3, i=0;
    srand(time(0));
    do{
    i++;
    num1=rand()%3+1;
    num2=rand()%3+1;
    num3=rand()%3+1;
    cout<<i<<"."<<num1<<num2<<num3<<endl;
    }while(!((num1==1)&&(num2==2)&&(num3==3)));

}

This is the second one. As I understand it, do-while loop should run until num1 doesn't equal to 1, num2 doesn't equal to 2 and num3 doesn't equal to 3.

#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;

int main()
{
    int num1, num2, num3, i=0;
    srand(time(0));
    do{
    i++;
    num1=rand()%3+1;
    num2=rand()%3+1;
    num3=rand()%3+1;
    cout<<i<<"."<<num1<<num2<<num3<<endl;
    }while((num1!=1)&&(num2!=2)&&(num3!=3));

}




Aucun commentaire:

Enregistrer un commentaire