»while« – Iterationen in C♯
Beispiel
Program.cs
public static class Program
{ public static void Main()
{ while( true )
global::System.Console.WriteLine( global::Microsoft.JScript.MathObject.random() ); }}global::System.Console.Out
0.566118218484735
0.2474786329776645
0.1467934074234326
0.2447229046636278
0.41637269525573997- u. s. w.
Program.cs
public static class Program
{ public static void Main()
{ if( true )
global::System.Console.WriteLine( global::Microsoft.JScript.MathObject.random() ); }}global::System.Console.Out
0.26323967935587644
Program.cs
public static class Program
{ public static void Main()
{ int i = 10; /* Schleifenvorbereitung */
while
(
i < 16 /* Schleifenpruefung */
)
{ global::System.Console.WriteLine( i ); /* Nutzlast */ /* Schleifeninhalt */
i = i + 2; /* Schleifenschritt */ /* Schleifeninhalt */
}}}Program.cs
public static class Program
{ public static void Main()
{ int i = 10;
while( i < 16 )
{ global::System.Console.WriteLine( i );
i = i + 2; }}}global::System.Console.Out
10
12
14Program.cs
public static class Program
{ public static void Main()
{ global::System.Console.WriteLine( 10 );
global::System.Console.WriteLine( 12 );
global::System.Console.WriteLine( 14 ); }}global::System.Console.Out
10
12
14Program.cs
public static class Program
{ public static void Main()
{ int i = 32;
while( i < 127 )
{ global::System.Console.WriteLine
( "{0} {1}", i, global::System.Char.ConvertFromUtf32( i ));
i = i + 1; }}}global::System.Console.Out
32
33 !
34 "- …
Program.cs
public static class Program
{ public static void Main()
{ int i = 32;
while( i < 127 )
{ global::System.Console.WriteLine
( "{0} {1} {2} {3} {4}", i, global::System.Char.ConvertFromUtf32( i ),
global::System.Char.IsDigit( global::System.Char.ConvertFromUtf32( i ), 0 )? "D" : " ",
global::System.Char.IsLetter( global::System.Char.ConvertFromUtf32( i ), 0 )? "A" : " ",
global::System.Char.IsWhiteSpace( global::System.Char.ConvertFromUtf32( i ), 0 )? "space" : " " );
i = i + 1; }}}global::System.Console.Out
32 space
33 !
34 "
35 #
36 $
37 %
38 &
39 '
40 (
41 )
42 *
43 +
44 ,
45 -
46 .
47 /
48 0 D
49 1 D
50 2 D
51 3 D
52 4 D
53 5 D
54 6 D
55 7 D
56 8 D
57 9 D
58 :
59 ;
60 <
61 =
62 >
63 ?
64 @
65 A A
66 B A
67 C A
68 D A
69 E A- …
Übungsfragen
Übungsfrage
Was gibt das folgende Programm aus?
Program.cs
public static class Program
{ public static void Main()
{ bool v = true;
while( v )
global::System.Console.WriteLine( v = false ); }}
Übungsfrage
Was gibt das folgende Programm aus?
Program.cs
public static class Program
{ public static void Main()
{ bool v = false;
while( v == false )
global::System.Console.WriteLine( v = false ); }}
Übungsfrage
Was gibt das folgende Programm aus?
Program.cs
public static class Program
{ public static void Main()
{ bool v = false;
while( v = false )
global::System.Console.WriteLine( v = false ); }}
Übungsfrage
Was gibt das folgende Programm aus?
Program.cs
public static class Program
{ public static void Main()
{ bool v = true;
while( v = false )
global::System.Console.WriteLine( v = false ); }}
Übungsfrage
Was gibt das folgende Programm aus?
Program.cs
public static class Program
{ public static void Main()
{ bool v = true;
while( v = true )
global::System.Console.WriteLine( v = false ); }}
Übungsfrage
Was gibt das folgende Programm aus?
Program.cs
public static class Program
{ public static void Main()
{ int v = 0;
while( v == 0 )
global::System.Console.WriteLine( v = 2 ); }}
Übungsfrage
Was gibt das folgende Programm aus?
Program.cs
public static class Program
{ public static void Main()
{ int v = 0;
while( v < 2 )
global::System.Console.WriteLine( v = v + 1 ); }}
Übung
Wiederholte Ausgabe eines Textes
Schreiben Sie ein Programm, in dem die Anweisung »global::System.Console.WriteLine( "Dieses ist ein langer Satz." );« nur einmal vorkommt, und zwar in einer Schleife, von der sie insgesamt zehnmal wiederholt wird. Das Programm soll auf diese Weise die Zeile »Dieses ist ein langer Satz.« zehnmal hintereinander ausgeben.
Zählen von 12 bis 27
Schreiben Sie ein Programm, das die Anweisung »global::System.Console.WriteLine( i );« enthält und die Zahlen von 12 bis 27 (eine Zahl pro Zeile, aufsteigend in Einerschritten) ausgibt. Das Programm darf neben der genannten Anweisung keine anderen Ausgabeanweisungen enthalten.
Übungsaufgaben (Gruppe 1)
In Zweierschritten von 14 bis 2 abwärts zählen
Schreiben Sie ein Programm, das die Anweisung »global::System.Console.WriteLine( i );« enthält und die Zahlen von 14 bis 2 (eine Zahl pro Zeile) absteigend in Zweierschritten ausgibt. Das Programm darf neben der genannten Anweisung keine anderen Ausgabeanweisungen enthalten.
Grundformen
Schreiben Sie eine Methode, die mit Sternchen eine Linie ausgibt! Dabei darf das Zeichen »*« (Sternchen, asterisk ) selber nur einmal im Quelltext vorkommen. Die Aufgabe soll also wieder mit einer Schleife gelöst werden. Die Anzahl der Sternchen wird der Methode als Argument übergeben.
linie( 20 )
********************