Created
April 4, 2012 14:45
-
-
Save HiroNakamura/2302029 to your computer and use it in GitHub Desktop.
Estructuras de control básicas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Python] | |
''' | |
Este es un comentario multilínea | |
''' | |
#selectivas, en Python no existe switch | |
global numero | |
numero=18 | |
if numero<=18: | |
print "no cumpliste los 18 años" | |
elif numero>=18: | |
print "bienvenido" | |
else: | |
print "un simple mensaje" | |
#iterativas: while | |
global suma,cont,maximo | |
cont, suma,maximo=0,0, 10 | |
while(cont<maximo): | |
num=int(raw_input('Número:')) | |
cont=cont+1 | |
suma=suma+1 | |
print "suma: ",suma | |
#iterativas: for | |
vector=[34,54,67,76] | |
for i in vector: | |
print i*2 | |
[Groovy] | |
/*iterativas*/ | |
def valores=[1,2,3,5,7,54,3,0] | |
numeros.each(){ | |
println("${it}") | |
} | |
//otra forma | |
def rango=1..12 | |
def suma=0 | |
rango.each(){ | |
suma+=it | |
println("suma obtenida"+suma) | |
// también se puede así println("suma obtenida ${suma}") | |
} | |
//otra más | |
for(i in rango){ | |
println("${i}") | |
} | |
/*selectivas*/ | |
def numero=10 | |
switch(numero){ | |
case 1..4: algo();break; | |
case 5..8: algo(); break; | |
case 9..12:esto();break; | |
} | |
def booleano=(3>21) && (0==4) | |
if(booleano){ | |
//no se cumple | |
}else{ | |
//se cumple | |
} | |
[Scala] | |
//selectivas | |
var hrs=50 | |
var msg="" | |
var booleano=if(hrs>=50) msg="correcto" else msg="incorrecto" | |
println(booleano) | |
//iterativas: while | |
var i=0 | |
while(i <args.length){ | |
println("Hola: "+args(i)) | |
i=i+1 | |
} | |
//iterativas: for | |
var i=1 | |
var limite=10 | |
var suma=0 | |
for(i <- 1 to limite) | |
suma=suma+i | |
println("suma: "+suma) | |
[Java] | |
/*iterativas*/ | |
//for "simple" | |
int i=0,suma=0; | |
int[] numeros={3,5,6,7,8,12}; | |
for(i=0;i<numeros.length;i++){ | |
suma+=numeros[i]; | |
} | |
System.out.printf("suma total: %d",suma); | |
//for "foreach" | |
int[] valores={32,65,6,21,88}; | |
int suma=0; | |
for(int e: valores){ | |
suma+=e; | |
} | |
System.out.printf("suma total: %d",suma); | |
/*selectivas*/ | |
//switch | |
int opc=3; | |
switch(opc){ | |
case 0: algo();break; | |
case 1: algo();break; | |
case 2: algo();break; | |
case 3: esto();break; | |
default: nada();break; | |
} | |
// if(){}else{} | |
boolean expresion=(9!=0) && (4>=0); | |
if(expresion){ | |
// esto se cumple | |
}else{ | |
// esto no se cumple | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment