<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Coder-Blog &#187; if-Anweisung</title>
	<atom:link href="http://coder-blog.de/tag/if-anweisung/feed/" rel="self" type="application/rss+xml" />
	<link>http://coder-blog.de</link>
	<description>Computer, Informatik, Internet und Programmieren</description>
	<lastBuildDate>Wed, 23 Sep 2009 09:25:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Java-Tutorial: Fallunterscheidungen (Bedingte Anweisungen)</title>
		<link>http://coder-blog.de/java-tutorial-fallunterscheidungen-bedingte-anweisungen/</link>
		<comments>http://coder-blog.de/java-tutorial-fallunterscheidungen-bedingte-anweisungen/#comments</comments>
		<pubDate>Sat, 28 Mar 2009 23:28:04 +0000</pubDate>
		<dc:creator>Eugen</dc:creator>
				<category><![CDATA[Java-Tutorials]]></category>
		<category><![CDATA[bedingte-anweisungen]]></category>
		<category><![CDATA[else-Anweisung]]></category>
		<category><![CDATA[else-if-anweisung]]></category>
		<category><![CDATA[fallunterscheidungen]]></category>
		<category><![CDATA[if-Anweisung]]></category>
		<category><![CDATA[java-arrays]]></category>
		<category><![CDATA[java-lernen]]></category>
		<category><![CDATA[java-tutorial]]></category>
		<category><![CDATA[objektorientierte programmierung]]></category>
		<category><![CDATA[switch-anweisung]]></category>
		<category><![CDATA[variablen]]></category>
		<category><![CDATA[verschachtelte-fallunterscheidung]]></category>

		<guid isPermaLink="false">http://coder-blog.de/?p=103</guid>
		<description><![CDATA[Dieses Java-Tutoral bringt uns Fallunterscheidungen oder auch bedingte Anweisungen näher]]></description>
			<content:encoded><![CDATA[<p>Wie in anderen Programmiersprachen, so bietet natürlich auch Java die Möglichkeit der <strong>Fallunterscheidung</strong> zu nutzen, die auch in mancher Literatur als <strong>bedingte Anweisung</strong> beschrieben wird.<br />
Ohne Fallunterscheidungen wäre ein Programm ziemlich doof, denn es könnte nicht flexibel Programmteile ausführen, die nur unter einer <strong>bestimmten Bedingung</strong> aufgerufen werden soll. Dazu bietet Java die <strong>if-</strong> bzw. <strong>if/else-Anweisungen</strong> und die <strong>switch-Anweisung</strong>.</p>
<p><strong>Bedingte Anweisungen</strong> sind gar nicht so schwer – also fangen wir gleich mit der <strong>if-Anweisung</strong> an:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span> meinAlter <span style="color: #339933;">=</span> <span style="color: #cc66cc;">55</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">int</span> rentenalter <span style="color: #339933;">=</span> <span style="color: #cc66cc;">66</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>meinAlter <span style="color: #339933;">!=</span> rentenalter<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Rentenalter nicht erreicht – weiterackern!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Was macht dieses Programm nun? Wir setzen uns zwei Variablen mit den Werten 55 und 66. Die Variable meinAlter soll das jetzige Alter repräsentieren. Die Zweite enthält das gegenwärtige Rentenalter 66. Darunter sehen wir die <strong>if-Anweisung</strong>, die – wie jede if-Anweisung – mit dem Schlüsselwort if eingeleitet wird und in Klammern einen <strong>boolschen Wert</strong> enthält.<br />
<span id="more-103"></span><br />
Hä, ich sehe aber gar keinen boolschen Wert?! Auf den ersten Blick erkennt man dies auch nicht sofort, doch der <strong>Operator !=</strong> vergleicht zwei Werte auf <strong>Nicht-Gleichheit</strong> und liefert uns einen boolschen Wert als Ergebnis. Als Gegenstück dazu gibt es den <strong>== Operator</strong>, der zwei <strong>Werte auf Gleichheit</strong> hin überprüft. In unserem Fall würde es bedeuten, dass wir den Wert der Variablen meinAlter mit dem Wert der Variablen rentenalter vergleichen. Wir fragen uns also: &#8220;Ist der Wert der Variablen meinAlter ungleich des Wertes der Variablen rentenalter?&#8221;. Als Ergebnis bekommen wir von Java ein true – also es ist wahr, dass beide ungleich sind.<br />
Da diese Bedingung nun wahr ist, wir der Code, der sich unterhalb der <strong>if-Anweisung</strong> in den geschweiften Klammern befindet ausgeführt.<br />
In normaler Sprache ausgedrückt, könnte man diese Bedingung auch schreiben als: &#8220;Wenn meinAlter ungleich rentenalter ist, dann gib mir aus &#8216;Rentenalter nicht erreicht – weiterackern!&#8217;&#8221;.</p>
<p>Natürlich ist diese Bedingung nicht gut programmiert, denn wenn meinAlter einen Wert enthält, der größer als 66 ist, wir die Meldung ebenfalls ausgegeben. Stattdessen sollten wir den <strong>< Operator</strong> benutzen, der wie in der Mathematik prüft, ob der erste Wert kleiner dem zweiten Wert ist.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span> meinAlter <span style="color: #339933;">=</span> <span style="color: #cc66cc;">55</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">int</span> rentenalter <span style="color: #339933;">=</span> <span style="color: #cc66cc;">66</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>meinAlter <span style="color: #339933;">&lt;</span> rentenalter<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Rentenalter nicht erreicht – weiterackern!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>So ist der Code schon viel besser. Was ist, wenn wir aber auch etwas ausführen möchten, wenn die if-Anweisung nicht zutrifft? Dafür gibt es eben die<strong> if-/else-Anweisung</strong>. Fügen wir nun unserem if- einen <strong>else-Zweig</strong> hinzu.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span> meinAlter <span style="color: #339933;">=</span> <span style="color: #cc66cc;">55</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">int</span> rentenalter <span style="color: #339933;">=</span> <span style="color: #cc66cc;">66</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>meinAlter <span style="color: #339933;">&lt;</span> rentenalter<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Rentenalter nicht erreicht – weiterackern!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">else</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Yay, jetzt ist erstmal ausruhen angesagt!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Ins else wird immer nur dann gesprungen, wenn das if nicht zutrifft. Bei uns würde eben die if-Anweisung zuschlagen, da unsere meinAlter-Variable kleiner ist als die rentenalter.<br />
Ändern wir nun meinAlter zum Beispiel in 80, so würde die if-Anweisung nicht mehr zutreffen und dem Else-Zweig Beachtung geschenkt werden.</p>
<p>Es sind auch <strong>verschachtelte Fallunterscheidungen</strong> möglich:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span> meinAlter <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1000</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">int</span> rentenalter <span style="color: #339933;">=</span> <span style="color: #cc66cc;">66</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>meinAlter <span style="color: #339933;">&lt;</span> rentenalter<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Rentenalter nicht erreicht – weiterackern!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">else</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>meinAlter <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">969</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
      <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Na, willst du Methusalem Konkurrenz machen?&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">else</span>
  <span style="color: #009900;">&#123;</span>
      <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Yay, jetzt ist erstmal ausruhen angesagt!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Wir haben meinAlter auf 1000 geändert. Da jetzt die Bedingung der <strong>if-Anweisung</strong> nicht zutrifft, wird in die <strong>else-Anweisung</strong> gesprungen. Darin befindet sich nun ebenfalls eine <strong>if-/else-Anweisung</strong>. Innerhalb des else-Zweiges, wird jetzt im if geprüft, ob meinAlter größer oder gleich 969 ist. Dies trifft nun zu und es wir die entsprechende Nachricht ausgegeben.<br />
Für solch einen Fall bietet uns Java ein besonderes Konstrukt. Wir hätten also auch schreiben können:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span> meinAlter <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1000</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">int</span> rentenalter <span style="color: #339933;">=</span> <span style="color: #cc66cc;">66</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>meinAlter <span style="color: #339933;">&lt;</span> rentenalter<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Rentenalter nicht erreicht – weiterackern!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>meinAlter <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">969</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
      <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Na, willst du Methusalem Konkurrenz machen?&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">else</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Yay, jetzt ist erstmal ausruhen angesagt!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Die <strong>else-if-Anweisung</strong> ist eine Kombination der <strong>if –</strong> und <strong>else-Anweisung</strong>. Denn trifft nun der <strong>if-Fall</strong> bei uns nicht zu, dann wird der <strong>else-if-Fall</strong> überprüft. Trifft dieser auch nicht zu, wird <strong>else</strong> ausgeführt. Als Anmerkung sei noch gesagt, dass es uns möglich ist, so viele else-if-Fälle zu benutzen wie wir wollen.</p>
<p>Neben der <strong>if-Anweisung</strong> und der <strong>if-/else-Anweisung</strong> findet man die <strong>switch-Anweisung</strong>:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span> meinAlter <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1000</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">switch</span><span style="color: #009900;">&#40;</span>meinAlter<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">:</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Na, heute Windeln schon gewechselt bekommen?&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #cc66cc;">66</span><span style="color: #339933;">:</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Du darfst in Rente gehen!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #cc66cc;">969</span><span style="color: #339933;">:</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Methusalem lässt grüßen&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #cc66cc;">4723</span><span style="color: #339933;">:</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Der Methuselah-Baum ist so alt wie du – Respekt!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">default</span><span style="color: #339933;">:</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Ja, schönes Alter!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Der <strong>switch-Anweisung</strong> wird zunächst ein Wert übergeben. Dieser wir dann überprüft, ob er sich einem bestimmten Fall (case) zuordnen lassen kann. Ist dies nicht möglich, wird der <strong>default-Fall</strong> angewandt. Der default-Fall ist optional und kann auch weggelassen werden. Bei switch ist zu beachten, dass die <strong>break-Anweisungen</strong> gesetzt sind. Ein Break bedeutet, dass nach Aufruf der break-Anweisung der weitere Durchlauf einer Anweisung abgebrochen wird. Wird ein Break bei der switch-Anweisung vergessen, so erzeugen wir einen <strong>Fall-Through</strong>. Es wird jetzt nicht abgebrochen sobald ein Fall eingetroffen ist, sondern es werden die weiter unten stehenden Fälle auch überprüft.</p>
<p>Jetzt müssen wir nur noch etwas über <strong>Arrays</strong> erfahren und wir können unser <strong>CowSay-Programm</strong> zum &#8220;Sprechen&#8221; bringen.</p>
]]></content:encoded>
			<wfw:commentRss>http://coder-blog.de/java-tutorial-fallunterscheidungen-bedingte-anweisungen/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
