Recent

Author Topic: If Else statement  (Read 5016 times)

maryg

  • New Member
  • *
  • Posts: 13
  • avid technician
If Else statement
« on: October 17, 2018, 08:53:27 am »
Hi Guys,
I'm working on a student progress analysis system and I'm trying to set it up in such a way that the scores per subject are linked to the school's grading statement i.e if the student scores 80% then the grade will show 'A' in the report.
The system is being used by a Secondary School, so the different subject clusters(Languages, Humanities, Sciences) have somewhat different grading systems.
Please any ideas on such a code?

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: If Else statement
« Reply #1 on: October 17, 2018, 09:11:12 am »
I personally think the calculation should not be hard coded into the source. You can use a config file (or maybe database) to store what and how each items will affect the formula. Then you write a CalculateGrading function, which contains lots of if-then-else to calculate the result based on the data read from the file.
« Last Edit: October 17, 2018, 09:13:38 am by Handoko »

HeavyUser

  • Sr. Member
  • ****
  • Posts: 397
Re: If Else statement
« Reply #2 on: October 17, 2018, 09:18:09 am »
Hi Guys,
I'm working on a student progress analysis system and I'm trying to set it up in such a way that the scores per subject are linked to the school's grading statement i.e if the student scores 80% then the grade will show 'A' in the report.
The system is being used by a Secondary School, so the different subject clusters(Languages, Humanities, Sciences) have somewhat different grading systems.
Please any ideas on such a code?
how many grades are they equally spread across the scale? if not what are the rules for each grade on the scale per department? for a 5 grade system equally spaced on the 100 scale for example you can do something along the lines of
Code: [Select]
Type
  TGrade = (E,D,C,B,A);
const
  cStep = 100 / 5;
function Grade(const aScore:integer):TGrade;
begin
   result := TGrade(floor(ascore / cstep));
end;
treat it as a pseudo code it was typed directly on the browser and never been compiled or tested in any way.

maryg

  • New Member
  • *
  • Posts: 13
  • avid technician
Re: If Else statement
« Reply #3 on: October 17, 2018, 09:38:45 am »
The grading system is on a 100% scale with intervals e.g 100-75 'A', 70-74 'A-', 65-69 'B+',... but this differs in the different departments.

madref

  • Hero Member
  • *****
  • Posts: 949
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: If Else statement
« Reply #4 on: October 17, 2018, 11:32:10 am »
Then make it a case-statement
Code: [Select]
case score of
  100..75: Grade := 'A';
  74..70: Grade := 'A-';
...
...
  else
    Grade := 'You failed miserably !!';
end;  // case;


You can put this in different function with a name like
Code: [Select]
function GradeForDepartmantX (Score: Integer) : String;
function GradeForDepartmantY (Score: Integer) : String;
function GradeForDepartmantZ (Score: Integer) : String;
And then you can make another case with the different department like
Code: [Select]
case Department of
  'X' : Grade :=  GradeForDepartmantX;
  'Y' : Grade :=  GradeForDepartmantY;
  'Z' : Grade :=  GradeForDepartmantZ;
end;  // case
« Last Edit: October 17, 2018, 11:37:07 am by madref »
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus 3.99 (rev main_3_99-649-ge13451a5ab) FPC 3.3.1 x86_64-darwin-cocoa
Mac OS X Monterey

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: If Else statement
« Reply #5 on: October 17, 2018, 12:10:29 pm »
What I like about programming is there are many different ways to solve the same problem. Here is how I will do for this case:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     ComboBox1: TComboBox;
  17.     Edit1: TEdit;
  18.     Label1: TLabel;
  19.     procedure Button1Click(Sender: TObject);
  20.     procedure FormCreate(Sender: TObject);
  21.     procedure FormDestroy(Sender: TObject);
  22.   private
  23.     GradeSystem: TStringList;
  24.     procedure ShowGrade(Score: Integer);
  25.   end;
  26.  
  27. const
  28.   GradeSettingFile = 'GradeSettings.txt';
  29.  
  30. var
  31.   Form1: TForm1;
  32.  
  33. implementation
  34.  
  35. {$R *.lfm}
  36.  
  37. { TForm1 }
  38.  
  39. procedure TForm1.FormCreate(Sender: TObject);
  40. var
  41.   SL : TStringList;
  42.   i  : Integer;
  43. begin
  44.  
  45.   GradeSystem := TStringList.Create;
  46.   GradeSystem.LoadFromFile(GradeSettingFile);
  47.  
  48.   // Show school subject names
  49.   SL := TStringList.Create;
  50.   SL.Delimiter := ' ';
  51.   for i := 0 to (GradeSystem.Count-1) do
  52.   begin
  53.     SL.DelimitedText := GradeSystem[i];
  54.     ComboBox1.AddItem(SL[0], nil);
  55.   end;
  56.   SL.Free;
  57.  
  58.   if (GradeSystem.Count > 0) then
  59.     ComboBox1.ItemIndex := 0;
  60.  
  61. end;
  62.  
  63. procedure TForm1.FormDestroy(Sender: TObject);
  64. begin
  65.   GradeSystem.Free;
  66. end;
  67.  
  68. procedure TForm1.ShowGrade(Score: Integer);
  69. var
  70.   SL    : TStringList;
  71.   Grade : string;
  72.   Value : Integer;
  73.   i     : Integer;
  74. begin
  75.  
  76.   SL := TStringList.Create;
  77.   SL.Delimiter := ' ';
  78.   SL.DelimitedText := GradeSystem[ComboBox1.ItemIndex];
  79.  
  80.   Grade := '';
  81.   i     := 2;
  82.   while i <= SL.Count do
  83.   begin
  84.     if not(TryStrToInt(SL[i], Value)) then
  85.     begin
  86.       ShowMessage('Error in ' + GradeSettingFile + '.');
  87.       Halt;
  88.     end;
  89.     if Score > Value then
  90.       Grade := SL[i-1];
  91.     if Score < Value then
  92.       Break;
  93.     Inc(i, 2);
  94.   end;
  95.  
  96.   SL.Free;
  97.  
  98.   ShowMessage('Well done stundent.' + LineEnding +
  99.               'Your grade is ' + Grade + '.');
  100.  
  101. end;
  102.  
  103. procedure TForm1.Button1Click(Sender: TObject);
  104. var
  105.   Score: Integer;
  106. begin
  107.  
  108.   if (GradeSystem.Count <= 0) then
  109.   begin
  110.     ShowMessage('Unable to load grading system.');
  111.     Exit;
  112.   end;
  113.  
  114.   if not(TryStrToInt(Edit1.Caption, Score)) then
  115.   begin
  116.     ShowMessage('Please provide a valid value.');
  117.     Exit;
  118.   end;
  119.  
  120.   if (Score < 0) or (Score > 100) then
  121.   begin
  122.     ShowMessage('0 <= Score <= 100');
  123.     Exit;
  124.   end;
  125.  
  126.   ShowGrade(Score);
  127.  
  128. end;
  129.  
  130. end.

And this is the content of GradeSetting.txt:

Math F 0 E 30 D 50 C 60 B 65 A- 70 A 75
English F 0 E 30 D 50 C 60 B 65 A- 70 A 75
Spanish F 0 E 30 D 50 C 60 B 65 A- 70 A 75
Biology F 0 E 30 D 50 C 60 B 70 A- 80 A 90
Physics F 0 E 30 D 50 C 60 B 65 A- 70 A 75
Music D 0 C 55 B 70 A 85


Oops, I've just noticed I typed an extra n for student.
« Last Edit: October 17, 2018, 12:18:46 pm by Handoko »

maryg

  • New Member
  • *
  • Posts: 13
  • avid technician
Re: If Else statement
« Reply #6 on: October 17, 2018, 01:17:51 pm »
What I like about programming is there are many different ways to solve the same problem. Here is how I will do for this case:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     ComboBox1: TComboBox;
  17.     Edit1: TEdit;
  18.     Label1: TLabel;
  19.     procedure Button1Click(Sender: TObject);
  20.     procedure FormCreate(Sender: TObject);
  21.     procedure FormDestroy(Sender: TObject);
  22.   private
  23.     GradeSystem: TStringList;
  24.     procedure ShowGrade(Score: Integer);
  25.   end;
  26.  
  27. const
  28.   GradeSettingFile = 'GradeSettings.txt';
  29.  
  30. var
  31.   Form1: TForm1;
  32.  
  33. implementation
  34.  
  35. {$R *.lfm}
  36.  
  37. { TForm1 }
  38.  
  39. procedure TForm1.FormCreate(Sender: TObject);
  40. var
  41.   SL : TStringList;
  42.   i  : Integer;
  43. begin
  44.  
  45.   GradeSystem := TStringList.Create;
  46.   GradeSystem.LoadFromFile(GradeSettingFile);
  47.  
  48.   // Show school subject names
  49.   SL := TStringList.Create;
  50.   SL.Delimiter := ' ';
  51.   for i := 0 to (GradeSystem.Count-1) do
  52.   begin
  53.     SL.DelimitedText := GradeSystem[i];
  54.     ComboBox1.AddItem(SL[0], nil);
  55.   end;
  56.   SL.Free;
  57.  
  58.   if (GradeSystem.Count > 0) then
  59.     ComboBox1.ItemIndex := 0;
  60.  
  61. end;
  62.  
  63. procedure TForm1.FormDestroy(Sender: TObject);
  64. begin
  65.   GradeSystem.Free;
  66. end;
  67.  
  68. procedure TForm1.ShowGrade(Score: Integer);
  69. var
  70.   SL    : TStringList;
  71.   Grade : string;
  72.   Value : Integer;
  73.   i     : Integer;
  74. begin
  75.  
  76.   SL := TStringList.Create;
  77.   SL.Delimiter := ' ';
  78.   SL.DelimitedText := GradeSystem[ComboBox1.ItemIndex];
  79.  
  80.   Grade := '';
  81.   i     := 2;
  82.   while i <= SL.Count do
  83.   begin
  84.     if not(TryStrToInt(SL[i], Value)) then
  85.     begin
  86.       ShowMessage('Error in ' + GradeSettingFile + '.');
  87.       Halt;
  88.     end;
  89.     if Score > Value then
  90.       Grade := SL[i-1];
  91.     if Score < Value then
  92.       Break;
  93.     Inc(i, 2);
  94.   end;
  95.  
  96.   SL.Free;
  97.  
  98.   ShowMessage('Well done stundent.' + LineEnding +
  99.               'Your grade is ' + Grade + '.');
  100.  
  101. end;
  102.  
  103. procedure TForm1.Button1Click(Sender: TObject);
  104. var
  105.   Score: Integer;
  106. begin
  107.  
  108.   if (GradeSystem.Count <= 0) then
  109.   begin
  110.     ShowMessage('Unable to load grading system.');
  111.     Exit;
  112.   end;
  113.  
  114.   if not(TryStrToInt(Edit1.Caption, Score)) then
  115.   begin
  116.     ShowMessage('Please provide a valid value.');
  117.     Exit;
  118.   end;
  119.  
  120.   if (Score < 0) or (Score > 100) then
  121.   begin
  122.     ShowMessage('0 <= Score <= 100');
  123.     Exit;
  124.   end;
  125.  
  126.   ShowGrade(Score);
  127.  
  128. end;
  129.  
  130. end.

And this is the content of GradeSetting.txt:

Math F 0 E 30 D 50 C 60 B 65 A- 70 A 75
English F 0 E 30 D 50 C 60 B 65 A- 70 A 75
Spanish F 0 E 30 D 50 C 60 B 65 A- 70 A 75
Biology F 0 E 30 D 50 C 60 B 70 A- 80 A 90
Physics F 0 E 30 D 50 C 60 B 65 A- 70 A 75
Music D 0 C 55 B 70 A 85


Oops, I've just noticed I typed an extra n for student.

Thanks  :D

maryg

  • New Member
  • *
  • Posts: 13
  • avid technician
Re: If Else statement
« Reply #7 on: October 17, 2018, 01:19:26 pm »
Then make it a case-statement
Code: [Select]
case score of
  100..75: Grade := 'A';
  74..70: Grade := 'A-';
...
...
  else
    Grade := 'You failed miserably !!';
end;  // case;


You can put this in different function with a name like
Code: [Select]
function GradeForDepartmantX (Score: Integer) : String;
function GradeForDepartmantY (Score: Integer) : String;
function GradeForDepartmantZ (Score: Integer) : String;
And then you can make another case with the different department like
Code: [Select]
case Department of
  'X' : Grade :=  GradeForDepartmantX;
  'Y' : Grade :=  GradeForDepartmantY;
  'Z' : Grade :=  GradeForDepartmantZ;
end;  // case

Thanks  :D

maryg

  • New Member
  • *
  • Posts: 13
  • avid technician
Re: If Else statement
« Reply #8 on: October 21, 2018, 09:44:35 am »
What I like about programming is there are many different ways to solve the same problem. Here is how I will do for this case:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     ComboBox1: TComboBox;
  17.     Edit1: TEdit;
  18.     Label1: TLabel;
  19.     procedure Button1Click(Sender: TObject);
  20.     procedure FormCreate(Sender: TObject);
  21.     procedure FormDestroy(Sender: TObject);
  22.   private
  23.     GradeSystem: TStringList;
  24.     procedure ShowGrade(Score: Integer);
  25.   end;
  26.  
  27. const
  28.   GradeSettingFile = 'GradeSettings.txt';
  29.  
  30. var
  31.   Form1: TForm1;
  32.  
  33. implementation
  34.  
  35. {$R *.lfm}
  36.  
  37. { TForm1 }
  38.  
  39. procedure TForm1.FormCreate(Sender: TObject);
  40. var
  41.   SL : TStringList;
  42.   i  : Integer;
  43. begin
  44.  
  45.   GradeSystem := TStringList.Create;
  46.   GradeSystem.LoadFromFile(GradeSettingFile);
  47.  
  48.   // Show school subject names
  49.   SL := TStringList.Create;
  50.   SL.Delimiter := ' ';
  51.   for i := 0 to (GradeSystem.Count-1) do
  52.   begin
  53.     SL.DelimitedText := GradeSystem[i];
  54.     ComboBox1.AddItem(SL[0], nil);
  55.   end;
  56.   SL.Free;
  57.  
  58.   if (GradeSystem.Count > 0) then
  59.     ComboBox1.ItemIndex := 0;
  60.  
  61. end;
  62.  
  63. procedure TForm1.FormDestroy(Sender: TObject);
  64. begin
  65.   GradeSystem.Free;
  66. end;
  67.  
  68. procedure TForm1.ShowGrade(Score: Integer);
  69. var
  70.   SL    : TStringList;
  71.   Grade : string;
  72.   Value : Integer;
  73.   i     : Integer;
  74. begin
  75.  
  76.   SL := TStringList.Create;
  77.   SL.Delimiter := ' ';
  78.   SL.DelimitedText := GradeSystem[ComboBox1.ItemIndex];
  79.  
  80.   Grade := '';
  81.   i     := 2;
  82.   while i <= SL.Count do
  83.   begin
  84.     if not(TryStrToInt(SL[i], Value)) then
  85.     begin
  86.       ShowMessage('Error in ' + GradeSettingFile + '.');
  87.       Halt;
  88.     end;
  89.     if Score > Value then
  90.       Grade := SL[i-1];
  91.     if Score < Value then
  92.       Break;
  93.     Inc(i, 2);
  94.   end;
  95.  
  96.   SL.Free;
  97.  
  98.   ShowMessage('Well done stundent.' + LineEnding +
  99.               'Your grade is ' + Grade + '.');
  100.  
  101. end;
  102.  
  103. procedure TForm1.Button1Click(Sender: TObject);
  104. var
  105.   Score: Integer;
  106. begin
  107.  
  108.   if (GradeSystem.Count <= 0) then
  109.   begin
  110.     ShowMessage('Unable to load grading system.');
  111.     Exit;
  112.   end;
  113.  
  114.   if not(TryStrToInt(Edit1.Caption, Score)) then
  115.   begin
  116.     ShowMessage('Please provide a valid value.');
  117.     Exit;
  118.   end;
  119.  
  120.   if (Score < 0) or (Score > 100) then
  121.   begin
  122.     ShowMessage('0 <= Score <= 100');
  123.     Exit;
  124.   end;
  125.  
  126.   ShowGrade(Score);
  127.  
  128. end;
  129.  
  130. end.

And this is the content of GradeSetting.txt:

Math F 0 E 30 D 50 C 60 B 65 A- 70 A 75
English F 0 E 30 D 50 C 60 B 65 A- 70 A 75
Spanish F 0 E 30 D 50 C 60 B 65 A- 70 A 75
Biology F 0 E 30 D 50 C 60 B 70 A- 80 A 90
Physics F 0 E 30 D 50 C 60 B 65 A- 70 A 75
Music D 0 C 55 B 70 A 85


Oops, I've just noticed I typed an extra n for student.

I tried this instead, but it only shows the 'A' grade after the calculations. I my range wrong?  :-[
procedure Tfrmstdtperf.btngradeClick(Sender: TObject);
var
  no1c, no2c: double;
begin
 no1c := StrToFloat(CATmks.Text);
  no2c := StrToFloat(Marks.Text);
  GrTotal.Text := FloatToStr(no1c+no2c);
  Points.Text := FloatToStr(RoundTo(StrToFloat(Marks.Text),1) / 12);
 if (no2c>=880) or (no2c<=1100)) then Grade0.Caption:='A'
  else if (no2c>=825) or (no2c<=879) then Grade0.Caption:='A-'
  else if (no2c>=770) or (no2c<=824) then Grade0.Caption:='B+'
  else if (no2c>=715) or (no2c<=769) then Grade0.Caption:='B'
  else if (no2c>=660) or (no2c<=714) then Grade0.Caption:='B-'
  else if (no2c>=605) or (no2c<=659) then Grade0.Caption:='C+'
  else if (no2c>=550) or (no2c<=604) then Grade0.Caption:='C'
  else if (no2c>=495) or (no2c<=549) then Grade0.Caption:='C-'
  else if (no2c>=440) or (no2c<=494) then Grade0.Caption:='D+'
  else if (no2c>=385) or (no2c<=439) then Grade0.Caption:='D'
  else if (no2c>=330) or (no2c<=384) then Grade0.Caption:='D-'
  else if (no2c>=0) or (no2c<=329) then Grade0.Caption:='E'
end;       

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: If Else statement
« Reply #9 on: October 21, 2018, 10:35:51 am »
You can solve this by fuzzy logic.
Here's a unit for it using Zadeh's logic.
You can subsequntly perform logic operators on the scores.
This allows for all kinds of limits as well: so only a grade when a certain score for a certain subject
Code: Pascal  [Select][+][-]
  1. unit uzadehlogic;
  2. {$mode objfpc}
  3. { Zadeh standard fuzzy logic operators
  4.  See:
  5.    https://commons.wikimedia.org/wiki/Fuzzy_operator
  6.  Enjoy,
  7.  
  8.  Thaddy }
  9.  
  10. interface
  11. type
  12.   { Fuzz is a double in the inclusive range 0..1
  13.     The programmer is responsible for valid input.
  14.     (You can use EnsureRange from the math unit.) }
  15.   Fuzz = type double;  
  16.  
  17.   // AND
  18.   operator and (const a,b:Fuzz):Fuzz;inline;
  19.  
  20.   // OR
  21.   operator or (const a,b:Fuzz):Fuzz;inline;
  22.  
  23.   // NOT
  24.   operator not (const a:Fuzz):Fuzz;inline;
  25.    
  26.   // XOR
  27.   operator xor (const a,b:Fuzz):Fuzz;inline;
  28.  
  29. {
  30. * The following logic implemented as functions
  31. * because Pascal doesn't know them as operators
  32. }  
  33.   // IMP
  34.   function imp(const a,b:Fuzz):Fuzz;inline;
  35.  
  36.   // NAND
  37.   function nand(const a,b:Fuzz):Fuzz;inline;
  38.  
  39.   // NOR
  40.   function nor(const a,b:Fuzz):Fuzz;inline;
  41.  
  42.   // NXR
  43.   function nxr(const a,b:Fuzz):Fuzz;inline;
  44.  
  45.   // NIMP
  46.   function nimp(const a,b:Fuzz):Fuzz;inline;
  47.  
  48.  
  49. { Utilities }
  50.   function min(const a,b:Fuzz):Fuzz;
  51.  
  52.   function max(const a,b:Fuzz):Fuzz;
  53.   // to prevent overshoot. Not strictly necessary  
  54.   function Limit(const a:Double):Fuzz;
  55.  
  56. implementation
  57.  
  58.   function min(const a,b:Fuzz):Fuzz;
  59.   begin    
  60.     if a > b then result := b else result := a;
  61.   end;
  62.  
  63.   function max(const a,b:Fuzz):Fuzz;
  64.   begin    
  65.     if b > a then result := b else result := a;
  66.   end;
  67.    
  68.   function Limit(const a:Double):Fuzz;
  69.   begin
  70.     Result := min(1,max(0,a));
  71.   end;
  72.  
  73.   // AND
  74.   operator and (const a,b:Fuzz):Fuzz;inline;
  75.   begin
  76.     Result:=min(limit(a),Limit(b));
  77.   end;
  78.  
  79.   // OR
  80.   operator or (const a,b:Fuzz):Fuzz;inline;
  81.   begin
  82.     Result:=max(Limit(a),limit(b));
  83.   end;
  84.  
  85.   // NOT
  86.   operator not (const a:Fuzz):Fuzz;inline;
  87.   begin
  88.     result := 1 - Limit(a);
  89.   end;
  90.  
  91.   // XOR
  92.   operator xor (const a,b:Fuzz):Fuzz;inline;
  93.   begin
  94.     result := limit(a) + limit(b) - 2 * (a and b);
  95.   end;
  96.  
  97.   // IMP
  98.   function imp(const a,b:Fuzz):Fuzz;inline;
  99.   begin
  100.     Result := not (a and not b)
  101.   end;  
  102.  
  103.   // NAND
  104.   function nand(const a,b:Fuzz):Fuzz;inline;
  105.   begin
  106.     Result := not (a and b);
  107.   end;
  108.  
  109.   // NOR
  110.   function nor(const a,b:Fuzz):Fuzz;inline;
  111.   begin
  112.     Result := not (a or b);
  113.   end;
  114.  
  115.   // NXR
  116.   function nxr(const a,b:Fuzz):Fuzz;inline;
  117.   begin
  118.     Result := not (a xor b);
  119.   end;
  120.  
  121.   // NIMP
  122.   function nimp(const a,b:Fuzz):Fuzz;inline;
  123.   begin
  124.     Result := a and not b;
  125.   end;  
  126. end.
« Last Edit: October 21, 2018, 10:57:46 am by Thaddy »
Specialize a type, not a var.

HeavyUser

  • Sr. Member
  • ****
  • Posts: 397
Re: If Else statement
« Reply #10 on: October 21, 2018, 11:13:09 am »
I tried this instead, but it only shows the 'A' grade after the calculations. I my range wrong?  :-[
procedure Tfrmstdtperf.btngradeClick(Sender: TObject);
var
  no1c, no2c: double;
begin
 no1c := StrToFloat(CATmks.Text);
  no2c := StrToFloat(Marks.Text);
  GrTotal.Text := FloatToStr(no1c+no2c);
  Points.Text := FloatToStr(RoundTo(StrToFloat(Marks.Text),1) / 12);
 if (no2c>=880) or (no2c<=1100)) then Grade0.Caption:='A'
  else if (no2c>=825) or (no2c<=879) then Grade0.Caption:='A-'
  else if (no2c>=770) or (no2c<=824) then Grade0.Caption:='B+'
  else if (no2c>=715) or (no2c<=769) then Grade0.Caption:='B'
  else if (no2c>=660) or (no2c<=714) then Grade0.Caption:='B-'
  else if (no2c>=605) or (no2c<=659) then Grade0.Caption:='C+'
  else if (no2c>=550) or (no2c<=604) then Grade0.Caption:='C'
  else if (no2c>=495) or (no2c<=549) then Grade0.Caption:='C-'
  else if (no2c>=440) or (no2c<=494) then Grade0.Caption:='D+'
  else if (no2c>=385) or (no2c<=439) then Grade0.Caption:='D'
  else if (no2c>=330) or (no2c<=384) then Grade0.Caption:='D-'
  else if (no2c>=0) or (no2c<=329) then Grade0.Caption:='E'
end;     
yes your ifs are wrong. for example the first if "(no2c>=880) or(no2c<=1100)", what do you expect to happen for a grade of 2? should it be false or true? As it is now it will be true. How about a grade of 8000000? it is still true. All numbers are either grater than 880 or smaller than 1100. I assume you try to say, the number is between 880 and 1100 inclusive, in which case you need to change to "and" instead of "or".

maryg

  • New Member
  • *
  • Posts: 13
  • avid technician
Re: If Else statement
« Reply #11 on: October 22, 2018, 10:40:46 am »
I tried this instead, but it only shows the 'A' grade after the calculations. I my range wrong?  :-[
procedure Tfrmstdtperf.btngradeClick(Sender: TObject);
var
  no1c, no2c: double;
begin
 no1c := StrToFloat(CATmks.Text);
  no2c := StrToFloat(Marks.Text);
  GrTotal.Text := FloatToStr(no1c+no2c);
  Points.Text := FloatToStr(RoundTo(StrToFloat(Marks.Text),1) / 12);
 if (no2c>=880) or (no2c<=1100)) then Grade0.Caption:='A'
  else if (no2c>=825) or (no2c<=879) then Grade0.Caption:='A-'
  else if (no2c>=770) or (no2c<=824) then Grade0.Caption:='B+'
  else if (no2c>=715) or (no2c<=769) then Grade0.Caption:='B'
  else if (no2c>=660) or (no2c<=714) then Grade0.Caption:='B-'
  else if (no2c>=605) or (no2c<=659) then Grade0.Caption:='C+'
  else if (no2c>=550) or (no2c<=604) then Grade0.Caption:='C'
  else if (no2c>=495) or (no2c<=549) then Grade0.Caption:='C-'
  else if (no2c>=440) or (no2c<=494) then Grade0.Caption:='D+'
  else if (no2c>=385) or (no2c<=439) then Grade0.Caption:='D'
  else if (no2c>=330) or (no2c<=384) then Grade0.Caption:='D-'
  else if (no2c>=0) or (no2c<=329) then Grade0.Caption:='E'
end;     
yes your ifs are wrong. for example the first if "(no2c>=880) or(no2c<=1100)", what do you expect to happen for a grade of 2? should it be false or true? As it is now it will be true. How about a grade of 8000000? it is still true. All numbers are either grater than 880 or smaller than 1100. I assume you try to say, the number is between 880 and 1100 inclusive, in which case you need to change to "and" instead of "or".

Yees!! It works  :D  :D
Thanks HeavyUser

bytebites

  • Hero Member
  • *****
  • Posts: 633
Re: If Else statement
« Reply #12 on: October 22, 2018, 02:44:31 pm »
Code: Pascal  [Select][+][-]
  1.  else if (no2c>=825) and (no2c<=879) then Grade0.Caption:='A-'

Besides the second comparison is unnecessary because no2c for sure is less than 880.

madref

  • Hero Member
  • *****
  • Posts: 949
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: If Else statement
« Reply #13 on: October 22, 2018, 03:30:08 pm »
Why don't you make a case-statement out of it...
That is much easier
Code: Pascal  [Select][+][-]
  1. Case no2c of
  2.   1100..880 : Grade0.Caption := 'A';
  3.    879..825 : Grade0.Caption := 'A-';
  4.    824..770 : Grade0.Caption := 'B+';
  5. etc...
  6. etc..
  7.  
  8.  
  9. end;  // case
  10.  


And it's much easier to change and it's more readable
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus 3.99 (rev main_3_99-649-ge13451a5ab) FPC 3.3.1 x86_64-darwin-cocoa
Mac OS X Monterey

 

TinyPortal © 2005-2018