# Output of the code

**URL:** <https://verificationacademy.com/forums/t/output-of-the-code/42820>\
**Category:** SystemVerilog\
**Tags:** Dave-Rich, SystemVerilog, System-Verilog, for-loop\
**Created:** [December 12, 2023, 5:01am UTC](https://verificationacademy.com/forums/t/output-of-the-code/42820 "2023-12-12T05:01:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![m\_v](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/m/a3d4f5/32.png) [@m\_v](https://verificationacademy.com/forums/u/m_v)\
**Post date:** [December 12, 2023, 5:01am UTC](https://verificationacademy.com/forums/t/output-of-the-code/42820/1 "2023-12-12T05:01:27Z")

</div>

Hi,

Can you help me to find why this code gives output as 0.I am not able to understand the output because it has to be executed serially so the output of the display must be 20.

```verilog

module test;
  int a;  
  initial begin
    for(int i = 0;i<20;i++)begin
      a = a++;
    end
    $display("a = %0d",a);
  end
endmodule

```

---

<div class="post-metadata">

**Author:** ![Nandakumari](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/n/b9e5f3/32.png) [@Nandakumari](https://verificationacademy.com/forums/u/Nandakumari)\
**Post date:** [December 12, 2023, 5:40am UTC](https://verificationacademy.com/forums/t/output-of-the-code/42820/2 "2023-12-12T05:40:35Z")

</div>

_In reply to [m\_v](https://verificationacademy.com/forums/t/output-of-the-code/42820):_

```verilog
module test;
  int a;  
  initial begin
    for(int i = 0;i<20;i++)begin
       a++;
      end
    $display("a = %0d",a);
  end
endmodule

```

this will gives you the expected answer.

---

<div class="post-metadata">

**Author:** ![m\_v](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/m/a3d4f5/32.png) [@m\_v](https://verificationacademy.com/forums/u/m_v)\
**Post date:** [December 12, 2023, 5:53am UTC](https://verificationacademy.com/forums/t/output-of-the-code/42820/3 "2023-12-12T05:53:07Z")

</div>

_In reply to [Nandakumari](https://verificationacademy.com/forums/t/output-of-the-code/42820/2):_

Hi,yes this will work but i want to understand why that code is giving the output 0.

Thanks.

---

<div class="post-metadata">

**Author:** ![Nandakumari](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/n/b9e5f3/32.png) [@Nandakumari](https://verificationacademy.com/forums/u/Nandakumari)\
**Post date:** [December 12, 2023, 6:48am UTC](https://verificationacademy.com/forums/t/output-of-the-code/42820/4 "2023-12-12T06:48:27Z")

</div>

_In reply to [m\_v](https://verificationacademy.com/forums/t/output-of-the-code/42820/3):_

because always value of a is 0 here.  
first loop: a = 0; then value of a is incremented but not assigned; =\>still value of a is zero this repeats.  
if assign a = ++a ; this assigns incremented value and works as you expected.  
hope you understood.

---

<div class="post-metadata">

**Author:** ![m\_v](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/m/a3d4f5/32.png) [@m\_v](https://verificationacademy.com/forums/u/m_v)\
**Post date:** [December 12, 2023, 7:12am UTC](https://verificationacademy.com/forums/t/output-of-the-code/42820/5 "2023-12-12T07:12:42Z")

</div>

_In reply to [Nandakumari](https://verificationacademy.com/forums/t/output-of-the-code/42820/4):_

Okay. I got you but why incremented value is not assigned? can you explain.

Thanks

---

<div class="post-metadata">

**Author:** ![user60](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/user60/32/1096_2.png) [@user60](https://verificationacademy.com/forums/u/user60)\
**Post date:** [December 12, 2023, 9:30pm UTC](https://verificationacademy.com/forums/t/output-of-the-code/42820/6 "2023-12-12T21:30:27Z")

</div>

_In reply to [m\_v](https://verificationacademy.com/forums/t/output-of-the-code/42820/5):_

> _In reply to [Nandakumari](https://verificationacademy.com/forums/t/output-of-the-code/42820/5):_  
> Okay. I got you but why incremented value is not assigned? can you explain.  
> Thanks

I believe that’s a race condition. As per LRM:

> SystemVerilog includes the C increment and decrement assignment operators ++i, --i, i++, and i–.  
> These do not need parentheses when used in expressions. These increment and decrement assignment  
> operators behave as blocking assignments.  
> The ordering of assignment operations relative to any other operation within an expression is undefined. An  
> implementation can warn whenever a variable is both written and read-or-written within an integral  
> expression or in other contexts where an implementation cannot guarantee order of evaluation

Atleast one simulator does warn on such usage.

HTH
