4 Function And Procedure
Function And Procedure is a routine that usually used to perform certain tasks or / and to place particular value.
usually when we build a program ( just open and klik form create) in fact we have created procedure thats related with even, example procedure to control program if we klik certain button. this kind of procedure identified by programmer. besides we can create function and procedure, Delphi also provides procedures and functions that are ready to use.
delphi programming
delphi programmers, tutorials
Sunday, June 12, 2011
3.10 try..Finally
3.10 try..Finally
try..finally similar with try..except. just different in writting and try..finally will execute the list of statements , whether there are errors or not.
sintax :
<code>
try
<listofstatements1>;
finally
<listofstatements1>;
end;
</code>
examples try..finally
<code>
Reset(F);
try
...//proces file F
finally
CloseFile(F);
end;
</code>
try..finally similar with try..except. just different in writting and try..finally will execute the list of statements , whether there are errors or not.
sintax :
<code>
try
<listofstatements1>;
finally
<listofstatements1>;
end;
</code>
examples try..finally
<code>
Reset(F);
try
...//proces file F
finally
CloseFile(F);
end;
</code>
3.9 Try..except
3.9 Try..except
Try..Except used if there have possibility ERROR.
Sintax :
<code>
try
<statements_list1>;
except
<statements_list2>;
end;
</code>
operation: the first program will execute one or more statements under try. if not error, program will execute statements until <strong>end;</strong> statements. if any error under <strong>try</strong> statement program will jump and execute the statement below <strong>except</strong>.
attention ! try..except will work if option <strong>stop</strong> on <strong>Delphi Exception</strong> listed on page language exceptions in the menu tools-->debugger Options is not check.
examples 1 try..except
<code>
try
x:=i/j;
except
on EZeroDivide do HandleZeroDivide;
end;
</code>
<hr/>
examples 2 try..except
<hr/>
<code>
try
. . .
except
on EzeroDivide do HandleZeroDivide;
on EOverflow do HandleOverflow;
onEMathError do HAndleMathError;
end;
</code>
<hr/>
examples 3 try..except
<hr/>
<code>
try
. . .
except
on EzeroDivide do HandleZeroDivide;
on EOverflow do HandleOverflow;
onEMathError do HAndleMathError;
else
HandleAllOthers;
end;
</code>
Try..Except used if there have possibility ERROR.
Sintax :
<code>
try
<statements_list1>;
except
<statements_list2>;
end;
</code>
operation: the first program will execute one or more statements under try. if not error, program will execute statements until <strong>end;</strong> statements. if any error under <strong>try</strong> statement program will jump and execute the statement below <strong>except</strong>.
attention ! try..except will work if option <strong>stop</strong> on <strong>Delphi Exception</strong> listed on page language exceptions in the menu tools-->debugger Options is not check.
examples 1 try..except
<code>
try
x:=i/j;
except
on EZeroDivide do HandleZeroDivide;
end;
</code>
<hr/>
examples 2 try..except
<hr/>
<code>
try
. . .
except
on EzeroDivide do HandleZeroDivide;
on EOverflow do HandleOverflow;
onEMathError do HAndleMathError;
end;
</code>
<hr/>
examples 3 try..except
<hr/>
<code>
try
. . .
except
on EzeroDivide do HandleZeroDivide;
on EOverflow do HandleOverflow;
onEMathError do HAndleMathError;
else
HandleAllOthers;
end;
</code>
3.8 Exit statements
3.8 Exit statements
The Exit statements abruptly terminates the current function or procedure.
If exiting a function, then Result contains the last set value.
Warning : use with caution - jumping is a concept at odds with structured coding - it makes code maintenance difficult.
examples :
Result := name1 + ' ' + name2;
end;
The Exit statements abruptly terminates the current function or procedure.
If exiting a function, then Result contains the last set value.
Warning : use with caution - jumping is a concept at odds with structured coding - it makes code maintenance difficult.
examples :
var
name1, name2 : string;
begin
Result := 'Tanjung Kona';
repeat
if not InputQuery('Test program', 'First name :', name1)
then Exit;
if not InputQuery('Test program', 'Second name :', name2)
then Exit;
until (name <> '') or (name2 <> '');Result := name1 + ' ' + name2;
end;
3.7 Break and Continue statements
3.7 Break and Continue statements
Break
The Break procedure forces a jump out of the setof statements within a loop. Like the Goto statement, it should be used with caution.
The next statement that is executed is the one following the loop terminator. For example:
It is important to note that the Break statement only jumps out of the current loop - not out of any nested loops above it. The Goto statement can.
Continue
Passes control to the next iteration of the enclosing iteration statement in which it appears.
The enclosing iteration assumes for, while, or repeat statements.
examples :
Break
The Break procedure forces a jump out of the setof statements within a loop. Like the Goto statement, it should be used with caution.
The next statement that is executed is the one following the loop terminator. For example:
for i := 1 to 10 do
begin
...
break;
...
end;
size := 10; // breaks to here
It is important to note that the Break statement only jumps out of the current loop - not out of any nested loops above it. The Goto statement can.
Continue
Passes control to the next iteration of the enclosing iteration statement in which it appears.
The enclosing iteration assumes for, while, or repeat statements.
examples :
for i = 0 to 100 do
begin
if i MOD 2 = 0 then
Continue;
end;
3.6 Go to
3.6 Go to
A Goto statement transfers program execution to the statement marked by the specified label.
Read this again! Goto does not "ask questions", it simply forces a jump to some other location in the code block. When badly used Goto could transform your code into a non-readable piece of junk.
According to the "Are you using the Goto statement in Delphi" poll, it seems 40% of Delphi developers do not even know Goto exists in Delphi. Another 40% would have a very low opinion on you if they would see Goto in your Delphi code.
Be it bad or good, Goto is in Delphi and you might need to use it, or at least need to understand how it works.
Here's a code block that might produce some very nasty, unpredictable effects:
var
label : skipOut;
label : breakCodeFlow;
j,k : integer;
begin
breakCodeFlow:
for j := 0 to 10 do
begin
for k := 0 to 10 do
begin
if Something_Related_To_J_and_K then
begin
GOTO skipOut;
end;
end;
end;
skipOut:
//continue program execution here...
if j = 0 then
begin
//some code block
GOTO breakCodeFlow;
end;
A goto statement needs a label ("skipOut" and "breakCodeFlow") to be defined in the var section.
Luckily, even though Goto can be used for bad ideas, there are some restrictions in using Goto:
* You can "Goto" only inside the same scope block.
* You cannot "Goto" out of a function of procedure.
* You cannot jump into or out of a try/finally or try/except block.
Note that if you need, for example, to skip out of a "for loop" you should be using the Break statement.
(http://delphi.about.com)
A Goto statement transfers program execution to the statement marked by the specified label.
Read this again! Goto does not "ask questions", it simply forces a jump to some other location in the code block. When badly used Goto could transform your code into a non-readable piece of junk.
According to the "Are you using the Goto statement in Delphi" poll, it seems 40% of Delphi developers do not even know Goto exists in Delphi. Another 40% would have a very low opinion on you if they would see Goto in your Delphi code.
Be it bad or good, Goto is in Delphi and you might need to use it, or at least need to understand how it works.
Here's a code block that might produce some very nasty, unpredictable effects:
var
label : skipOut;
label : breakCodeFlow;
j,k : integer;
begin
breakCodeFlow:
for j := 0 to 10 do
begin
for k := 0 to 10 do
begin
if Something_Related_To_J_and_K then
begin
GOTO skipOut;
end;
end;
end;
skipOut:
//continue program execution here...
if j = 0 then
begin
//some code block
GOTO breakCodeFlow;
end;
A goto statement needs a label ("skipOut" and "breakCodeFlow") to be defined in the var section.
Luckily, even though Goto can be used for bad ideas, there are some restrictions in using Goto:
* You can "Goto" only inside the same scope block.
* You cannot "Goto" out of a function of procedure.
* You cannot jump into or out of a try/finally or try/except block.
Note that if you need, for example, to skip out of a "for loop" you should be using the Break statement.
(http://delphi.about.com)
3.5 Case
3.5 Case
Although, we can use the if statement for very complex (nested) condition testing, the case statement is usually easier to read (debug!) and the code runs more quickly.
The case statement makes it clear that a program has reached a point with many branches; multiple if-then statements do not.
Sintax :
examples :
What follows the case keyword is usually called the selector. The selector is a variable or expression taken from either the char type or any integer type (an ordinal type). String type are invalid!. However, the StringToCaseSelect custom function enables you to use the Case statement with string type variables
As you can see, the individual case statements use a single constant, a group of constants (separated by comma), or a range of constants (double dot separated). We can even add an else keyword to take care of all the remaining cases at once.
Note 1: Only one case statement will be executed, we cannot have overlapping conditions in the case statements.
Note 2: If you want to include more than one statement in the part following the colon (:), place the begin and end keywords around the multiple statements.
examples case statements usage
Although, we can use the if statement for very complex (nested) condition testing, the case statement is usually easier to read (debug!) and the code runs more quickly.
The case statement makes it clear that a program has reached a point with many branches; multiple if-then statements do not.
Sintax :
Case <parameters> of
<number_1>:<execute_1>
. . .
. . .
. . .
<number_n>:<execute_n>
else < execute_n+1 >;examples :
begin
case days of
1 : 'Monday';
2 : 'Tuesday';
3 : 'Wednesday';
4 : 'Thursday';
5 : 'Friday';
6 : 'Saturday';
7 : 'Sunday';
end;
end;
What follows the case keyword is usually called the selector. The selector is a variable or expression taken from either the char type or any integer type (an ordinal type). String type are invalid!. However, the StringToCaseSelect custom function enables you to use the Case statement with string type variables
As you can see, the individual case statements use a single constant, a group of constants (separated by comma), or a range of constants (double dot separated). We can even add an else keyword to take care of all the remaining cases at once.
Note 1: Only one case statement will be executed, we cannot have overlapping conditions in the case statements.
Note 2: If you want to include more than one statement in the part following the colon (:), place the begin and end keywords around the multiple statements.
examples case statements usage
Subscribe to:
Comments (Atom)