{"id":14528,"date":"2025-04-16T22:08:16","date_gmt":"2025-04-16T15:08:16","guid":{"rendered":"https:\/\/www.krujeen.com\/?p=14528"},"modified":"2025-04-16T22:08:16","modified_gmt":"2025-04-16T15:08:16","slug":"%e0%b8%84%e0%b8%b3%e0%b8%aa%e0%b8%b1%e0%b9%88%e0%b8%87-loop-while-%e0%b9%83%e0%b8%8a%e0%b9%89%e0%b8%87%e0%b8%b2%e0%b8%99%e0%b9%83%e0%b8%99-c","status":"publish","type":"post","link":"https:\/\/www.krujeen.com\/?p=14528","title":{"rendered":"\u0e04\u0e33\u0e2a\u0e31\u0e48\u0e07 loop while \u0e43\u0e0a\u0e49\u0e07\u0e32\u0e19\u0e43\u0e19 C++"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"683\" src=\"https:\/\/www.krujeen.com\/wp-content\/uploads\/2025\/04\/ChatGPT-Image-Apr-16-2025-10_07_12-PM-1024x683.png\" alt=\"\" class=\"wp-image-14529\" srcset=\"https:\/\/www.krujeen.com\/wp-content\/uploads\/2025\/04\/ChatGPT-Image-Apr-16-2025-10_07_12-PM-1024x683.png 1024w, https:\/\/www.krujeen.com\/wp-content\/uploads\/2025\/04\/ChatGPT-Image-Apr-16-2025-10_07_12-PM-300x200.png 300w, https:\/\/www.krujeen.com\/wp-content\/uploads\/2025\/04\/ChatGPT-Image-Apr-16-2025-10_07_12-PM-768x512.png 768w, https:\/\/www.krujeen.com\/wp-content\/uploads\/2025\/04\/ChatGPT-Image-Apr-16-2025-10_07_12-PM.png 1536w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>1. while loop \u0e1e\u0e37\u0e49\u0e19\u0e10\u0e32\u0e19<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">while (\u0e40\u0e07\u0e37\u0e48\u0e2d\u0e19\u0e44\u0e02) {\n    \/\/ \u0e04\u0e33\u0e2a\u0e31\u0e48\u0e07\u0e17\u0e35\u0e48\u0e08\u0e30\u0e17\u0e33\u0e0b\u0e49\u0e33\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u0e15\u0e31\u0e27\u0e2d\u0e22\u0e48\u0e32\u0e07:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">int i = 0;\nwhile (i &lt; 5) {\n    cout &lt;&lt; i &lt;&lt; \" \";\n    i++;\n}\n\/\/ \u0e1c\u0e25\u0e25\u0e31\u0e1e\u0e18\u0e4c: 0 1 2 3 4<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>2. do-while loop<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">do {\n    \/\/ \u0e04\u0e33\u0e2a\u0e31\u0e48\u0e07\u0e17\u0e35\u0e48\u0e08\u0e30\u0e17\u0e33\u0e0b\u0e49\u0e33\n} while (\u0e40\u0e07\u0e37\u0e48\u0e2d\u0e19\u0e44\u0e02);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u0e15\u0e31\u0e27\u0e2d\u0e22\u0e48\u0e32\u0e07:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">int i = 0;\ndo {\n    cout &lt;&lt; i &lt;&lt; \" \";\n    i++;\n} while (i &lt; 5);\n\/\/ \u0e1c\u0e25\u0e25\u0e31\u0e1e\u0e18\u0e4c: 0 1 2 3 4<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>3. while loop \u0e01\u0e31\u0e1a\u0e01\u0e32\u0e23\u0e2d\u0e48\u0e32\u0e19\u0e02\u0e49\u0e2d\u0e21\u0e39\u0e25<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">string input;\nwhile (cin >> input) {\n    cout &lt;&lt; \"\u0e04\u0e38\u0e13\u0e1b\u0e49\u0e2d\u0e19: \" &lt;&lt; input &lt;&lt; endl;\n    if (input == \"exit\") break;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>4. while loop \u0e44\u0e21\u0e48\u0e21\u0e35\u0e17\u0e35\u0e48\u0e2a\u0e34\u0e49\u0e19\u0e2a\u0e38\u0e14 (Infinite loop)<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">while (true) {\n    \/\/ \u0e04\u0e33\u0e2a\u0e31\u0e48\u0e07\u0e17\u0e35\u0e48\u0e08\u0e30\u0e17\u0e33\u0e0b\u0e49\u0e33\u0e44\u0e1b\u0e40\u0e23\u0e37\u0e48\u0e2d\u0e22\u0e46\n    \/\/ \u0e15\u0e49\u0e2d\u0e07\u0e21\u0e35\u0e40\u0e07\u0e37\u0e48\u0e2d\u0e19\u0e44\u0e02\u0e01\u0e32\u0e23\u0e2b\u0e22\u0e38\u0e14 (break) \u0e20\u0e32\u0e22\u0e43\u0e19\u0e25\u0e39\u0e1b\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>5. while loop \u0e01\u0e31\u0e1a multiple conditions<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">int x = 0, y = 10;\nwhile (x &lt; 5 &amp;&amp; y > 0) {\n    cout &lt;&lt; x &lt;&lt; \":\" &lt;&lt; y &lt;&lt; \" \";\n    x++;\n    y--;\n}\n\/\/ \u0e1c\u0e25\u0e25\u0e31\u0e1e\u0e18\u0e4c: 0:10 1:9 2:8 3:7 4:6<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>6. while loop \u0e01\u0e31\u0e1a flag variable<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">bool keepRunning = true;\nwhile (keepRunning) {\n    \/\/ \u0e17\u0e33\u0e07\u0e32\u0e19\u0e1a\u0e32\u0e07\u0e2d\u0e22\u0e48\u0e32\u0e07\n    \/\/ ...\n    if (someCondition) {\n        keepRunning = false;  \/\/ \u0e2b\u0e22\u0e38\u0e14 loop \u0e40\u0e21\u0e37\u0e48\u0e2d\u0e40\u0e07\u0e37\u0e48\u0e2d\u0e19\u0e44\u0e02\u0e40\u0e1b\u0e47\u0e19\u0e08\u0e23\u0e34\u0e07\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>7. Nested while loops<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">int i = 0;\nwhile (i &lt; 3) {\n    int j = 0;\n    while (j &lt; 3) {\n        cout &lt;&lt; \"(\" &lt;&lt; i &lt;&lt; \",\" &lt;&lt; j &lt;&lt; \") \";\n        j++;\n    }\n    cout &lt;&lt; endl;\n    i++;\n}\n\/\/ \u0e1c\u0e25\u0e25\u0e31\u0e1e\u0e18\u0e4c:\n\/\/ (0,0) (0,1) (0,2)\n\/\/ (1,0) (1,1) (1,2)\n\/\/ (2,0) (2,1) (2,2)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u0e2b\u0e21\u0e32\u0e22\u0e40\u0e2b\u0e15\u0e38:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>while loop \u0e15\u0e23\u0e27\u0e08\u0e2a\u0e2d\u0e1a\u0e40\u0e07\u0e37\u0e48\u0e2d\u0e19\u0e44\u0e02\u0e01\u0e48\u0e2d\u0e19\u0e17\u0e33\u0e07\u0e32\u0e19\u0e43\u0e19\u0e25\u0e39\u0e1b<\/li>\n\n\n\n<li>do-while loop \u0e17\u0e33\u0e07\u0e32\u0e19\u0e43\u0e19\u0e25\u0e39\u0e1b\u0e2d\u0e22\u0e48\u0e32\u0e07\u0e19\u0e49\u0e2d\u0e22\u0e2b\u0e19\u0e36\u0e48\u0e07\u0e04\u0e23\u0e31\u0e49\u0e07\u0e01\u0e48\u0e2d\u0e19\u0e15\u0e23\u0e27\u0e08\u0e2a\u0e2d\u0e1a\u0e40\u0e07\u0e37\u0e48\u0e2d\u0e19\u0e44\u0e02<\/li>\n\n\n\n<li>\u0e2a\u0e32\u0e21\u0e32\u0e23\u0e16\u0e43\u0e0a\u0e49 <code>break<\/code> \u0e40\u0e1e\u0e37\u0e48\u0e2d\u0e2d\u0e2d\u0e01\u0e08\u0e32\u0e01 loop \u0e01\u0e48\u0e2d\u0e19\u0e04\u0e23\u0e1a\u0e23\u0e2d\u0e1a<\/li>\n\n\n\n<li>\u0e2a\u0e32\u0e21\u0e32\u0e23\u0e16\u0e43\u0e0a\u0e49 <code>continue<\/code> \u0e40\u0e1e\u0e37\u0e48\u0e2d\u0e02\u0e49\u0e32\u0e21\u0e01\u0e32\u0e23\u0e17\u0e33\u0e07\u0e32\u0e19\u0e43\u0e19\u0e23\u0e2d\u0e1a\u0e1b\u0e31\u0e08\u0e08\u0e38\u0e1a\u0e31\u0e19\u0e41\u0e25\u0e30\u0e44\u0e1b\u0e22\u0e31\u0e07\u0e23\u0e2d\u0e1a\u0e16\u0e31\u0e14\u0e44\u0e1b<\/li>\n\n\n\n<li>\u0e23\u0e30\u0e27\u0e31\u0e07\u0e01\u0e32\u0e23\u0e43\u0e0a\u0e49 while loop \u0e41\u0e1a\u0e1a\u0e44\u0e21\u0e48\u0e21\u0e35\u0e17\u0e35\u0e48\u0e2a\u0e34\u0e49\u0e19\u0e2a\u0e38\u0e14 (infinite loop) \u0e42\u0e14\u0e22\u0e44\u0e21\u0e48\u0e15\u0e31\u0e49\u0e07\u0e43\u0e08<\/li>\n\n\n\n<li>\u0e04\u0e27\u0e23\u0e21\u0e35\u0e01\u0e32\u0e23\u0e40\u0e1b\u0e25\u0e35\u0e48\u0e22\u0e19\u0e41\u0e1b\u0e25\u0e07\u0e04\u0e48\u0e32\u0e17\u0e35\u0e48\u0e43\u0e0a\u0e49\u0e43\u0e19\u0e40\u0e07\u0e37\u0e48\u0e2d\u0e19\u0e44\u0e02\u0e20\u0e32\u0e22\u0e43\u0e19 loop body \u0e40\u0e1e\u0e37\u0e48\u0e2d\u0e1b\u0e49\u0e2d\u0e07\u0e01\u0e31\u0e19 infinite loop<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>1. while loop \u0e1e\u0e37\u0e49\u0e19\u0e10\u0e32\u0e19 \u0e15\u0e31\u0e27\u0e2d\u0e22\u0e48\u0e32\u0e07: 2. do-while loop \u0e15\u0e31\u0e27\u0e2d\u0e22\u0e48 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":14529,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[155,156],"tags":[160],"class_list":["post-14528","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programing","category-c_language","tag-c"],"_links":{"self":[{"href":"https:\/\/www.krujeen.com\/index.php?rest_route=\/wp\/v2\/posts\/14528","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.krujeen.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.krujeen.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.krujeen.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.krujeen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=14528"}],"version-history":[{"count":1,"href":"https:\/\/www.krujeen.com\/index.php?rest_route=\/wp\/v2\/posts\/14528\/revisions"}],"predecessor-version":[{"id":14530,"href":"https:\/\/www.krujeen.com\/index.php?rest_route=\/wp\/v2\/posts\/14528\/revisions\/14530"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.krujeen.com\/index.php?rest_route=\/wp\/v2\/media\/14529"}],"wp:attachment":[{"href":"https:\/\/www.krujeen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=14528"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.krujeen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=14528"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.krujeen.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=14528"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}