Pascal guide - If & Case

  today we will talk about ,If_then_else,Case_of
  they are condition checking function,the condition include (OR ,AND)
  and conditional checking only invole (>,<,<=,>=,not)

  1-if_then_else,
     thats means if (condition=true)/OR (condition=true)/And (Condition=true) then (do something)
                        else (condition between if then=wrong)
                         (do something)
     compond statement  is provided for this work.

     sample 1:
     program IFTHENELSE;
     const              {const is used  to put a number or string to a variable}
      a=1;
      b=2;
     begin
       if (a>b) then
        writeln('A is greater then  B')       {semi colon are not allowed in this time}
       else
        ('B is greater then A');                            {and simply ,else can be removed}
     end.

     sample 2:
     program IFTHENELSE2;
     const           {const is used to put a number or string to a variable}
      a=1;
      b=2;
      c=3;
     begin
       if (a>b) then
        begin
          if (a>c) then
           writeln('A is the greatest')
          else if (c>a) then
           writeln('C is the greatest')
        end;
       else
         begin
           if (c>b) then
            writeln('C is the greatest')
           else
             writeln('B  is the greatest')
          end;
     end.

    sample 3:to show use of not ,>=,<= ,and ,or
    program notS;
    const
      a=1;
      b=2;
    begin
      if (a=b) then writeln('a is equal to b');
      if (a>b) or (a=b) then writeln('a is greater then or equal to b');
      { the above statement can be shorten to IF (a>=b) then ('.....');}
      if (a<b) and (a=b) then writeln('A is smaller then and equal to b');
      {the above line have error,A number can't smaller then and equal to other number}
      if (a=b) and (b=2) then writeln('A equal to B and equal to 2');
      if Not(a=b) then writeln('A is not equal to B');
      {the above line  can be replace by if (A<>B) then writeln('.....');
       this NOT(a=b) method is always used in C language}
     end.
 

  2.case of
     some time if then statement is too long to the condition!!
     so case_of statement is deleveloped like a switcher,
     which equal to the swith statement of C;
     sample 4:
     program CASEOF;
     var
      a:integer;
    begin
      write('put number here:(1-4)');
      readln(a);
      case a of
      1:writeln('a=1')
       2,3:writeln('a=2 or a=3');
       4:writeln('a=4');
       end;  {end are required for case statement}
      end.
    {we use if then statement to compare}
    if a=1 then
     writeln('a=1');
    if (a=2) or (a=3) then
     writeln('a=2 or a=3')
    else
      writeln('a=4');
    {which is longer then case statement!!}

<< Previous || Next >>