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;


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.1545833444.txt.gz · Last modified: 2018/12/26 21:10 by admin