A pair of statements as single statement
Author Message
A pair of statements as single statement

Quote:
> I am a newbie in Pascal. I am a fairly good programmer in Clipper, but
> I am trying to get my B.S. in computer science, and I have several > questions. I will begin with one.

> They want me to write this as a single statement.....

> Age := 17 + oldage;
> Age := Newage - 5;

Are you sure this is the exact requirement?  It doesn't seem right,
because it's contradictory: the 1st statemnt assigns a value to Age and
the 2nd assigns a different value to Age.  Combining these statements has
no purpose, since the 2nd one is the only statement which accomplishes
anything.
It looks as though you didn't copy the assignment correctly, and that
it should be:

NewAge := 17 + OldAge;
Age := NewAge - 5;

If that's the case, you have to understand operator precedence, to
know what will happen when these statement are combined.  In these
statements, the order of precedence is linear/equal, so all
subexpressions are evaluated left-to-right.  It can be coded as:

Age := (OldAge+17)-5

That is, the expression in the parentheses is evaluated, _then_ 5 is
subtracted from the result...which is then assigned to Age.  You have to
evaluate the 1st statement's expression completely, before you can apply
it in the 2nd statement.
Of course, it )could_ be made even simpler by:

Age := OldAge+12;

but that isn't doing what the instructor wants you to learn...

Wed, 18 Jun 1902 08:00:00 GMT
A pair of statements as single statement

I am a newbie in Pascal. I am a fairly good programmer in Clipper, but I am
trying to get my B.S. in computer science, and I have several questions. I
will begin with one.

They want me to write this as a single statement.....

Age := 17 + oldage;
Age := Newage - 5;

Any help would be appriciated!!!!

Wed, 18 Jun 1902 08:00:00 GMT
A pair of statements as single statement

Quote:
>I am a newbie in Pascal. I am a fairly good programmer in Clipper, but I am
>trying to get my B.S. in computer science, and I have several questions. I
>will begin with one.

And  they're teaching you Pascal?  Last I'd heard, if you were going into
Comp. Sci. you started learning C right away.  At least I did.

Quote:
>They want me to write this as a single statement.....

>Age := 17 + oldage;
>Age := Newage - 5;

Um, that is really easy.  I would say the new line is:

Age:=Newage-5;

If you notice in the first line, Age is being given a value of
17+oldage.  In the second line, Age is given Newage-5.  Since there is no
operation in the second line to keep the value of Age before Newage-5 is
assigned to it. (If the 2nd line had said Age:=Age +/- Newage - 5, then
you could've written Age := 17+oldage +/- Newage-5.)

Quote:
>Any help would be appriciated!!!!

--
Mike Shepherd (aka:  the Sheepster)
If you're ever in trouble on the water, the first thing you'll
see is a bunch of maniacs in a {*filter*} boat, comin' for to carry
you home.

Wed, 18 Jun 1902 08:00:00 GMT
A pair of statements as single statement

Quote:
>I am a newbie in Pascal. I am a fairly good programmer in Clipper, but I am
>trying to get my B.S. in computer science, and I have several questions. I
>will begin with one.

>They want me to write this as a single statement.....

>Age := 17 + oldage;
>Age := Newage - 5;

(Assuming that OldAge is a variable or constant and not a function
that affects NewAge ...)

I fail to see how you could possibly have a problem with this.

The outcome of the pair of statements above is totally independent
of the first statement: just throw it away and use the second one.

(If OldAge and/or NewAge are functions rather than constants or variables
you should have said so, so I have assumed they are not.)

Wed, 18 Jun 1902 08:00:00 GMT

 Page 1 of 1 [ 4 post ]

Relevant Pages