pascalfreepascalpascals-triangleturbo-pascal

how do I rectify this switch case error in pascal?


This is a pascal program that returns factor . rate is the input which is given by the user. This program is throwing me error. please do have a look and help.

I want to rectify the error. I'm not able to find out the error since im very new to pascal and im trying to learn

program Mss; 
var
   rate,factor:integer;
begin
  readln(rate);
  case rate of
  1..2:begin
         factor:=(2*rate)-1;
         writeln(factor);
       end
  3:begin                             throws error here
      factor:=(3*rate)-1;
      writeln(factor);
    end
  4:begin
      factor:=(4*rate)-1;
      writeln:=(factor);
    end
  5:begin
      factor:=(3*rate)-1;
      writeln(factor);
    end
  6..8:begin
         factor:=rate-2;
         writeln(factor);
        end
   else begin        
          writeln(rate);
        end  
end;

This is a switch case which returns factor. rate is the input from user. this throws me an error.

Fatal: Syntax error, ";" expected but "ordinal const" found

Solution

  • You have a few syntax errors. Your begin/end blocks need to be followed by ;.

    writeln:=(factor) should be writeln(factor).

    And you need an end. to finish the program.

    program Mss; 
    var
       rate,factor:integer;
    begin
      readln(rate);
      case rate of
      1..2:begin
             factor:=(2*rate)-1;
             writeln(factor);
           end;
      3:begin               
          factor:=(3*rate)-1;
          writeln(factor);
        end;
      4:begin
          factor:=(4*rate)-1;
          writeln(factor);
        end;
      5:begin
          factor:=(3*rate)-1;
          writeln(factor);
        end;
      6..8:begin
             factor:=rate-2;
             writeln(factor);
            end;
       else begin        
              writeln(rate);
            end  
      end
    end.
    

    Also, please note that ; in Pascal is a separator, so you could write code like:

      6..8:begin
             factor:=rate-2;
             writeln(factor);
            end;
    

    As:

      6..8:begin
             factor:=rate-2;
             writeln(factor)
            end;