Die bedingte Ausführung in C♯
Beispiel
Program.cs
public static class Program
{public static bool DruckHallo()
{ global::System.Console.WriteLine( "Hallo" );
return false; }public static void Main()
{ if( DruckHallo() ){} }}global::System.Console.Out
Hallo
Program.cs
public static class Program
{public static void Main()
{ if( false )global::System.Console.WriteLine( "false" );
if( true )global::System.Console.WriteLine( "true" ); }}global::System.Console.Out
true
Program.cs
public static class Program
{
public static void Main()
{ if( true )
{ global::System.Console.WriteLine( "Haus" );
global::System.Console.WriteLine( "Hof" ); }
if( false )
{ global::System.Console.WriteLine( "Garten" );
global::System.Console.WriteLine( "123" ); }}}transcript
Haus
HofProgram.cs
public static class Program
{ public static void Main()
{ if( false )
{ global::System.Console.WriteLine( "Haus" );
global::System.Console.WriteLine( "Hof" );
global::System.Console.WriteLine( "Garten" ); }}}Program.cs
public static class Program
{public static void Stern( int n )
{ if( n >= 1 )global::System.Console.Write( "*" );
if( n >= 2 )global::System.Console.Write( "*" );
if( n >= 3 )global::System.Console.Write( "*" );
if( n >= 4 )global::System.Console.Write( "*" ); }public static void Main()
{ Stern( 2 ); Stern( 1 ); global::System.Console.WriteLine(); }}transcript
***
Program.cs
public static class Program
{ public static void Main()
{ global::System.Console.WriteLine( global::System.Char.ConvertFromUtf32( 64 ));
global::System.Console.WriteLine( global::System.Char.ConvertFromUtf32( 65 )); }}transcript
@
AProgram.cs
public static class Program
{ public static void Main()
{ global::System.Console.WriteLine( global::System.Char.IsLetter( global::System.Char.ConvertFromUtf32( 64 ), 0 ));
global::System.Console.WriteLine( global::System.Char.IsLetter( global::System.Char.ConvertFromUtf32( 65 ), 0 )); }}transcript
false
trueProgram.cs
public static class Program
{ public static void Main()
{ global::System.Console.WriteLine( global::System.Char.IsLetter( global::System.Char.ConvertFromUtf32( 64 ), 0 ));
global::System.Console.WriteLine( global::System.Char.IsLetter( global::System.Char.ConvertFromUtf32( 65 ), 0 )); }}transcript
false
trueProgram.cs
public static class Program
{public static void Check( int c )
{ string s = global::System.Char.ConvertFromUtf32( c );if( global::System.Char.IsLetter( s, 0 )== false )
global::System.Console.WriteLine
( "Das Zeichen '{0}' ist kein Buchstabe.\n", s ); }public static void Main()
{ Check( 64 );
Check( 65 ); }}transcript
Das Zeichen '@' ist kein Buchstabe.
Übung
Übungsfrage
Weiter oben wurde die folgende Main-Methode gezeigt. Fehlt am Ende der if-Anweisung nicht ein Semikolon?
public static void Main()
{ if( DruckHallo() ){} }
Übungsfrage
Was gibt das folgende Programm aus?
Program.cs
public static class Program
{ public static void Main()
{ if( 4 == 2 )global::System.Console.WriteLine( "A" );
global::System.Console.WriteLine( "B" );
if( 3 == 3 )global::System.Console.WriteLine( "C" ); }}
Übungsfrage
- Soll "a" und "b" ausgeben, dann und nur dann wenn x == 2
if( x == 2 )
global::System.Console.WriteLine( "a" );
global::System.Console.WriteLine( "b" );
Wie kann man den Fehler der obigen if-Anweisung korrigieren?
* Übungsfrage
Welche Anweisung muß man am Ende des Rumpfs der Deklaration der Methode »Stern« noch hinzufügen, damit diese nicht nur bis zum Wert 4, sondern bis zu beliebig großen Werten des Parameters n, n Sterne ausgibt?
Übungsaufgaben
/ Übungsaufgabe
Fügen Sie in den Rumpf der folgenden Methodendeklaration als letzte Anweisung eine if-Anweisung ein. Diese soll ausgeben »Mindestbestand unterschritten, nachbestellen!«, falls der Wert von »bestand« kleiner als »8« ist.
- Methode
public static void Bericht( int bestand )
{ global::System.Console.WriteLine( "Lagerbestand = " + bestand ); }
/ Refaktor
Schreiben Sie die folgende Methode so um, daß sie – bei unverändertem Verhalten – keine return-Anweisung mehr enthält.
- Methode
static void SchreibeKehrwert( int i )
{ if( i == 0 )return;
global::System.Console.WriteLine( 1 / i ); }