In [1]:
z = begin
x = 1
y = 2
x + y
end
Out[1]:
In [2]:
z = (x = 1; y = 2; x + y)
Out[2]:
In [3]:
begin x = 1; y = 2; x + y end
Out[3]:
In [4]:
(x = 1;
y = 2;
x + y)
Out[4]:
Condicional simple
In [5]:
a = 2
b = 3
if a < b
println("a es menor que b")
end
In [6]:
a = 3
b = 2
if a < b
println("a es menor que b")
else a > b
println("a es mayor que b")
end
Condicional anidado
In [7]:
a = 2
b = 2
if a < b
println("a es menor que b")
elseif a > b
println("a es mayor que b")
else
println("a es igual a b")
end
Operador ternario
In [8]:
x = 1
y = 2
println(x < y ? "x es menor que y": "x es mayor que y")
&& (y)
In [9]:
(2 > 3) && (4 < 5)
Out[9]:
In [10]:
(3 == 3) && (2 > 1)
Out[10]:
|| (o)
In [11]:
(3 > 2) || (2 > 5)
Out[11]:
In [12]:
(3 == 2) || (1.2 > 1.1)
Out[12]:
for
In [13]:
for i = 1:10
println(i)
end
In [14]:
for i in 1:15
print(i, " ")
end
In [15]:
for elementos in [1,4,0,2,12,11]
println(elementos)
end
In [16]:
for elementos in [1,4,0,2,12,11]
println(typeof(i), " ", elementos)
end
In [17]:
for colores in ["rojo", "azul", "verde"]
println(colores)
end
In [18]:
for colores in ["rojo", "azul", "verde"]
print(typeof(colores), " ", i)
println()
end
Para crear una matriz usamos un bucle y el comando reshape
In [19]:
A = reshape(1:100, (10,10))
Out[19]:
In [20]:
for elemento in A
print(elemento, " ")
end
while
In [21]:
i = 10
while i >= 1
println(i)
i -= 1
end
print("i = ", i)
Para imitar un:
do
...
while blarg
usamos:
while true
...
if blarg
break
end
end
In [22]:
j = 1
while true
println(j)
if j > 10
break
end
j += 1
end
In [23]:
j = 1
while true
println(j)
j += 1
if j > 10
break
end
end
In [24]:
j = 1
while true
println(j)
if j >= 10
break
end
j += 1
end
In [25]:
j = 1
while true
println(j)
j += 1
if j == 10
break
end
end
In [26]:
j = 1
while true
println(j)
j += 1
if j == 11
break
end
end
break y continue
In [27]:
for i in 1:1000
print(i, " ")
if i == 10
break
end
end
In [28]:
for j = 1:100
if j % 2 == 0
print(j, " ")
else
continue
end
end