miércoles, 15 de marzo de 2017

Bucle para imprimir números naturales

<html>
<head>
<title>Practica7</title>
<script type="text/javascript">
    //Imprimir los 10 primeros numeros naturales
    var x, y;
    //Con ciclo for
    document.write("Imprime los numeros naturales del 1 al 10 <br>");
    for (x = 1; x <= 10; x++)
    {
        document.write( x + "; ");
    }
    document.write("<br>");
    //solo los pares
    document.write("Imprime los numeros naturales pares del 1 al 10 <br>");
    for (x = 1; x < 10; x++)
    {
        if (x % 2)
        {
            x++;
        }
        document.write( x + '; ');
    }
    document.write("<br>");
    //Bucle anidado
    document.write("Imprime una tabla de los numeros naturales del 1-10 <br>");
    for (x = 1; x < 10; x++)
    {    for (y = 1; y < 10; y++)
        {
            document.write(x + ":" + y+"  ");
        }
        document.write('<br>');
    }
    document.write("<br>");
    //Bucle infinito
    document.write("Imprime los numeros naturales del 1-10 utilizando un bucle infinito <br>");
    x=1;
    for (;;)
    {
        document.write( x + '; ');
        x++;
        if(x==10)
            break;
    }
    document.write("<br>");
    //Bucle while
    document.write("Imprime los numeros naturales del 1-10 utilizando un bucle while <br>");
    var x = 1;
    while (x < 10)
    {
        document.write( x + "; ");
        x++;
    }
    document.write("<br>");
    //Bucle while
    document.write("Imprime los numeros naturales del 1-10 utilizando un bucle while <br>");
    var x = 1;
    do
    {
        document.write( + x + "; ");
        x++;
    } while (x < 10);
    document.write("<br>");
    //Aplicación: Trabajando con listas
    document.write("Imprime una lista utilizando bucle for <br>");
    var lista = ["1primero","2segundo","3tercero"];
    for (i=0; i<lista.length; i++)
    {
        document.write(lista[i]+"; ");
    }
    document.write("<br>");
   
</script>
</head>
<body>

</body>
</html>

No hay comentarios:

Publicar un comentario