### How do I replace an if with a bool? So im a beginner in programming and i need help with replacing the if below with anything else.I tried to use bool but it didnt work.So if someone can help me thank you.

 ``123456789101112131415161718192021`` ``````#include using namespace std; int main(){ int i,a={4,3,5,8},b={3,7,8,4},c={0,0,0,0},p=0; for(i=0;i<4;i++){ b[i]=10-b[i]; } for(i=3;i>=0;i--){ c[i]=(a[i]+b[i]-p)%10; p=(a[i]+b[i]-p); if(p<10) // <---- This if p=1; else p=0; } for(i=0;i<4;i++){ cout<
Last edited on If you have to test that p is less than 10, you don't have many choices.

One option is the ternary operator:
 `` `` `` p = (p<10) ? 1 : 0;``

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button. Booleans are not conditionals.

Booleans can STORE the RESULT of binary condition statements.

bool b = p < 10; //b is either true or false, OK

but you still need

if(b)
...
else
....

you can tighten the code with
p = p<10; //

which only works because P is 1 or 0. If you needed

if(p<10)
p = 3;
else
p = -17;

You could not do it. `p=int(p < 10);`

That will perform an explicit conversion of a bool to an int. You can do the implicit `p=(p<10);` but it is less clear that you actually intended to convert the value. The compiler won't care, but the next programmer to maintain the code will. That one cast isn't going to fix the totally unreadable gibberish of the original. I suppose every little bit helps :) You're complaining about gibberish code on this forum? That's pretty much all I ever see. No, I am not complaining, just observing.
Topic archived. No new replies allowed.