C # if else-if statements

First, the role

Determination section for processing a plurality of conditions.

Second, grammar

if (determination condition)

{
       To code execution;

}
else if (determination condition)

{
      To code execution;

}
else if (determination condition)

{
       To code execution;

}
else if (determination condition)

{
       To code execution;
}

........

else

{
       To code execution;
} 

Implementation process:

  The program first judgment to determine if conditions parentheses first brought in, if the condition is true, that is a return to true, the code braces if brought in the execution, execution is completed, immediately jump out if else-if structure.

If it is determined if a condition brought does not hold, that is, returns a false, then continued down the judgment, the judgment in order to determine the conditions of each if carried, if established, on the implementation of braces that if brought in the code, if not satisfied, the judge continued downward, if the judgment conditions of each if not carried by the establishment, to see if there is else if else-if this current structure.

If there is else, then brought else in the code is executed, if there is no else, then the whole if-else if Smarter nothing. else can be omitted.

Third, the flow chart

 

 Fourth, examples

[ Exercise 1] students final examination results Reviews, 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)
        {        
            // student graduation exams Reviews
            // score> = 90: A
            // 90> Results> = 80: B
            // 80> score> = 70: C
            // 70> score> = 60: D
            // score <60: E
             Console.WriteLine ( "Please enter student test scores");
            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] [3 numbers compare the size is not considered equal

Console.WriteLine ( "Please enter a 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());
 
// three cases should be used if else-if to do
// If the first number is greater than two and more than three digital and digital
if (numberOne > numberTwo && numberOne > numberThree)
{
    Console.WriteLine(numberOne);
}
 
// If the second number is greater than a number and may also be greater than three digits
else if (numberTwo > numberOne && numberTwo > numberThree)
{
    Console.WriteLine(numberTwo);
}
 
// greater than the third number and the second number is greater than a number
else
{
    Console.WriteLine(numberThree);
}
Console.ReadKey();  

  

Guess you like

Origin www.cnblogs.com/qy1234/p/11824996.html