From Clomosy Docs

Revision as of 13:25, 3 October 2024 by ClomosyManager (talk | contribs) (Created page with "Conditional statement structures require the programmer to specify one or more conditions that will be evaluated or tested by the program, one or more statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.<br> <div class="table-responsive"> {| class="wikitable" style="border: 2px solid #c3d7e0" ! style="background-color: #c3d7e0"| Statement !!style="background-color...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Conditional statement structures require the programmer to specify one or more conditions that will be evaluated or tested by the program, one or more statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.

Statement Description
if - then statement An if - then statement consists of a boolean expression followed by one or more statements.
If then-else statement An if - then statement can be followed by an optional else statement, which executes when the boolean expression is false.
If then-else if statement It is a structure used to sequentially check multiple conditions.
nested if statements You can use one if or else if statement inside another if or else if statement(s).
case statement A case statement allows a variable to be tested for equality against a list of values.
case - else statement It is similar to the if-then-else statement. Here, an else term follows the case statement.


if-else statement example

TRObject Syntax

var
 I:Integer;
{
 I =3;
 if (I <> 0)
 {
   ShowMessage('The number is not equal to the number 0.');
 }
}

Base Syntax

var
 I:Integer;
begin
 I:=3;
 if I <> 0 then
 begin
   ShowMessage('The number is not equal to the number 0.');
 end;
end;


If-then-else statement example

TRObject Syntax

var
  age: Integer;
{
  age = 28;

  if (age >= 18)
    ShowMessage('You are an adult.')
  else
    ShowMessage('You are a minor.');
}

Base Syntax

var
  age: Integer;
begin
  age := 28;

  if age >= 18 then
    ShowMessage('You are an adult.')
  else
    ShowMessage('You are a minor.');
end;


If then-else if statements example

TRObject Syntax

var
  score: Integer;
{
  score = 85;

  if (score >= 90) 
    ShowMessage('Grade: A')
  else if (score >= 80)
    ShowMessage('Grade: B')
  else if (score >= 70)
    ShowMessage('Grade: C')
  else if (score >= 60)
    ShowMessage('Grade: D')
  else
    ShowMessage('Grade: F');
}

Base Syntax

var
  score: Integer;
begin
  score := 85;

  if score >= 90 then
    ShowMessage('Grade: A')
  else if score >= 80 then
    ShowMessage('Grade: B')
  else if score >= 70 then
    ShowMessage('Grade: C')
  else if score >= 60 then
    ShowMessage('Grade: D')
  else
    ShowMessage('Grade: F');
end;

nested if statements example

TRObject Syntax

var
  age: Integer;
  hasPermission: Boolean;
{
  age = 20;
  hasPermission = True;

  if (age >= 18)
  {
    if (hasPermission)
      ShowMessage('Access granted.')
    else
      ShowMessage('Access denied. Permission required.');
  }
  else
    ShowMessage('You are a minor. Access denied.');
}

Base Syntax

var
  age: Integer;
  hasPermission: Boolean;
begin
  age := 20;
  hasPermission := True;

  if age >= 18 then
  begin
    if hasPermission then
      ShowMessage('Access granted.')
    else
      ShowMessage('Access denied. Permission required.');
  end
  else
    ShowMessage('You are a minor. Access denied.');
end;

case statement example

TRObject Syntax

var
  grade: char;
{
  grade = 'F';
  
  case grade of
  {
    'A': ShowMessage('Excellent!');
    'B': ShowMessage('Good job!');
    'C': ShowMessage('Fair!');
    'D': ShowMessage('Needs improvement!');
    'F': ShowMessage('Failed!');
  }
}

Base Syntax

var
  grade: char;
begin
  grade := 'F';
  
  case grade of
    'A': ShowMessage('Excellent!');
    'B': ShowMessage('Good job!');
    'C': ShowMessage('Fair!');
    'D': ShowMessage('Needs improvement!');
    'F': ShowMessage('Failed!');
  end;
end;

case - else statement example

TRObject Syntax

var
  day: integer;
{
  day = 10;
  
  case day of
  {
    1: ShowMessage('Monday');
    2: ShowMessage('Tuesday');
    3: ShowMessage('Wednesday');
    4: ShowMessage('Thursday');
    5: ShowMessage('Friday');
    6: ShowMessage('Saturday');
    7: ShowMessage('Sunday');
  else
    ShowMessage('Invalid day!');
  }
}

Base Syntax

var
  day: integer;
begin
  day := 10;
  
  case day of
    1: ShowMessage('Monday');
    2: ShowMessage('Tuesday');
    3: ShowMessage('Wednesday');
    4: ShowMessage('Thursday');
    5: ShowMessage('Friday');
    6: ShowMessage('Saturday');
    7: ShowMessage('Sunday');
  else
    ShowMessage('Invalid day!');
  end;
end;