User Tools

Site Tools


Sidebar


Introduction


Basic Tutorials


Advance Tutorials


Useful Techniques


Examples

  • Simple Pipe Weight Calculator
  • Unit Convertor

Sidebar

tutorial:conditionalstatement

This is an old revision of the document!


Conditional Statements

If - Statements

Statements Syntax
If..Then
//---Single Statement---
if {Condition} then {Statement}; 

//---Multiple Statements---
if {Condition} then 
 begin
  {Statement1};
  {Statement2};
  {...};
 end;
If..Then..Else
//---Single Statement---
if {Condition} then 
  {Statement1}
else
  {Statement2};

//---Multiple Statements---
if {Condition} then 
 begin
  {Statement1};
  {Statement2};
  {...};
 end
else
 begin
  {Statement3};
  {Statement4};
  {...};
 end;


Example-2: If Statements (Nested If)

Example-2: If Statements (Nested If)

ตัวอย่าง โปรแกรมตัดคะแนน ให้กลายเป็นเกรด A-F

program Nested_If_Then_Else;

var Score:real;

begin
  write('Please insert your score(0-100): ');
  readln(Score);

  if (Score<0)or(Score>100) then
    writeln('Your score is not correct!!')
  else
    if Score<50 then
       writeln('Your Grade is : F')
    else if Score<60 then
       writeln('Your Grade is : D')
    else if Score<70 then
       writeln('Your Grade is : C')
    else if Score<80 then
       writeln('Your Grade is : B')
    else
       writeln('Excellent!, your Grade is : A');

  readln();
end.    



Case - Statements

Statements Syntax
Exact value
//---Single Statement---
case {Var} of 
  1: {Statement1};
  2: {Statement2};
  3: {Statement3};
  else {Statement4};
end; 

//---Multiple Statements---
case {Var} of 
  1: begin 
      {Statement1};
      {Statement2};
      ...;
      end;
  2: begin
      {Statement3};
      {Statement4};
      {Statement5};
      ...
      end;
  3: {Statement6};
  else {Statement7};
end; 
Range
//---Single Statement---
case {Var} of 
  1..10: {Statement1};
  11..20: {Statement2};
  21..30: {Statement3};
  else {Statement4};
end; 


Example-3: Case Statements

Example-3: Case Statements

ตัวอย่าง โปรแกรมตัดคะแนน ให้กลายเป็นเกรด A-F

program TestCaseStatement;

var score:integer;
  Grade:char;

begin
  write('Insert your score (0 to 100): ');
  readln(score);
  
  case score of
    0..50: Grade:='F';
    51..60: Grade:='D';
    61..70: Grade:='C';
    71..80: Grade:='B';
  else Grade:='A';
  end;
  
  writeln('Your grade is: ',Grade);
  readln();
end.  

Compiled Results:

Insert your score (0 to 100): 35
Your grade is: F



Break, Continue and Goto

tutorial/conditionalstatement.1546483842.txt.gz · Last modified: 2019/01/03 09:50 by admin