-
Notifications
You must be signed in to change notification settings - Fork 0
/
20-controleDeFluxo-Funcoes.kt
71 lines (54 loc) · 1.4 KB
/
20-controleDeFluxo-Funcoes.kt
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
package introducaoALinguagem
fun main(){
//Exemplo de funções com a estrutura IF/ELSE:
//Podemos ter um retorno direto
val resultado = retornoUm(true)
println("Maior de Idade? ${maiorDeIdade(15)}")
println(retornoDois(true))
//Exemplo de funções com o controle de fluxo While:
println(bonusWhen("Engenheiro de Software"))
println(whenComRange(5))
println(whenComBoolean(false))
}
//Funções com IF/ELSE:
fun maiorDeIdade(idade: Int): Boolean{
if(idade > 18){
return true
}
return false
//Poderiamos ter retornado assim: return (idade > 18), pois o proprio retorno é um booleano
}
fun retornoUm(t: Boolean): String{
return if(t){
"Bom dia"
}else{
"Boa Noite"
}//Esse return do IF só funciona se existir
}
fun retornoDois(t: Boolean) = if(t){
"Bom dia"
}else{
"Boa Noite"
}
//Funções com While:
fun bonusWhen(cargo: String): Float{
return when(cargo){
"Gerente" -> 2000f
"Coordenador" -> 1500f
"Engenheiro de Software" -> 1000f
"Estagiário" -> 500f
else -> 0f
}//Exemplo de When com Retorno automático
}
fun whenComRange(n: Int): Unit{
when(n){
in 1..10 -> println("1...10")
in 10..20 -> println("10...20")
}
}
fun whenComBoolean(b: Boolean): Unit{
when(b){
true -> println("Verdadeiro")
false -> println("Falso")
}
}