{"id":297,"date":"2009-05-14T22:56:03","date_gmt":"2009-05-15T03:56:03","guid":{"rendered":"http:\/\/blog.espol.edu.ec\/programando\/?p=297"},"modified":"2009-05-15T09:47:11","modified_gmt":"2009-05-15T14:47:11","slug":"297","status":"publish","type":"post","link":"https:\/\/blog.espol.edu.ec\/programando\/297\/","title":{"rendered":"Sentencia Repetitiva while()"},"content":{"rendered":"<p style=\"margin-bottom: 0cm\">Si necesitamos repetir un grupo de acciones mientras se cumpla una condici\u00f3n la mejor opci\u00f3n es la sentencia repetitiva while().<\/p>\n<p style=\"margin-bottom: 0cm\">\n<p style=\"margin-bottom: 0cm\">Esta sentencia eval\u00faa una condici\u00f3n y luego realiza las acciones especificadas si esta se cumple.<\/p>\n<p style=\"margin-bottom: 0cm\">\n<p style=\"margin-bottom: 0cm\">Modo de uso:<\/p>\n<p style=\"margin-bottom: 0cm\">while(condicion)<\/p>\n<p style=\"margin-bottom: 0cm\">{<\/p>\n<p style=\"margin-bottom: 0cm\">\/\/Acciones<\/p>\n<p style=\"margin-bottom: 0cm\">}<\/p>\n<p style=\"margin-bottom: 0cm\">Secuencia:<\/p>\n<p style=\"margin-bottom: 0cm\">1. Se eval\u00faa la condici\u00f3n.<\/p>\n<p style=\"margin-bottom: 0cm\">2. Si la condici\u00f3n se cumple, se realizan las acciones especificadas, si no se cumple, no se ejecuta ninguna acci\u00f3n y se salta a la pr\u00f3xima l\u00ednea.<\/p>\n<p style=\"margin-bottom: 0cm\">3. Se repiten los pasos 1 y 2 mientras se cumpla la condici\u00f3n.<\/p>\n<p style=\"margin-bottom: 0cm\">\n<p style=\"margin-bottom: 0cm\">Ejemplos:<\/p>\n<p style=\"margin-bottom: 0cm\">\n<p style=\"margin-bottom: 0cm\">Escriba un programa en lenguaje C que solicite el ingreso de dos n\u00fameros (valor inicial y un valor final) y que muestre por pantalla los n\u00fameros que pertenecen al intervalo dado.<\/p>\n<p style=\"margin-bottom: 0cm\">\n<p style=\"margin-bottom: 0cm\">Soluci\u00f3n:<\/p>\n<p style=\"margin-bottom: 0cm\">Este ejercicio lo realizamos cap\u00edtulos anteriores usando la sentencia for(). Podemos hacer lo mismo con la sentencia while()<\/p>\n<p style=\"margin-bottom: 0cm\">\n<p style=\"margin-bottom: 0cm\">Soluci\u00f3n:<\/p>\n<p style=\"margin-bottom: 0cm\">\n<p style=\"margin-bottom: 0cm\">#include &lt;stdio.h&gt;<\/p>\n<p style=\"margin-bottom: 0cm\">void main()<\/p>\n<p style=\"margin-bottom: 0cm\">{<\/p>\n<p style=\"margin-bottom: 0cm\">\/\/Declaraci\u00f3n de variables<\/p>\n<p style=\"margin-bottom: 0cm\">int inicial, final, i;<\/p>\n<p style=\"margin-bottom: 0cm\">\/\/Solicitando y leyendo el ingreso de datos desde el teclado<\/p>\n<p style=\"margin-bottom: 0cm\">printf(\"Ingrese el valor inicial: \");<\/p>\n<p style=\"margin-bottom: 0cm\">scanf(\"%d\",&amp;inicial);<\/p>\n<p style=\"margin-bottom: 0cm\">printf(\"Ingrese el valor final: \");<\/p>\n<p style=\"margin-bottom: 0cm\">scanf(\"%d\",&amp;final);<\/p>\n<p style=\"margin-bottom: 0cm\">i=0; \/\/inicializando la variable \u00edndice<\/p>\n<p style=\"margin-bottom: 0cm\">while(i&lt;=final)<\/p>\n<p style=\"margin-bottom: 0cm\">{<\/p>\n<p style=\"margin-bottom: 0cm\">printf(\"%d\\n\",i); \/\/mostrando por pantalla los n\u00fameros desde<\/p>\n<p style=\"margin-bottom: 0cm\">\/\/el valor inicial hasta el valor final<\/p>\n<p style=\"margin-bottom: 0cm\">i++; \/\/incrementando la variable<\/p>\n<p style=\"margin-bottom: 0cm\">}<\/p>\n<p style=\"margin-bottom: 0cm\">}<\/p>\n<p style=\"margin-bottom: 0cm\">\n<p style=\"margin-bottom: 0cm\">2. Escriba un programa en lenguaje C que solicite el ingreso de un n\u00famero, si este n\u00famero no es par, que lo vuelva a solicitar, el programa terminar\u00e1 cuando sea ingresado un n\u00famero par.<\/p>\n<p style=\"margin-bottom: 0cm\">\n<p style=\"margin-bottom: 0cm\">Soluci\u00f3n:<\/p>\n<p style=\"margin-bottom: 0cm\">\n<p style=\"margin-bottom: 0cm\">#include &lt;stdio.h&gt;<\/p>\n<p style=\"margin-bottom: 0cm\">void main()<\/p>\n<p style=\"margin-bottom: 0cm\">{<\/p>\n<p style=\"margin-bottom: 0cm\">\/\/Declaraci\u00f3n de variables<\/p>\n<p style=\"margin-bottom: 0cm\">int numero;<\/p>\n<p style=\"margin-bottom: 0cm\">\/\/Solicitando y leyendo el ingreso de datos desde el teclado<\/p>\n<p style=\"margin-bottom: 0cm\">numero=1; \/\/Asegurando que el n\u00famero sea impar para que la condici\u00f3n se cumpla<\/p>\n<p style=\"margin-bottom: 0cm\">while(numero%2!=0)<\/p>\n<p style=\"margin-bottom: 0cm\">{<\/p>\n<p style=\"margin-bottom: 0cm\">printf(\"Ingrese un numero: \");<\/p>\n<p style=\"margin-bottom: 0cm\">scanf(\"%d\",&amp;numero);<\/p>\n<p style=\"margin-bottom: 0cm\">}<\/p>\n<p style=\"margin-bottom: 0cm\">}<\/p>\n<p style=\"margin-bottom: 0cm\">\n","protected":false},"excerpt":{"rendered":"<p>Si necesitamos repetir un grupo de acciones mientras se cumpla una condici\u00f3n la mejor opci\u00f3n es la sentencia repetitiva while(). Esta sentencia eval\u00faa una condici\u00f3n y luego realiza las acciones especificadas si esta se cumple. Modo de uso: while(condicion) { \/\/Acciones } Secuencia: 1. Se eval\u00faa la condici\u00f3n. 2. Si la condici\u00f3n se cumple, se [&hellip;]<\/p>\n","protected":false},"author":385,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-297","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/blog.espol.edu.ec\/programando\/wp-json\/wp\/v2\/posts\/297","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.espol.edu.ec\/programando\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.espol.edu.ec\/programando\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.espol.edu.ec\/programando\/wp-json\/wp\/v2\/users\/385"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.espol.edu.ec\/programando\/wp-json\/wp\/v2\/comments?post=297"}],"version-history":[{"count":5,"href":"https:\/\/blog.espol.edu.ec\/programando\/wp-json\/wp\/v2\/posts\/297\/revisions"}],"predecessor-version":[{"id":299,"href":"https:\/\/blog.espol.edu.ec\/programando\/wp-json\/wp\/v2\/posts\/297\/revisions\/299"}],"wp:attachment":[{"href":"https:\/\/blog.espol.edu.ec\/programando\/wp-json\/wp\/v2\/media?parent=297"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.espol.edu.ec\/programando\/wp-json\/wp\/v2\/categories?post=297"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.espol.edu.ec\/programando\/wp-json\/wp\/v2\/tags?post=297"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}