BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
odesh
Quartz | Level 8

Hello.

Please see attached DS2 program.

 

My understanding is as follows:

In the method SumPlus , V1  receives a datatype of double. However, when called/instantiated, TotalA is converted ( or coerced ? )from char to double. This enables addition in the method without a compile error, ( Is my thinking correct so far ? )

 

Going through the do ..end loop, we should, in my mind, have the following values as we go through the 3 iterations:

 

Iteration1 TotalB = 1   TotalA =  101  GrandTot = 102 ( 101 + 1 )

Iteration2 TotalB = 2   TotalA =  101  GrandTot = 103 ( 102 + 1 )

Iteration1 TotalB = 3   TotalA =  101  GrandTot = 104 ( 103 + 1 )

 

The answer says TotalB = 4 !

Please help.

 

Thanks.

Odesh.

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Cynthia_sas
Diamond | Level 26

Hi:

  The fact that TotalB is 4 is due to standard DO loop behavior. When you have both start and stop values (in this case, start=1 and stop=3), the way the DO loop works is that the value of the index-variable is evaluated at the TOP of the loop. If the index-variable is less than or equal to the stop value, the statements inside the DO/END loop are executed; then at the bottom of the loop, the index-variable is increased by the increment amount (1 is the default).

 

  I would correct your statements as follows:

correct_end_value.png

 

  Here's the documentation page where the DO loop syntax and operation is discussed. https://go.documentation.sas.com/?docsetId=lestmtsref&docsetTarget=p1cydk5fq0u4bfn1xfbjt7w1c7lu.htm&... -- it is VERY clear -- When both start and stop are present, (both are present in your code) execution continues until the value of index-variable (in this case TotalB) passes the value of stop (in this case, 3).

 

So here's a longer description of processing:

The first iteration, TotalB starts at 1; when TotalB is tested 1 is not greater than 3, so the statements execute; at the bottom of loop 1, TotalB is incremented by 1. The new value of TotalB is 2.

 

The second iteration, TotalB is now 2; when TotalB is tested 2 is not greater than 3, so the statements execute; at the bottom of loop2, TotalB is incremented to 3.

 

The third iteration, TotalB is now 3; when TotalB is tested 3 is equal to 3, so the statements execute; at the bottom of loop3, TotalB is incremented to 4.

 

The 4 iteration, TotalB is now 4; when totalB is tested at the TOP of the loop, before the loop is entered, 4 IS greater than 3, so the loop stops.

 

  You can prove this to yourself by adding a few more PUT statements into your program as shown below:

use_PUT_show_values.png

 

This is standard DO loop processing that we cover in the Programming 2 class. In the above screen shot, the yellow PUT highlight is for the values at the top of the loop, the green highlight from the PUT after the calculation of GrandTot. Note how at  the top of iteration 2, TotalB has already been incremented -- the value is 1 at the end of iteration 1 and is 2 at the top of iteration 2. Since we know that the loop stops only when the value of the index-variable passes or is greater than the stop value, that means TotalB had to be incremented to 4 and on the final test at the top of the loop, the fact that 4 was greater than 3 stopped the loop, so the statements INSIDE the loop were never executed, but the final PUT was executed.

 

Hope this helps,

Cynthia

View solution in original post

2 REPLIES 2
Cynthia_sas
Diamond | Level 26

Hi:

  The fact that TotalB is 4 is due to standard DO loop behavior. When you have both start and stop values (in this case, start=1 and stop=3), the way the DO loop works is that the value of the index-variable is evaluated at the TOP of the loop. If the index-variable is less than or equal to the stop value, the statements inside the DO/END loop are executed; then at the bottom of the loop, the index-variable is increased by the increment amount (1 is the default).

 

  I would correct your statements as follows:

correct_end_value.png

 

  Here's the documentation page where the DO loop syntax and operation is discussed. https://go.documentation.sas.com/?docsetId=lestmtsref&docsetTarget=p1cydk5fq0u4bfn1xfbjt7w1c7lu.htm&... -- it is VERY clear -- When both start and stop are present, (both are present in your code) execution continues until the value of index-variable (in this case TotalB) passes the value of stop (in this case, 3).

 

So here's a longer description of processing:

The first iteration, TotalB starts at 1; when TotalB is tested 1 is not greater than 3, so the statements execute; at the bottom of loop 1, TotalB is incremented by 1. The new value of TotalB is 2.

 

The second iteration, TotalB is now 2; when TotalB is tested 2 is not greater than 3, so the statements execute; at the bottom of loop2, TotalB is incremented to 3.

 

The third iteration, TotalB is now 3; when TotalB is tested 3 is equal to 3, so the statements execute; at the bottom of loop3, TotalB is incremented to 4.

 

The 4 iteration, TotalB is now 4; when totalB is tested at the TOP of the loop, before the loop is entered, 4 IS greater than 3, so the loop stops.

 

  You can prove this to yourself by adding a few more PUT statements into your program as shown below:

use_PUT_show_values.png

 

This is standard DO loop processing that we cover in the Programming 2 class. In the above screen shot, the yellow PUT highlight is for the values at the top of the loop, the green highlight from the PUT after the calculation of GrandTot. Note how at  the top of iteration 2, TotalB has already been incremented -- the value is 1 at the end of iteration 1 and is 2 at the top of iteration 2. Since we know that the loop stops only when the value of the index-variable passes or is greater than the stop value, that means TotalB had to be incremented to 4 and on the final test at the top of the loop, the fact that 4 was greater than 3 stopped the loop, so the statements INSIDE the loop were never executed, but the final PUT was executed.

 

Hope this helps,

Cynthia

odesh
Quartz | Level 8

Thanks Cynthia for the excellent detailed answer. I was a bit rusty on do-loop processing.

 

Odesh.