943,892 Members | Top Members by Rank

Ad:
Jul 4th, 2009
0

May I ask you a question on Delphi?

Expand Post »
Code a decision using an IF, THEN, ELSE statement (2 branch decision)
Code a one branch decision using a null else statement



The following is my answer:

Pascal and Delphi Syntax (Toggle Plain Text)
  1.  
  2. program Negatives;
  3.  
  4. {$APPTYPE CONSOLE}
  5.  
  6. uses
  7. SysUtils,
  8. ourcrt;
  9.  
  10. var
  11. balance, posNeg:integer;
  12.  
  13. begin
  14. randomize;
  15. writeln ('Generating your bank balance');
  16. writeln ('Please wait .....');
  17. sleep (2000);
  18. //create a random number between 0 and 1000
  19. balance:=random(1000);
  20. //randomly decide if the balance is negative
  21. //1 for positive, 0 for negative
  22. posNeg:=random(2);
  23. if posNeg=0 then
  24. begin
  25. balance:=balance*-1;
  26. writeln('Can you hear that sound? Its your cheque bouncing');
  27. sound(200,1000);
  28. end
  29. else
  30. begin
  31. writeln ('Positive balance');
  32. end;
  33. writeln('Your balance is ', balance);
  34. sleep(5000);
  35.  
  36. end.

Could you tell me why at the Line 22 & 25, we do not want to put full-stop / semi-colon after the 'end'? It makes me very confused about the punctuation marks.
Attached Images
File Type: bmp untitled.bmp (595.6 KB, 20 views)
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster
turbomen is offline Offline
113 posts
since Feb 2009
Jul 4th, 2009
1

Re: May I ask you a question on Delphi?

your code is working well,where is the problem?????
you can write here too:
if posNeg=0 then
begin
balance:=balance*-1;
writeln('Can you hear that sound? Its your cheque bouncing');
sound(200,1000);
end;
If NegPos = 1 Then writeln ('Positive balance');

In your example if you put a semicolon; after your If statement's end,then the compiler thinks that it is a short version of the if statement,and send you an error about the else part,try it.....';' Not allowed before 'ELSE'....now you got it?.....
Reputation Points: 132
Solved Threads: 138
Posting Pro
FlamingClaw is offline Offline
559 posts
since Feb 2009
Jul 5th, 2009
0

Re: May I ask you a question on Delphi?

I have got a problem on the punctuation mark and what is the meaning of the following code?

Pascal and Delphi Syntax (Toggle Plain Text)
  1. posNeg:=random(2);
  2. if posNeg=0 then
Reputation Points: 10
Solved Threads: 0
Junior Poster
turbomen is offline Offline
113 posts
since Feb 2009
Jul 5th, 2009
0

Re: May I ask you a question on Delphi?

Hi turbomen
PosNeg is a variable,the Random() function generate a random number it will one or zero,and PosNeg's value willl be one of them,the If statement will evaluate the condition.The evaluating result can be only two values,such as [True] or the another is [False].
If true that posneg's value is zero Then,the program does the added commands,if the condition is FALSE then the [Else] branch follows if there is any....
Good practice,try it...
Load your code into delphi,press the [Run] menu and press [Trace Into F7],so you can go inside the program,by pressing the [F7] button,you will see the steps one by one,look at your If statement's changes with your own eyes...
Reputation Points: 132
Solved Threads: 138
Posting Pro
FlamingClaw is offline Offline
559 posts
since Feb 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Pascal and Delphi Forum Timeline: generate sine waves with delphi
Next Thread in Pascal and Delphi Forum Timeline: Could you tell me what's wrong of my answer?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC