# Code to be explained

**URL:** <https://forum.ada-lang.io/t/code-to-be-explained/1218>\
**Category:** General\
**Created:** [September 19, 2024, 10:47am UTC](https://forum.ada-lang.io/t/code-to-be-explained/1218 "2024-09-19T10:47:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![valerieadams](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/v/ba9def/32.png) [@valerieadams](https://forum.ada-lang.io/u/valerieadams)\
**Post date:** [September 19, 2024, 10:47am UTC](https://forum.ada-lang.io/t/code-to-be-explained/1218/1 "2024-09-19T10:47:58Z")

</div>

How does the number of + signs increases for each line by using this program ? I’m still quite lost

with Ada.Text\_IO; – For input and output  
with Ada.Integer\_Text\_IO; – For handling integer inputs

procedure Plus\_Triangle is  
– Declare variables  
Lines : Integer; – The number of lines  
I : Integer; – Line counter  
J : Integer; – ‘+’ counter for each line

begin  
– Prompt the user for the number of lines  
Ada.Text\_IO.Put("Enter the number of lines: ");  
Ada.Integer\_Text\_IO.Get(Lines); – Get the number of lines from user input

– Initialize the outer loop counter  
I := 1;

– Outer while loop for each line  
while I \<= Lines loop  
– Initialize the inner loop counter for the ‘+’ symbols  
J := 1;

```
  -- Inner while loop to print the '+' symbols for the current line
  while J <= I loop
     Ada.Text_IO.Put("+");
     J := J + 1;
  end loop;

  -- Move to the next line
  Ada.Text_IO.New_Line;

  -- Increment the line counter
  I := I + 1;

```

end loop;

end Plus\_Triangle;

Can anybody give me a clarification regarding the inner loop ?

---

<div class="post-metadata">

**Author:** ![simonjwright](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/simonjwright/32/11_2.png) [@simonjwright](https://forum.ada-lang.io/u/simonjwright)\
**Post date:** [September 19, 2024, 11:42am UTC](https://forum.ada-lang.io/t/code-to-be-explained/1218/2 "2024-09-19T11:42:39Z")

</div>

I’m not great at teaching, so have a look [here](https://dwheeler.com/lovelace/s5sf.htm#:~:text=A%20while%20loop%20repeatedly%20executes,the%20keyword%20%60%60for''.).

---

<div class="post-metadata">

**Author:** ![DrPi](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/drpi/32/225_2.png) [@DrPi](https://forum.ada-lang.io/u/DrPi)\
**Post date:** [September 22, 2024, 12:31pm UTC](https://forum.ada-lang.io/t/code-to-be-explained/1218/3 "2024-09-22T12:31:45Z")

</div>

You should edit your post and add triple back ticks to make the code readable.

---

<div class="post-metadata">

**Author:** ![Micronian2](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/m/e56c9b/32.png) [@Micronian2](https://forum.ada-lang.io/u/Micronian2)\
**Post date:** [September 22, 2024, 6:22pm UTC](https://forum.ada-lang.io/t/code-to-be-explained/1218/4 "2024-09-22T18:22:28Z")

</div>

Hi, It’s increasing because the upper bound, “I”, is increased after each line.

---

<div class="post-metadata">

**Author:** ![OneWingedShark](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/onewingedshark/32/305_2.png) [@OneWingedShark](https://forum.ada-lang.io/u/OneWingedShark)\
**Post date:** [September 22, 2024, 10:09pm UTC](https://forum.ada-lang.io/t/code-to-be-explained/1218/5 "2024-09-22T22:09:23Z")

</div>

> [@valerieadams](#):
>
> Can anybody give me a clarification regarding the inner loop ?

Well, you have several things going on:

1. Obtain some user-input;
2. Using a `while`-loop, (the comment indicates a `for` iteration though;
3. Using a second `while`-loop, which comment indicates is possibly a `for` iteration.

Given these considerations, I have to ask: _are these examples from a book or tutorial?_ If so, _are they from some other, non-Ada language?_

Because, as I read it, your procedure is equivalent to:

```ada
with
Ada.Text_IO,
Ada.Integer_Text_IO;

Procedure Plus_Triangle is
   Lines : Integer; – The number of lines
Begin
   Ada.Text_IO.Put("Enter the number of lines: ");
   Ada.Integer_Text_IO.Get(Lines);
   OUTER:
   For I in 1..Lines loop
      INNER:
      For J in 1..I loop
         Ada.Text_IO.Put( "+" );
      End loop INNER;
      Ada.Text_IO.New_Line;
   End loop OUTER;
End Plus_Triangle;

```

So, observably the inner loop goes from `1` to whatever value `I` is, where `I` is ranging from 1 to the user-input `Lines`.

---

<div class="post-metadata">

**Author:** ![JC001](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/j/e56c9b/32.png) [@JC001](https://forum.ada-lang.io/u/JC001)\
**Post date:** [September 23, 2024, 10:40am UTC](https://forum.ada-lang.io/t/code-to-be-explained/1218/6 "2024-09-23T10:40:04Z")

</div>

Yes, and the output part of this can be simplified even further to

```ada
for I in 1 .. Lines loop
   Ada.Text_IO.Put_Line (Item => (1 .. I => '+') );
end loop;

```

which is even clearer. This was shown to the OP in [this post](https://forum.ada-lang.io/t/how-to-multiply-a-string/1214/8), so perhaps there is some unstated reason for taking the more complex, unclear approach.
