{"id":671,"date":"2024-07-30T19:56:08","date_gmt":"2024-07-30T17:56:08","guid":{"rendered":"https:\/\/blog.sutilweb.eu\/?page_id=671"},"modified":"2024-07-30T19:56:08","modified_gmt":"2024-07-30T17:56:08","slug":"005-break-y-continue","status":"publish","type":"page","link":"https:\/\/sutilweb.eu\/index.php\/lenguajes\/javascript\/javascript-practico\/04-estructuras-de-control-en-javascript\/005-break-y-continue\/","title":{"rendered":"005. Break y continue"},"content":{"rendered":"\n<p>En cap\u00edtulos anteriores ya hab\u00edamos visto el uso de <strong>estructuras de control<\/strong> como <strong>condicionales<\/strong> y <strong>ciclos<\/strong>, y cuando est\u00e1bamos trabajando con el uso de la instrucci\u00f3n <strong><em>switch&#8230; case<\/em><\/strong>, en cada uno de los casos ten\u00edamos para romper dichos casos, la palabra <strong><em>break<\/em><\/strong>, que lo que&nbsp; hace es salir de la estructura (en este caso el <strong><em>switch<\/em><\/strong>) para no estar leyendo todos los casos.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>En este cap\u00edtulo vamos a ver para que se utilizan las palabras <strong><em>break<\/em> <\/strong>y <strong><em>continue<\/em><\/strong>. Estas palabras nos ayudan a controlar m\u00e1s el flujo de nuestras <strong>estructuras de control<\/strong>, sobre todo si estamos en un <strong>bucle<\/strong> y a lo mejor, necesitemos llegar a cierto n\u00famero de repeticiones, y cuando lo hagamos, salirnos de la ejecuci\u00f3n del c\u00f3digo o, a veces, s\u00f3lo saltarnos ciertas vueltas a un bucle en base a un patr\u00f3n. Por ejm, que de una lista s\u00f3lo queramos imprimir los n\u00fameros pares o impares, para ello nos puede ayudar <strong><em>break<\/em> <\/strong>o <strong><em>continue<\/em><\/strong>.<\/p>\n\n\n\n<p><strong>Ejm<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">  &nbsp; &nbsp; &nbsp; const numeros = [1,2,3,4,5,6,7,8,9,10];\n\n&nbsp; &nbsp; &nbsp; &nbsp; for (let i = 0; i &lt; numeros.length; i++) {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (i === 5) {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(numeros[i]);\n&nbsp; &nbsp; &nbsp; &nbsp; }<\/pre>\n\n\n\n<p>Lo que hace el ejm de arriba es que cuando llega al 5, detiene la ejecuci\u00f3n del c\u00f3digo y se sale del bucle.<\/p>\n\n\n\n<p><strong>Ejm<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp; &nbsp; &nbsp; &nbsp; const numeros = [1,2,3,4,5,6,7,8,9,10];\n\n&nbsp; &nbsp; &nbsp; &nbsp; for (let i = 0; i &lt; numeros.length; i++) {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (i === 5) {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(numeros[i]);\n&nbsp; &nbsp; &nbsp; &nbsp; }<\/pre>\n\n\n\n<p>Mientras que con la palabra reservada <strong><em>continue<\/em> <\/strong>lo que hace&nbsp; es saltarse esa condici\u00f3n y contin\u00faa ejecutando el c\u00f3digo del bucle.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>En cap\u00edtulos anteriores ya hab\u00edamos visto el uso de estructuras de control como condicionales y ciclos, y cuando est\u00e1bamos trabajando [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":658,"menu_order":4,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"class_list":["post-671","page","type-page","status-publish","hentry"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"Sutil Web","author_link":"https:\/\/sutilweb.eu\/index.php\/author\/sutilweb\/"},"uagb_comment_info":0,"uagb_excerpt":"En cap\u00edtulos anteriores ya hab\u00edamos visto el uso de estructuras de control como condicionales y ciclos, y cuando est\u00e1bamos trabajando [&hellip;]","_links":{"self":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/671","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/comments?post=671"}],"version-history":[{"count":2,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/671\/revisions"}],"predecessor-version":[{"id":673,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/671\/revisions\/673"}],"up":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/658"}],"wp:attachment":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/media?parent=671"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}