C # Practice 5 (goto statement)

The C# goto statement is used to directly go to the position specified by the label in a program in a program. The label is actually composed of an identifier and a colon.

The syntax is as follows.

goto Labell;
Sentence block 1;
Labell
Sentence block 2;

If To jump to the location specified by a label, just use goto and add the label name directly.

After using the goto statement in the above statement, the execution order of the statements has changed, that is, statement block 2 is executed first, and then statement block 1 is executed.

In addition, it should be noted that the goto statement cannot jump into the loop statement, nor can it jump out of the scope of the class.

Because the goto statement is not easy to understand the program, the goto statement is not commonly used.

EX. is used for account and password login: If the number of errors exceeds 3 times, it will output “You have entered too many times, please contact the administrator”.

using System;


namespace KingTest03
{
class Program
{
static void Main(string[] args)
{
Program program
= new Program();
program.password();

}
public void password()
{
int count = 0;
denglu:
Console.WriteLine(
"Please enter your username: ");
string username = Console.ReadLine();
Console.WriteLine(
"Please enter the password:< span style="color: #800000;">");
int password = int.Parse(Console.ReadLine());
if (username == "King" && password == 123456)
{
Console.WriteLine(
"Congratulations on your successful login< span style="color: #800000;">");
}
else
{
count
++;
if (count <= 3)
{
Console.WriteLine(
"Sorry, the account password you entered is incorrect , Please re-enter");
goto denglu;
}
else
{
Console.WriteLine(
"You have entered too many times, please contact management Member");
}
}
}
}
}

Share a picture< /p>

using System;


namespace KingTest03
{
class Program
{
static void Main(string[] args)
{
Program program
= new Program();
program.password();

}
public void password()
{
int count = 0;
denglu:
Console.WriteLine(
"Please enter your username: ");
string username = Console.ReadLine();
Console.WriteLine(
"Please enter the password:< span style="color: #800000;">");
int password = int.Parse(Console.ReadLine());
if (username == "King" && password == 123456)
{
Console.WriteLine(
"Congratulations on your successful login< span style="color: #800000;">");
}
else
{
count
++;
if (count <= 3)
{
Console.WriteLine(
"Sorry, the account password you entered is incorrect , Please re-enter");
goto denglu;
}
else
{
Console.WriteLine(
"You have entered too many times, please contact management Member");
}
}
}
}
}

Leave a Comment

Your email address will not be published.