HomeForumResearchGuide
Sign in to reply to forum posts.
Negative integers

hi!

I'm just learning Dana for a project and wanted to check if it has negative integers?

thanks

jess

Hi Jess,

Dana doesn't have negative integers; this is a design choice which simplifies the type system, particularly given the level of runtime reflection that the language has.

If you need to use negative numbers for something you would go with the dec type instead :-)

Barry

That's what I thought, thank you!

Maybe this is a simple one but if I was to write a for loop with a decreasing loop counter (which is an int) how would I write the loop end condition?

thanks

jess

Hi Jess,

You would count it down until it gets to 0 and then break the loop. Note that you cannot use >= (the 'equals' symbol). So something like:

i = 10
while (i > 0) {
   out.println("hello")
   i--
}

or

for (int i = 10; i > 0; i--) {
    out.println("hey!")
}

Both would work.

I hope this was useful.

Roberto

Just for completeness...

In case you want to go through an array backward. Then the loop would look like this:

component provides App requires io.Output out, data.IntUtil iu {
    int App:main(AppParam params[]) {
        int myarray[] = new int[10]
        for (int i = myarray.arrayLength; i > 0; i--) {
            myarray[i-1] = i-1
            out.println("$(iu.intToString(myarray[i-1]))")
        }
        return 0
    }
}

ok, thanks for the examples!! Is there also a way to do it without the "i-1"? But to still loop down to zero?

jess

Hi Jess,

So you can use INT_MAX as your loop end condition:

        for (int i = 9; i != INT_MAX; i--) {
            out.println("$i")
        }

We also often use INT_MAX as an error value from functions that return an int, like StringUtil.find(), where INT_MAX would be an impossible result.

Barry

Oh that makes sense because the int is going to wrap after zero, thanks! :)

jess