Die bedingte Ausführung in C# (Die bedingte Ausführung in C#), Lektion, Seite 723516
https://www.purl.org/stefan_ram/pub/bedingte_ausfuehrung_csharp (Permalink) ist die kanonische URI dieser Seite.
Stefan Ram
C#-Kurs

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
Hof
Program.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
@
A
Program.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
true
Program.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
true
Program.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 ); }

Seiteninformationen und Impressum   |   Mitteilungsformular  |   "ram@zedat.fu-berlin.de" (ohne die Anführungszeichen) ist die Netzpostadresse von Stefan Ram.   |   Eine Verbindung zur Stefan-Ram-Startseite befindet sich oben auf dieser Seite hinter dem Text "Stefan Ram".)  |   Der Urheber dieses Textes ist Stefan Ram. Alle Rechte sind vorbehalten. Diese Seite ist eine Veröffentlichung von Stefan Ram. Schlüsselwörter zu dieser Seite/relevant keywords describing this page: Stefan Ram Berlin slrprd slrprd stefanramberlin spellched stefanram723516 stefan_ram:723516 Die bedingte Ausführung in C# Stefan Ram, Berlin, and, or, near, uni, online, slrprd, slrprdqxx, slrprddoc, slrprd723516, slrprddef723516, PbclevtugFgrsnaEnz Erklärung, Beschreibung, Info, Information, Hinweis,

Der Urheber dieses Textes ist Stefan Ram. Alle Rechte sind vorbehalten. Diese Seite ist eine Veröffentlichung von Stefan Ram.
https://www.purl.org/stefan_ram/pub/bedingte_ausfuehrung_csharp