<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[While - Sauber oder Unsauber]]></title><description><![CDATA[<p>Ich hatte unsauber programmiert, die Klammern nach while vergessen, aber das Programm funktionierte</p>
<p>so war es unsauber:</p>
<pre><code>#include &lt;iostream&gt;
using namespace std;

// Das Programm entwickelt die Zahlen nach der Collatz-Metode, also entweder durch 2 teilbar oder mit 3 multiplizieren, bis man auf 1 kommt.

int mulz; //Multiplikationszähler
int divz; //Divisionszähler
int zahl;
int main ()
 {
 int ix;

		cout&lt;&lt; &quot;Geben Sie eine Zahl ein: &quot;;
		cin&gt;&gt;ix;
		zahl = ix;
		while (ix&gt;1)
			if (ix%2==0)
			{
			ix=ix/2;
			divz = divz + 1;
			cout&lt;&lt;&quot;ix = &quot;&lt;&lt;ix&lt;&lt;&quot; ist Ergeb. der &quot; &lt;&lt;divz&lt;&lt; &quot;. Div. \n&quot;;			
			}
			else
			{
			ix=ix*3+1;
			mulz = mulz + 1;
			cout&lt;&lt;&quot;ix = &quot;&lt;&lt;ix&lt;&lt;&quot; Multipl: &quot; &lt;&lt;mulz&lt;&lt; &quot; \n&quot;;
			}
			return ix;

}
</code></pre>
<p>so habe ich verschlechtbessert: Nur die While klammern und den Schlussteil.</p>
<pre><code>#include &lt;iostream&gt;
using namespace std;

// Das Programm entwickelt die Zahlen nach der Collatz-Metode, also entweder durch 2 teilbar oder mit 3 multiplizieren, bis man auf 1 kommt.

int mulz; //Multiplikationszähler
int divz; //Divisionszähler
int zahl;
int main ()
 {
 int ix;

		cout&lt;&lt; &quot;Geben Sie eine Zahl ein: &quot;;
		cin&gt;&gt;ix;
		zahl = ix;
		while (ix&gt;1)
		{
			if (ix%2==0)
			{
			ix=ix/2;
			divz = divz + 1;
			cout&lt;&lt;&quot;ix = &quot;&lt;&lt;ix&lt;&lt;&quot; ist Ergeb. der &quot; &lt;&lt;divz&lt;&lt; &quot;. Div. \n&quot;;			
			}
			else
			{
			ix=ix*3+1;
			mulz = mulz + 1;
			cout&lt;&lt;&quot;ix = &quot;&lt;&lt;ix&lt;&lt;&quot; Multipl: &quot; &lt;&lt;mulz&lt;&lt; &quot; \n&quot;;
			}
			return ix;
		}
	cout&lt;&lt; &quot;um die Zahl &quot;&lt;&lt;zahl&lt;&lt; &quot;auf 1 zurückzuführen, sind nach der Collatz-Methode &quot; &lt;&lt;divz+mulz&lt;&lt; &quot;OPERATIONEN nötig&quot;;
cin.get();
}
</code></pre>
<p>Was habe ich falsch gemacht? Nach While sind die Klammern doch zwingend? Und den Schlussatz wollte ich auch rein haben.</p>
<p>Danke für die Tips</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/232652/while-sauber-oder-unsauber</link><generator>RSS for Node</generator><lastBuildDate>Fri, 25 Sep 2026 07:51:46 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/232652.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 25 Jan 2009 12:25:06 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to While - Sauber oder Unsauber on Sun, 25 Jan 2009 12:26:12 GMT]]></title><description><![CDATA[<p>Ich hatte unsauber programmiert, die Klammern nach while vergessen, aber das Programm funktionierte</p>
<p>so war es unsauber:</p>
<pre><code>#include &lt;iostream&gt;
using namespace std;

// Das Programm entwickelt die Zahlen nach der Collatz-Metode, also entweder durch 2 teilbar oder mit 3 multiplizieren, bis man auf 1 kommt.

int mulz; //Multiplikationszähler
int divz; //Divisionszähler
int zahl;
int main ()
 {
 int ix;

		cout&lt;&lt; &quot;Geben Sie eine Zahl ein: &quot;;
		cin&gt;&gt;ix;
		zahl = ix;
		while (ix&gt;1)
			if (ix%2==0)
			{
			ix=ix/2;
			divz = divz + 1;
			cout&lt;&lt;&quot;ix = &quot;&lt;&lt;ix&lt;&lt;&quot; ist Ergeb. der &quot; &lt;&lt;divz&lt;&lt; &quot;. Div. \n&quot;;			
			}
			else
			{
			ix=ix*3+1;
			mulz = mulz + 1;
			cout&lt;&lt;&quot;ix = &quot;&lt;&lt;ix&lt;&lt;&quot; Multipl: &quot; &lt;&lt;mulz&lt;&lt; &quot; \n&quot;;
			}
			return ix;

}
</code></pre>
<p>so habe ich verschlechtbessert: Nur die While klammern und den Schlussteil.</p>
<pre><code>#include &lt;iostream&gt;
using namespace std;

// Das Programm entwickelt die Zahlen nach der Collatz-Metode, also entweder durch 2 teilbar oder mit 3 multiplizieren, bis man auf 1 kommt.

int mulz; //Multiplikationszähler
int divz; //Divisionszähler
int zahl;
int main ()
 {
 int ix;

		cout&lt;&lt; &quot;Geben Sie eine Zahl ein: &quot;;
		cin&gt;&gt;ix;
		zahl = ix;
		while (ix&gt;1)
		{
			if (ix%2==0)
			{
			ix=ix/2;
			divz = divz + 1;
			cout&lt;&lt;&quot;ix = &quot;&lt;&lt;ix&lt;&lt;&quot; ist Ergeb. der &quot; &lt;&lt;divz&lt;&lt; &quot;. Div. \n&quot;;			
			}
			else
			{
			ix=ix*3+1;
			mulz = mulz + 1;
			cout&lt;&lt;&quot;ix = &quot;&lt;&lt;ix&lt;&lt;&quot; Multipl: &quot; &lt;&lt;mulz&lt;&lt; &quot; \n&quot;;
			}
			return ix;
		}
	cout&lt;&lt; &quot;um die Zahl &quot;&lt;&lt;zahl&lt;&lt; &quot;auf 1 zurückzuführen, sind nach der Collatz-Methode &quot; &lt;&lt;divz+mulz&lt;&lt; &quot;OPERATIONEN nötig&quot;;
cin.get();
}
</code></pre>
<p>Was habe ich falsch gemacht? Nach While sind die Klammern doch zwingend? Und den Schlussatz wollte ich auch rein haben.</p>
<p>Danke für die Tips</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1651944</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1651944</guid><dc:creator><![CDATA[Bekell]]></dc:creator><pubDate>Sun, 25 Jan 2009 12:26:12 GMT</pubDate></item><item><title><![CDATA[Reply to While - Sauber oder Unsauber on Sun, 25 Jan 2009 12:35:12 GMT]]></title><description><![CDATA[<p>Der Unterschied ist, dass <code>return ix</code> beim ersten Ausschnitt am Ende ausgegeben wird aber im zweiten bereits nach dem ersten Schleifedurchlauf.</p>
<p>erster Ausschnitt mit Klammern (ohne Änderung des Ablaufes):</p>
<pre><code class="language-cpp">while (ix&gt;1)
	{
		if (ix%2==0)
		{
			ix=ix/2;
			divz = divz + 1;
			cout&lt;&lt;&quot;ix = &quot;&lt;&lt;ix&lt;&lt;&quot; ist Ergeb. der &quot; &lt;&lt;divz&lt;&lt; &quot;. Div. \n&quot;;           
		}
		else
		{
			ix=ix*3+1;
			mulz = mulz + 1;
			cout&lt;&lt;&quot;ix = &quot;&lt;&lt;ix&lt;&lt;&quot; Multipl: &quot; &lt;&lt;mulz&lt;&lt; &quot; \n&quot;;
		}
	}

	return ix;
</code></pre>
<p>Wie du siehst ist hier das return nicht in der Schleife drin, wie bei deinem zweitem Ausschnitt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1651954</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1651954</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Sun, 25 Jan 2009 12:35:12 GMT</pubDate></item><item><title><![CDATA[Reply to While - Sauber oder Unsauber on Sun, 25 Jan 2009 12:39:15 GMT]]></title><description><![CDATA[<p>Auf jeden Fall erst mal danke...</p>
<p>Und wie bekomme ich jetzt noch den Schlussatz hin?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1651957</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1651957</guid><dc:creator><![CDATA[Bekell]]></dc:creator><pubDate>Sun, 25 Jan 2009 12:39:15 GMT</pubDate></item><item><title><![CDATA[Reply to While - Sauber oder Unsauber on Sun, 25 Jan 2009 12:44:02 GMT]]></title><description><![CDATA[<p>Du gibst ihn vor dem return aus..</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1651961</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1651961</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Sun, 25 Jan 2009 12:44:02 GMT</pubDate></item><item><title><![CDATA[Reply to While - Sauber oder Unsauber on Sun, 25 Jan 2009 13:36:41 GMT]]></title><description><![CDATA[<p>bei while, for, if, usw...<br />
sind die geschweiften klammern nicht pflicht</p>
<p>wenn du eine schleife schreisbt dann wird solange der erste befehl nach der schleife wiederholt, wenn man mehrere sachen machen will braucht man die klammern</p>
<pre><code class="language-cpp">for(int i = 0; i &lt; 10; ++i)
   cout &lt;&lt; i &lt;&lt; &quot; und &quot; &lt;&lt; i*i &lt;&lt; endl;   // wird 10mal ausführt
cout &lt;&lt; &quot;schleifen ende&quot; &lt;&lt; endl; // nur einmal ausgeführt

for(int i = 0; i &lt; 10; ++i)
{
   cout &lt;&lt; i &lt;&lt; &quot; und &quot; &lt;&lt; i*i &lt;&lt; endl;   // wird 10mal ausführt
   cout &lt;&lt; &quot;schleifen ende&quot; &lt;&lt; endl; // das auch
}

while (true)
   if ( !false )
   {
       cout &lt;&lt; &quot;lol&quot; &lt;&lt; endl;
       cout &lt;&lt; &quot;lol2&quot; &lt;&lt; endl;
   }
   else
   {
       cout &lt;&lt; &quot;Else&quot; &lt;&lt; endl;
   }
</code></pre>
<p>das while führt trotzdem das ganze if und else zeugs aus weils als ein zusammenhängender befehl gilt</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1651984</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1651984</guid><dc:creator><![CDATA[Skym0sh0]]></dc:creator><pubDate>Sun, 25 Jan 2009 13:36:41 GMT</pubDate></item></channel></rss>