Control Flow
Kite provides standard control flow constructs: if/else, while loops, and for loops.
If Statements
Basic Syntax
if condition {
// body
}With Parentheses
Parentheses around the condition are optional:
// Both are valid
if (x > 5) {
doSomething()
}
if x > 5 {
doSomething()
}If-Else
if condition {
// true branch
} else {
// false branch
}Examples
var x = 10
if x > 5 {
var result = "greater"
}
if (x == 10) {
var result = "equal"
} else {
var result = "not equal"
}
// Nested conditions
if x > 0 {
if x < 100 {
var result = "in range"
}
}Important: Braces Required
Unlike some languages, Kite requires braces for if/else bodies:
// Valid
if x > 5 { doSomething() }
// Invalid - missing braces
if x > 5 doSomething() // ERRORWhile Loops
Basic Syntax
while condition {
// body
}Examples
var count = 0
while count < 10 {
count++
}
// With parentheses (optional)
while (count < 20) {
count++
}For Loops
Iterating Over Ranges
for i in 0..5 {
// i = 0, 1, 2, 3, 4 (end exclusive)
}Iterating Over Arrays
var items = ["a", "b", "c"]
for item in items {
// item = "a", "b", "c"
}With Index
var items = ["a", "b", "c"]
for index, item in items {
// index = 0, item = "a"
// index = 1, item = "b"
// index = 2, item = "c"
}For Loops with Resources
For loops can create multiple resources:
var environments = ["dev", "staging", "prod"]
[for env in environments]
resource S3.Bucket bucket {
name = "data-${env}"
}This creates three buckets: data-dev, data-staging, data-prod.
Array Comprehensions
Kite supports three forms of array comprehensions.
Form 1: Compact (Inline Body)
// Basic
[for i in 0..6: i * 2]
// Result: [0, 2, 4, 6, 8, 10]
// With filter
[for i in 0..10: if i > 5 { i }]
// Result: [6, 7, 8, 9]
// With transformation
[for item in items: item.name]Form 2: Block Body (For Resources)
var environments = ["dev", "staging", "prod"]
[for env in environments]
resource S3.Bucket bucket {
name = "photos-${env}"
}Form 3: Standalone For Loop
for i in 0..10 {
var doubled = i * 2
}Comprehension with Conditionals
// Inline if
[for i in 0..10: if (i % 2 == 0) i]
// Result: [0, 2, 4, 6, 8]
// If-else
[for i in 0..10: if i > 5 { i * 2 } else { i }]
// Result: [0, 1, 2, 3, 4, 5, 12, 14, 16, 18]Nested Comprehensions
var matrix = [
[for j in 0..3: i * 3 + j]
for i in 0..3
]
// Result: [[0,1,2], [3,4,5], [6,7,8]]Creating Multiple Resources
Using @count Decorator
@count(3)
resource EC2.Instance server {
name = "server-${count}"
}
// Creates: server-0, server-1, server-2Using For Loops
var zones = ["us-east-1a", "us-east-1b", "us-east-1c"]
[for zone in zones]
resource EC2.Instance server {
name = "server-${zone}"
availabilityZone = zone
}Conditional Resource Creation
var createBackup = true
if createBackup {
resource S3.Bucket backup {
name = "backup-bucket"
}
}Control Flow in Components
component WebApp {
input number replicas = 1
input boolean enableCache = false
@count(replicas)
resource EC2.Instance server {
name = "server-${count}"
}
// Conditional resource
if enableCache {
resource ElastiCache.Cluster cache {
name = "app-cache"
}
}
}Best Practices
Use Braces Consistently
Always use braces, even for single statements:
// Recommended
if x > 5 {
doSomething()
}
// Not allowed anyway
if x > 5 doSomething() // ERRORPrefer For Loops for Resources
When creating multiple similar resources:
// Good - explicit iteration
for env in ["dev", "staging", "prod"]
resource S3.Bucket bucket {
name = "data-${env}"
}
// Also good - count decorator
@count(3)
resource EC2.Instance server {
name = "server-${count}"
}Use Comprehensions for Data Transformation
var servers = [
{ name: "web", port: 80 },
{ name: "api", port: 8080 },
{ name: "db", port: 5432 }
]
// Extract names
var names = [for s in servers: s.name]
// Result: ["web", "api", "db"]
// Filter by port
var highPorts = [for s in servers: if s.port > 1000 { s.name }]
// Result: ["api", "db"]Next: Learn about Strings - string types and interpolation.