HadesLang Doc
  • Overview
  • Getting Started
    • Installing Hades
    • Basic Syntax
    • Coding Conventions
  • Language Spec
    • Foundation
      • Types
      • Built-in functions
    • Operators
      • Comparison and equality
      • Logical operators
      • Bitwise operators
      • Compound Assignment Operators
      • Operator overloading
    • Control flow
      • Conditions
      • Loops
      • Exception handling
      • Ternary and nullcheck operators
      • Pipelines
    • Classes and variables
      • Declaring variables
      • Declaring classes
      • Declaring structs
      • Declaring protos
      • Declaring arrays
      • Type conversions in simple variable types
    • Actors
      • Message passing
      • GenServer
      • Channels
    • Functions and lambdas
      • Declaring functions
      • Declaring lambdas
    • Functions of simple types
      • int
      • string
      • float
      • bool
      • atom
      • pid
    • Other
      • Annotations
      • Comments
      • Preprocessor statements
      • Reflection
      • Script file arguments
      • this keyword
  • Core Libraries
    • Standard library
      • std:io
        • console
        • file
        • directory
      • std:exceptions
      • std:int
      • std:internals
        • annotations
          • findMethod
          • findMethodInProto
          • findMethods
          • findMethodsInProto
        • function
      • std:collections
        • map
        • list
      • std:math
        • math
        • constants
        • matrix
      • std:networking
      • std:os
      • std:params
      • std:string
      • std:sql
    • Extra libraries
      • mssql:client
  • Other
    • Tools
    • Examples
    • Todos
Powered by GitBook
On this page
  • for loop
  • Example
  • Example
  • while loop
  • Example
  • stop and skip statements
  • Example
  • Example
  1. Language Spec
  2. Control flow

Loops

for loop

In Hades, there is no C-style count-controlled loop, but only a foreach loop which iterates over an array.

Example

with math fixed from std:math
with console from std:io

for(var i int in range(0,10) /*'range' returns an array with the number 0 to 10*/)
    console.out(i)
end

let fruits string[] = {"Apple", "Banana", "Mango", "Kiwi"}

for(var fruit in fruits)
    console.out("{} is very healthy".format(fruit))
end

If you don't need a variable, you can use an underscore instead of naming a variable or variable declaration.

for(_ in range(0,10))
    console.out("")
end

Range also has a shorthand: ...

Example

1..5 //returns [1,2,3,4,5]
1|..5 //returns [2,3,4,5]
1|..|5 //returns [2,3,4]

"a".."Z" //returns ["a", "b"..."X", "Y", "Z"]

while loop

The while loop pretty much works like you'd expect. It loops as long as a given condition evaluates to true.

Example

with console from std:io
var c = 0

while(c not 10)
    console.out("c is {}".format(c))
end

stop and skip statements

The stop statement stops a loop.

Example

with console from std:io
var c = 0

while(true)
    if(c == 10)
        stop
    end
    
    console.out("c is {}".format(c))
    c++
end

The skip statement skips over the current state of a loop. In while loops, this just skips execution of the code after the skip statement. In a for loop, this statement also skips to the next value of an array.

Example

with console from std:io

var c = 0

while(c not 10)
    if(c not 5)
        c++
        skip
    end
    
    console.out("c is 5")
    c++
end

// Output: c is 5 

var fruits = {"Apple", "Banana", "Mango", "Kiwi"}

for(var fruit in fruits)
    if(fruit == "Banana")
        skip
    end
    
    console.out(fruit)
end

/*
Output:
Apple
Mango
Kiwi
*/

PreviousConditionsNextException handling

Last updated 5 years ago