cs11dotnet7/vscode/Chapter03/IterationStatements/Program.cs

50 lines
828 B
C#
Raw Normal View History

2022-02-18 13:13:25 +01:00
// Looping with the while statement
int x = 0;
while (x < 10)
{
WriteLine(x);
x++;
}
// Looping with the do statement
2023-04-06 09:16:14 +02:00
string? actualPassword = "Pa$$w0rd";
2022-02-18 13:13:25 +01:00
string? password;
2023-04-06 09:16:14 +02:00
int maximumAttempts = 10;
2022-02-18 13:13:25 +01:00
int attempts = 0;
do
{
attempts++;
Write("Enter your password: ");
password = ReadLine();
}
2023-04-06 09:16:14 +02:00
while ((password != actualPassword) & (attempts < maximumAttempts));
2022-02-18 13:13:25 +01:00
2023-04-06 09:16:14 +02:00
if (password == actualPassword)
2022-02-18 13:13:25 +01:00
{
WriteLine("Correct!");
}
else
{
2023-04-06 09:16:14 +02:00
WriteLine("You have used {0} attempts! The password was {1}.",
arg0: maximumAttempts, arg1: actualPassword);
2022-02-18 13:13:25 +01:00
}
// Looping with the for statement
for (int y = 1; y <= 10; y++)
{
WriteLine(y);
}
// Looping with the foreach statement
string[] names = { "Adam", "Barry", "Charlie" };
foreach (string name in names)
{
WriteLine($"{name} has {name.Length} characters.");
}