Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Simple Loop Problem

Former Member
0 Likes
821

I have been given this problem:

Write a program that prompts the user for three values: a starting number, an ending number, and a

count by number. The program should then display the number sequence that results from beginning at the starting

number, counting by the count by number, and stopping when the ending number is hit or passed by.

For example, if the user entered 2, 3, and 12 respectively, the program should display the following:

Going from 2 to 12, counting by 3:

2, 5, 8, 11

I have the display to work by doing this:

PARAMETERS: start TYPE i,

             end TYPE i,

             count TYPE i.

WHILE start LE end.

     write: start NO-GAP, ', '.

   start = start + count.

ENDWHILE.


But can't figure out for the life of me how to not have the ending comma.  I know I should prob use SY-INDEX in an IF statement, and I've tried so many ways but can't figure it out.  I've now looked at it so much that I need an outsiders input.  Any help would be greatly appreciated!

6 REPLIES 6
Read only

Former Member
0 Likes
790

Hi,

By no means the only way, but could you accumulate the output into a string rather than write it to the screen number by number?

This would give you the option to manipulate the commas before writing the string to the screen.

Regards,

Nick

Read only

karthikeyan_p3
Contributor
0 Likes
790

Hi Ashley,

This will work fine.

PARAMETERS: start TYPE i,

             end TYPE i,

             count TYPE i.

WHILE start LE end.

if sy-index = 1.

  write: start NO-GAP.

else.

  write: ', ' NO-GAP, start NO-GAP.

endif.

   start = start + count.

ENDWHILE.

Regards,

Karthikeyan

Read only

0 Likes
790

Thanks so much!

Read only

0 Likes
790

welcome

Read only

nabheetscn
SAP Champion
SAP Champion
0 Likes
790

You can move it to a string as Nick told or you can just check an extra addition of count in start in another variable to check if it is the last iteration or not

Read only

dirk_wittenberg
Contributor
0 Likes
790

Hi Ashley,

or this:

PARAMETERS: start TYPE i,

             end TYPE i,

             count TYPE i.

IF start LE end.

  WRITE start NO-GAP.

  start = start + count.

ENDIF.

WHILE start LE end.

  WRITE: ', ' NO-GAP, start NO-GAP.

  start = start + count.

ENDWHILE.

Regards,

Dirk