Gabriel
Gabriel

Reputation: 5734

Case statement with logic operators (< > =, etc) in Pascal

I'm having trouble making this work. Apparently, i can't use > or < in the case sentence, is there a workaround for this? Thanks!

case num of
    0:
        begin
            cont_0 := cont_0 + 1;
        end;
    > 0:
        begin
            cont_pos := cont_pos + 1;
            sum_pos  := sum_pos + num;
        end;
    < 0:
        begin
            sum_neg := sum_neg + num;
        end;  
    else;
end;

Upvotes: 0

Views: 1537

Answers (2)

Sebastian Ullrich
Sebastian Ullrich

Reputation: 1230

case Sign(num) of
    -1: ... 
     0: ...
     1: ...
end;

More readable than if ... else if ... else? You decide.

Upvotes: 6

Esteban K&#252;ber
Esteban K&#252;ber

Reputation: 36862

Don't use case then, why not use if?

if num = 0 then
        cont_0 := cont_0 + 1;
if num > 0 then
BEGIN
        cont_pos := cont_pos + 1;
        sum_pos  := sum_pos + num;
END
if num < 0 then
        sum_neg := sum_neg + num;

Upvotes: 0

Related Questions