Statement category:
I. Sequence
Execute programs one by one
Ii. Branch
If statement -- used to determine conditions, bool type.
1. If () 2.if() 3.if() 4.if ()
{{{{
}}}}
Else () else if ()
{{{
}}}
Else if ()
{{
}}
Else () else ()
PS: If () can only have one else (), but can have multiple else if ().
Example 1: Root
Static void main5 (string [] ARGs)
{
Console. writeline ("finding a quadratic equation: A * x + bx + c = 0 root ");
Console. Write ("A = ");
Int A = convert. toint32 (console. Readline ());
Console. Write ("B = ");
Int B = convert. toint32 (console. Readline ());
Console. Write ("c = ");
Int c = convert. toint32 (console. Readline ());
If (A = 0)
{
Console. writeline ("not a quadratic equation ");
}
Else
{
Console. writeline ("is a quadratic equation ");
Int Delta = B * B-4 * a * C;
If (delta> 0)
{
Console. writeline ("X1 = {0}, X2 = {1}", (-B + math. SQRT (DELTA)/2 * A), (-B-math. SQRT (DELTA)/2 * A); -- the root result of the request.
}
Else if (delta = 0)
{
Console. writeline ("x =" +-B/2 * );
}
Else
{
Console. writeline ("no real root ");
}
}
Example 2: guessing
Static void main (string [] ARGs)
{
Console. Write ("please punch :");
Int human = convert. toint32 (console. Readline ());
Random Mm = new random ();
Int computer = mm. Next (3 );
If (Human = 0)
{
If (Computer = 0)
{
Console. writeline ("Draw! ");
}
If (Computer = 1)
{
Console. writeline ("You get scissors, the computer gets stones, and you lose! ");
}
If (Computer = 2)
{
Console. writeline ("You Get Out of scissors, the computer gets out of the cloth, you win! ");
}
Else if (Human = 1)
{
If (Computer = 0)
{
Console. writeline ("you are out of the stone, the computer is out of the scissors, you win! ");
}
If (Computer = 1)
{
Console. writeline ("Draw! ");
}
If (Computer = 2)
{
Console. writeline ("you are out of the stone, the computer is out of the cloth, you are lost! ");
}
Else if (Human = 2)
{
If (Computer = 0)
{
Console. writeline ("you are out of the cloth, the computer is out of the scissors, you are lost! ");
}
If (Computer = 1)
{
Console. writeline ("you are out of the cloth, the computer is out of the stone, you win! ");
}
If (Computer = 2)
{
Console. writeline ("Draw! ");
}
Else
{
Console. writeline ("press the regular expression! Thank you! ");
}
}
}
PS:Generate a random generator:
Random SSS = new random ();
Int variable = SSS. Next ();
Console. writeline (variable );
Example 3: Standard Weight
Static void main8 (string [] ARGs)
{
Console. Write ("Gender :");
String sex = console. Readline ();
Console. Write ("height :");
Int H = convert. toint32 (console. Readline ());
Console. Write ("Weight :");
Int W = convert. toint32 (console. Readline ());
If (sex = "male ")
{
Int W1 = H-100-3;
Int W2 = H-100 + 3;
If (W <= W2 & W> = W1)
{
Console. writeline ("normal weight! ");
}
Else if (W> W2)
{
Console. writeline ("Overweight, need to exercise! ");
}
Else if (W <W1)
{
Console. writeline ("thin weight, pay attention to nutrition! ");
}
Else
{
Console. writeline ("do you have weight? ");
}
}
Else
{
Int W1 = H-100-3;
Int W2 = H-100 + 3;
If (W <= W2 & W> = W1)
{
Console. writeline ("normal weight! ");
}
Else if (W> W2)
{
Console. writeline ("Overweight, need to exercise! ");
}
Else if (W <W1)
{
Console. writeline ("thin weight, pay attention to nutrition! ");
}
Else
{
Console. writeline ("do you have weight? ");
}
}
PS:Breakpoint -- check program errors
Tool -- option -- text editor -- all languages -- General -- row number (click). -- (CTRL + F5 key does not execute this program and can only start)
Debugging -- start debugging -- window -- instant -- Statement by statement. -- view the program running steps, such as executing "if", that is, not executing "else ".