If statements

Hey, I am writing a compiler at the moment, and I could use the input of some experienced programers like yourselves.

I am working on how i want my if statements to work.

In C++ if statements follow this kind of rule
If the condition is not false then we will execute

And can be seen from
1
2
3
4
if (3) 
     cout << "This will work";
if (false)
     cout << "This will not work";




But I was also considering using a rule-set that follows
The condition MUST BE true to execute

And can be seen from
1
2
3
4
if (3) 
     cout << "This will NOT work";
if (true)
     cout << "This will work";


I was just wondering if either of these would be better on the user end of the language...
Last edited on
You could enforce that the expression in the if statement must always evaluate to a boolean type without implicit casts ;)
I wish it was that simple, but in my language I do not have a "bool" type. It only has two data types, the first being "number" (which is essentially a double in C++ terms), and "text" (which is similar to the std::string, but more functional).

(Remember that true/false are actually just the numbers 1 and 0)

Because my language does not have a bool type, look at this code:
1
2
3
4
5
6
7
8
9
10
main begin

    /////// THIS IS THE IMPORTANT LINE
    number X = true


    if X begin
        write("X = true")
    end
end


Now, because a "number variable" can hold any number I could have had line 4 be:
number X = -34.567

Now, that obviosly does not equal true, so should the if statement work? But, it also does not equal false.... I dont know what to do???
If you do not have a Boolean type, then what type do true and false evaluate to? Are they numbers?

If so, you could just say that if statements only evaluate if the provided number is = true (which I assume would be equal to 1)

So the user inputting this:
1
2
3
if X begin
    foobar
end

would be functionally identical to this:
1
2
3
if X = true begin
    foobar
end


The problem with that is that it'd be possible for X to be 'true' by mathematical computation, rather than being the result of a Boolean expression.

1
2
3
4
5
// assuming true == 1
X = 2
X -= 1

if X begin  // <- now this will work because X==1 which is ==true 


If you do not make a distinction between numerical and boolean types, this problem is unavoidable.
Topic archived. No new replies allowed.