Cannot figure out if statement

3.1k views Asked by At

Here is the code i am working with, it tests for the input of 'b' and should output 'Y' if there was anything else inputted.

Please, nobody ask why I chose to use 'Y' for a false value..

> , <
+++++ +++++
[
    > ----- ----
    < -
]
> ----- --- <


[
    >
    +++++ +++++
    [
            > +++++ +++
            < -
    ]
    > +++++ ++++ <
    . <
]

Would like the equivalent of

char c;
c = getchar();
if (c == 'b')
    putchar('Y');

I am getting no output (directed it at a file and hexdumped it, as well as nothing on the screen) no matter what I put into the program

2

There are 2 answers

0
mange On BEST ANSWER

Your issue is that when you reach your second loop you always have a value of zero in the current cell, so your second loop never runs. You need to be one further cell to the right to get the result of your modifications to the input, and then you need to do work only if the value is zero (unlike loops, which will only do work if the value is not zero).

To make a conditional I would suggest grabbing something from a site with some code someone else has come up (an example I found).

At any rate, this is the code I came up with to solve your problem:

,

7 * 7 * 7 = 98 = 'b'
> +++++ ++
[ > +++++ ++
  [
    << --
    >> -
  ]
  < -
]
<

expect everything to be zero here

[ >+ >+ <<- ]
> [ <+ >- ] +
> [ <- >[-] ]
< [ <
  >> [-]
  (11 * 8) plus 1 = 89 = 'Y'
  +++++ +++++ +
  [ << +++++ +++ >> - ]
  << + .
  > -
]
1
Ustym Ukhman On

After almost 2 years you probably don't need this anymore, but here's my version of "if - else" statement:

+++++ +++++
+++++ +++++
+++++ +++++
+++           (this is the value you want to check, in this case it's the "!")

> , <         (then you take input an char to compare)
[ - > - < ]   (after that you have to check if their ascii values are the same)
>>            (and initialize the answer on "true")

+++++ +++++
+++++ +++++
+++++ +++++
+++++

< [ - > ] > . (if "false", this loop will be executed and the answer won't be printed)

Hope it's usefull to someone today. :D