{"id":1949,"date":"2024-11-07T08:00:00","date_gmt":"2024-11-07T07:00:00","guid":{"rendered":"https:\/\/www.fuhselab.de\/?p=1949"},"modified":"2024-10-03T09:08:01","modified_gmt":"2024-10-03T07:08:01","slug":"c-und-die-do-while-schleife","status":"publish","type":"post","link":"https:\/\/www.fuhselab.de\/index.php\/2024\/11\/07\/c-und-die-do-while-schleife\/","title":{"rendered":"C++ und die do-while-Schleife"},"content":{"rendered":"\n<p><br>In diesem C++ Webartikel geht es um die do-while-Schleife&#8230;<\/p>\n\n\n\n<!--more-->\n\n\n\n<p><br><strong>Praxisbeispiel 1 f\u00fcr die do-while-Schleife:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream>\nint main() {\n\nunsigned i = 1;\nunsigned benutzer = 0;\nstd::cin >> benutzer;\n\ndo\n{\nstd::cout &lt;&lt; i &lt;&lt; std::endl;\n++i;\n}\n\nwhile (i &lt;= benutzer);\n\n}<\/code><\/pre>\n\n\n\n<p><br>Auch diesen Code k\u00f6nnte man etwas optimieren und dem Benutzer auch Anweisungen in das Terminal schreiben, damit er wei\u00df was hier gemacht werden soll. Der angepasste Code k\u00f6nnte so aussehen\u2026:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream>\nint main() {\n\nunsigned i = 1;\nunsigned benutzer = 0;\nstd::cout &lt;&lt;\"Bitte geben Sie eine Zahl ein und dr\u00fccken dann Enter\"&lt;&lt; std::endl;\nstd::cin >> benutzer;\n\ndo\n{\nstd::cout &lt;&lt; i &lt;&lt; std::endl;\n++i;\n}\nwhile (i &lt;= benutzer);\n}<\/code><\/pre>\n\n\n\n<p><br><strong>Praxisbeispiel 2 f\u00fcr die do-while-Schleife:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream>\n\nint main()\n{\nint secretNumber = 42; \/\/ die geheime zahl\nint attempts = 0;\nbool correctGuess = false;\n\ndo {\nstd::cout &lt;&lt; \"Wie lautet die geheime Zahl zwischen 1 und 100? \";\nint userGuess;\nstd::cin >> userGuess;\n\nif (userGuess == secretNumber)\n{\nstd::cout &lt;&lt; \" Gl\u00fcckwunsch! Du hast die geheime Zahl erraten in \" &lt;&lt; attempts + 1 &lt;&lt; \" Versuch(en).\" &lt;&lt; std::endl;\ncorrectGuess = true; \/\/ exit the loop\n}\n\nelse if (userGuess &lt; secretNumber)\n{\nstd::cout &lt;&lt; \"Die von dir eingegebene Zahl ist zu niedrig. Versuch es nochmal.\" &lt;&lt; std::endl;\n}\n\nelse\n{\nstd::cout &lt;&lt; \"Die von dir eingegebene Zahl ist zu hoch. Versuch es nochmal.\" &lt;&lt; std::endl;\n}\n\nattempts++; \/\/ increment attempt counter\n}\n\nwhile (!correctGuess);\nreturn 0;\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In diesem C++ Webartikel geht es um die do-while-Schleife&#8230;<\/p>\n","protected":false},"author":1,"featured_media":543,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33],"tags":[32],"class_list":["post-1949","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-coding-club","tag-coding-club-peine"],"_links":{"self":[{"href":"https:\/\/www.fuhselab.de\/index.php\/wp-json\/wp\/v2\/posts\/1949","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.fuhselab.de\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fuhselab.de\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fuhselab.de\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fuhselab.de\/index.php\/wp-json\/wp\/v2\/comments?post=1949"}],"version-history":[{"count":1,"href":"https:\/\/www.fuhselab.de\/index.php\/wp-json\/wp\/v2\/posts\/1949\/revisions"}],"predecessor-version":[{"id":1950,"href":"https:\/\/www.fuhselab.de\/index.php\/wp-json\/wp\/v2\/posts\/1949\/revisions\/1950"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.fuhselab.de\/index.php\/wp-json\/wp\/v2\/media\/543"}],"wp:attachment":[{"href":"https:\/\/www.fuhselab.de\/index.php\/wp-json\/wp\/v2\/media?parent=1949"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fuhselab.de\/index.php\/wp-json\/wp\/v2\/categories?post=1949"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fuhselab.de\/index.php\/wp-json\/wp\/v2\/tags?post=1949"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}