Expresiones compuestas


In [1]:
z = begin
    x = 1
    y = 2
    x + y
end


Out[1]:
3

In [2]:
z = (x = 1; y = 2; x + y)


Out[2]:
3

In [3]:
begin x = 1; y = 2; x + y end


Out[3]:
3

In [4]:
(x = 1;
y = 2;
x + y)


Out[4]:
3

Condicionales

Condicional simple


In [5]:
a = 2
b = 3
if a < b
    println("a es menor que b")
end


a es menor que b

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


a es mayor que b

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


a es igual a b

Operador ternario


In [8]:
x = 1
y = 2
println(x < y ? "x es menor que y": "x es mayor que y")


x es menor que y

Operadores lógicos

&& (y)


In [9]:
(2 > 3) && (4 < 5)


Out[9]:
false

In [10]:
(3 == 3) && (2 > 1)


Out[10]:
true

|| (o)


In [11]:
(3 > 2) || (2 > 5)


Out[11]:
true

In [12]:
(3 == 2) || (1.2 > 1.1)


Out[12]:
true

Bucles

for


In [13]:
for i = 1:10
    println(i)
end


1
2
3
4
5
6
7
8
9
10

In [14]:
for i in 1:15
    print(i, " ")
end


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 

In [15]:
for elementos in [1,4,0,2,12,11]
    println(elementos)
end


1
4
0
2
12
11

In [16]:
for elementos in [1,4,0,2,12,11]
    println(typeof(i), " ", elementos)
end


i not defined
while loading In[16], in expression starting on line 1

 in anonymous at no file:2

In [17]:
for colores in ["rojo", "azul", "verde"]
    println(colores)
end


rojo
azul
verde

In [18]:
for colores in ["rojo", "azul", "verde"]
    print(typeof(colores), " ", i)
    println()
end


i not defined
while loading In[18], in expression starting on line 1

 in anonymous at no file:2

Para crear una matriz usamos un bucle y el comando reshape


In [19]:
A = reshape(1:100, (10,10))


Out[19]:
10x10 Array{Int32,2}:
  1  11  21  31  41  51  61  71  81   91
  2  12  22  32  42  52  62  72  82   92
  3  13  23  33  43  53  63  73  83   93
  4  14  24  34  44  54  64  74  84   94
  5  15  25  35  45  55  65  75  85   95
  6  16  26  36  46  56  66  76  86   96
  7  17  27  37  47  57  67  77  87   97
  8  18  28  38  48  58  68  78  88   98
  9  19  29  39  49  59  69  79  89   99
 10  20  30  40  50  60  70  80  90  100

In [20]:
for elemento in A
    print(elemento, " ")
end


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 

while


In [21]:
i = 10
while i >= 1
    println(i)
    i -= 1
end
print("i = ", i)


10
9
8
7
6
5
4
3
2
1
i = 0

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


1
2
3
4
5
6
7
8
9
10
11

In [23]:
j = 1
while true
    println(j)
    j += 1
    if j > 10
        break
    end
end


1
2
3
4
5
6
7
8
9
10

In [24]:
j = 1
while true
    println(j)
    if j >= 10
        break
    end
    j += 1
end


1
2
3
4
5
6
7
8
9
10

In [25]:
j = 1
while true
    println(j)
    j += 1
    if j == 10
        break
    end
end


1
2
3
4
5
6
7
8
9

In [26]:
j = 1
while true
    println(j)
    j += 1
    if j == 11
        break
    end
end


1
2
3
4
5
6
7
8
9
10

break y continue


In [27]:
for i in 1:1000
    print(i, " ")
    if i == 10
        break
    end
end


1 2 3 4 5 6 7 8 9 10 

In [28]:
for j = 1:100
    if j % 2 == 0
        print(j, " ")
    else
        continue
    end
end


2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100