meme
meme

Reputation: 27

C++ How to make a variable true if another variable WAS true

I was wonder how could I make a bool true if another bool was true for example with this program :

for(int i=1;i<5;i++){
  if (i == 3){
    x = false;
  } else{
    x = true;
  }

  if(x){
    cout << "true";
  }else{
    cout << "false";
  } 
}

output :
true
true
false
true
true

how would I make it so that the output is this:

output:
true
true
false
false
false

sorry if this is hard to understand I'm not the best at explaining

Upvotes: 0

Views: 105

Answers (2)

Ashish K
Ashish K

Reputation: 935

int flag = 0;
for(int i=1;i<5;i++){
  if (i == 3){
    x = false;
    flag = 1;
  } else{
    x = true;
  }

  if(x && flag == 0){
    cout << "true";
  }else{
    cout << "false";
  } 
}

If you specifically wish to switch your output upon getting 3, you can flag up the case where there is a change in x. See the code above. This is in accordance to your code pattern.

Upvotes: 1

user3811082
user3811082

Reputation: 261

Try this code

x = true;

for(int i=1;i<5;i++){
  if (i == 3){
    x = false;
  }

  if(x){
    cout << "true";
  }else{
    cout << "false";
  } 
}

Upvotes: 0

Related Questions