@@ -36,7 +36,7 @@ Start coding immediately with these online compilers:
3636
3737Let's write your first Pascal program. Create a file called ` hello.pas ` :
3838
39- ``` objectpascal {class="highlight capsule-universal "}
39+ ``` objectpascal {class="highlight capsule-fpc "}
4040program HelloWorld;
4141
4242{$mode objfpc}{$H+}{$J-}
@@ -101,30 +101,39 @@ end.
101101Programs need to make choices based on conditions:
102102
103103``` objectpascal {class="highlight capsule-fpc"}
104- program Decisions ;
104+ program Controls ;
105105
106106{$mode objfpc}{$H+}{$J-}
107107
108108var
109- age: integer;
109+ score: integer;
110+ grade: char;
110111begin
111- write('Enter your age : ');
112- readln(age );
112+ write('Enter your score (0-100) : ');
113+ readln(score );
113114
114- if age >= 18 then
115- writeln('You can vote!')
116- else
117- writeln('Too young to vote.');
118-
119- // Multiple conditions
120- if age < 13 then
121- writeln('You are a child')
122- else if age < 20 then
123- writeln('You are a teenager')
115+ // If-then-else statement
116+ if score >= 90 then
117+ grade := 'A'
118+ else if score >= 80 then
119+ grade := 'B'
120+ else if score >= 70 then
121+ grade := 'C'
122+ else if score >= 60 then
123+ grade := 'D'
124124 else
125- writeln('You are an adult') ;
125+ grade := 'F' ;
126126
127- readln;
127+ writeln('Your grade is: ', grade);
128+
129+ // Case statement for multiple choices
130+ case grade of
131+ 'A': writeln('Excellent!');
132+ 'B': writeln('Good job!');
133+ 'C': writeln('Average');
134+ 'D': writeln('Below average');
135+ 'F': writeln('Failed');
136+ end;
128137end.
129138```
130139
@@ -133,39 +142,55 @@ end.
133142Loops let you repeat code multiple times:
134143
135144``` objectpascal {class="highlight capsule-fpc"}
136- program Loops ;
145+ program LoopsDemo ;
137146
138147{$mode objfpc}{$H+}{$J-}
139148
140149var
141- i: integer;
150+ i, sum: integer;
151+ numbers: array[1..5] of integer;
152+ num: integer;
142153begin
143- // Count from 1 to 5
144- writeln('Counting up :');
154+ // For loop - count from 1 to 5
155+ writeln('For loop :');
145156 for i := 1 to 5 do
146157 writeln('Number: ', i);
147158
148- // Count down from 5 to 1
149- writeln('Counting down:');
150- for i := 5 downto 1 do
151- writeln('Number: ', i);
159+ // For-in loop with array
160+ numbers[1] := 10; numbers[2] := 20; numbers[3] := 30;
161+ numbers[4] := 40; numbers[5] := 50;
162+
163+ writeln('For-in loop:');
164+ for num in numbers do
165+ writeln('Value: ', num);
152166
153- // While loop - keep going while condition is true
167+ // While loop - sum numbers until we reach 100
168+ sum := 0;
154169 i := 1;
155- writeln('While loop:');
156- while i <= 3 do
170+ writeln('While loop (sum until >= 100) :');
171+ while sum < 100 do
157172 begin
158- writeln('Step: ', i);
173+ sum := sum + i;
174+ writeln('Adding ', i, ', sum = ', sum);
159175 i := i + 1;
160176 end;
161177
162- readln;
178+ // Repeat-until loop - ask for positive number
179+ writeln('Repeat-until loop:');
180+ repeat
181+ write('Enter a positive number: ');
182+ readln(num);
183+ if num <= 0 then
184+ writeln('Please enter a positive number!');
185+ until num > 0;
186+ writeln('Thank you! You entered: ', num);
163187end.
164188```
165189
166- ---
167190
168191
192+ ---
193+
169194## Practice Time!
170195
171196Let's build some real programs to practice what you've learned.
@@ -323,6 +348,136 @@ begin
323348end.
324349```
325350
351+ ### Arrays - Static and Dynamic
352+
353+ Arrays store multiple values of the same type:
354+
355+ ``` objectpascal {class="highlight capsule-fpc"}
356+ program ArraysDemo;
357+
358+ {$mode objfpc}{$H+}{$J-}
359+
360+ var
361+ // Static array - fixed size
362+ staticNumbers: array[1..5] of integer;
363+
364+ // Dynamic array - size can change
365+ dynamicNumbers: array of integer;
366+
367+ i: integer;
368+ begin
369+ // Working with static arrays
370+ writeln('Static Array:');
371+ staticNumbers[1] := 10;
372+ staticNumbers[2] := 20;
373+ staticNumbers[3] := 30;
374+ staticNumbers[4] := 40;
375+ staticNumbers[5] := 50;
376+
377+ for i := 1 to 5 do
378+ writeln('staticNumbers[', i, '] = ', staticNumbers[i]);
379+
380+ // Working with dynamic arrays
381+ writeln('Dynamic Array:');
382+ SetLength(dynamicNumbers, 3); // Set size to 3 elements
383+
384+ dynamicNumbers[0] := 100; // Dynamic arrays start at index 0
385+ dynamicNumbers[1] := 200;
386+ dynamicNumbers[2] := 300;
387+
388+ for i := 0 to High(dynamicNumbers) do
389+ writeln('dynamicNumbers[', i, '] = ', dynamicNumbers[i]);
390+
391+ // Resize dynamic array
392+ SetLength(dynamicNumbers, 5);
393+ dynamicNumbers[3] := 400;
394+ dynamicNumbers[4] := 500;
395+
396+ writeln('After resize:');
397+ for i := 0 to High(dynamicNumbers) do
398+ writeln('dynamicNumbers[', i, '] = ', dynamicNumbers[i]);
399+ end.
400+ ```
401+
402+ ** Key Points:**
403+
404+ - Use ` SetLength ` to resize dynamic arrays
405+ - Use ` High ` to get the highest index of an array
406+
407+ ### Generics Collections
408+
409+ FPC 3.2.2 includes generic collections for flexible data storage:
410+
411+ ``` objectpascal {class="highlight capsule-fpc"}
412+ program CollectionsDemo;
413+
414+ {$mode objfpc}{$H+}{$J-}
415+
416+ uses
417+ Generics.Collections;
418+
419+ type
420+ TStringList = specialize TList<string>;
421+ TIntList = specialize TList<integer>;
422+ TStrIntDictionary = specialize TDictionary<string, integer>;
423+
424+ var
425+ stringList: TStringList;
426+ numberList: TIntList;
427+ dictionary: TStrIntDictionary;
428+ i: integer;
429+ key: string;
430+ begin
431+ // Generic List of strings
432+ stringList := TStringList.Create;
433+ try
434+ stringList.Add('Apple');
435+ stringList.Add('Banana');
436+ stringList.Add('Cherry');
437+
438+ writeln('String List:');
439+ for i := 0 to stringList.Count - 1 do
440+ writeln(i + 1, '. ', stringList[i]);
441+ finally
442+ stringList.Free;
443+ end;
444+
445+ // Generic List of integers
446+ numberList := TIntList.Create;
447+ try
448+ numberList.Add(10);
449+ numberList.Add(20);
450+ numberList.Add(30);
451+
452+ writeln('Number List:');
453+ for i := 0 to numberList.Count - 1 do
454+ writeln('Item ', i, ': ', numberList[i]);
455+ finally
456+ numberList.Free;
457+ end;
458+
459+ // Dictionary (key-value pairs)
460+ dictionary := TStrIntDictionary.Create;
461+ try
462+ dictionary.Add('Alice', 25);
463+ dictionary.Add('Bob', 30);
464+ dictionary.Add('Charlie', 35);
465+
466+ writeln('Dictionary:');
467+ for key in dictionary.Keys do
468+ writeln(key, ' is ', dictionary[key], ' years old');
469+ finally
470+ dictionary.Free;
471+ end;
472+ end.
473+ ```
474+
475+ ** Key Points:**
476+
477+ - Pair ` try ` /` finally ` to ensure resources are cleaned up
478+ - Create objects using ` Create `
479+ - Use ` Free ` to release resources
480+
326481### Classes - Objects with Behavior
327482
328483Classes combine data and methods (functions) that work on that data:
0 commit comments