C ා if else if statement

First, role

The judgment used to deal with the interval of multiple conditions.

Two. Grammar

If (judgment condition)

{
       Code to execute;

}
Else if (judgment condition)

{
      Code to execute;

}
Else if (judgment condition)

{
       Code to execute;

}
Else if (judgment condition)

{
       Code to execute;
}

........

else

{
       Code to execute;
} 

Execution process:

The program first judges the judging conditions in the braces of the first if. If the conditions are true, that is, it returns a true value, then it executes the code in the braces of the if. After the execution, it immediately jumps out of the if else if structure.

If the judgment condition of the first if is not tenable, that is, a false is returned, continue to judge downward, and judge the judgment condition of each if in turn. If it is tenable, execute the code in the braces of the if. If it is not tenable, continue to judge downward. If the judgment condition of each if is not tenable, see the current if else if structure Whether else exists in.

If there is else, the code in else will be executed. If there is no else, the whole if else if god horse will not do it. Else can be omitted.

III. flow chart

 

IV. examples

[exercise 1] student's final examination score evaluation, score > = 90: A, 90 > score > = 80: B, 80 > score > = 70: C, 70 > score > = 60: D, score < 60: e

class Program
    {
        static void Main(string[] args)
        {        
            //Evaluation of students' final examination results
            // Results >=90:A
            //90 > score > = 80: B
            //80 > score > = 70: C
            //70 > score > = 60: D
            //Results <60:E
             Console.WriteLine("Please enter the test scores of the students");
            int socre = Convert.ToInt32(Console.ReadLine());
            if (socre >= 90)
            {
                Console.WriteLine("A");
 
            }
            else if (socre >= 80)
            {
                Console.WriteLine("B");
            }
            else if (socre >= 70)
            {
                Console.WriteLine("C");
            }
            else if (socre >= 60)
            {
                Console.WriteLine("D");
            }
            else
            {
                Console.WriteLine("E");
            }
 
            Console.ReadKey();
 
        }
    }

 

[exercise 2] compare the three numbers regardless of their size

Console.WriteLine("Please enter the first number");
int numberOne = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please enter the second number");
int numberTwo = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please enter the third number");
int numberThree = Convert.ToInt32(Console.ReadLine());
 
//If else if should be used in three cases
//If the first number is greater than two and greater than three
if (numberOne > numberTwo && numberOne > numberThree)
{
    Console.WriteLine(numberOne);
}
 
//If the second number is greater than one and also greater than three
else if (numberTwo > numberOne && numberTwo > numberThree)
{
    Console.WriteLine(numberTwo);
}
 
//The third number is greater than the second and greater than one
else
{
    Console.WriteLine(numberThree);
}
Console.ReadKey();  

Tags: C#

Posted on Sat, 09 Nov 2019 06:10:26 -0800 by amax