个性化阅读
专注于IT技术分析

C# if-else语句

点击下载

本文概述

在C#编程中,if语句用于测试条件。 C#中有多种类型的if语句。

  • 如果声明
  • if-else语句
  • 嵌套if语句
  • if-else-if梯子

C#IF语句

C#if语句测试条件。条件为真时执行。

句法:

if(condition){
//code to be executed
}

C#如果示例

using System;    
public class IfExample
    {
       public static void Main(string[] args)
        {
            int num = 10;
            if (num % 2 == 0)
            {
                Console.WriteLine("It is even number");
            }
            
        }
    }

输出:

It is even number

C#IF-else语句

C#if-else语句也测试条件。如果条件为true,则执行if块,否则执行else块。

句法:

if(condition){
//code if condition is true
}else{
//code if condition is false
}

C#If-else示例

using System;    
public class IfExample
    {
        public static void Main(string[] args)
        {
            int num = 11;
            if (num % 2 == 0)
            {
                Console.WriteLine("It is even number");
            }
            else
            {
                Console.WriteLine("It is odd number");
            }
            
        }
    }

输出:

It is odd number

C#If-else示例:用户输入

在此示例中,我们使用Console.ReadLine()方法从用户那里获取输入。它返回字符串。对于数值,你需要使用Convert.ToInt32()方法将其转换为int。

using System;    
public class IfExample
    {
       public static void Main(string[] args)
        {
            Console.WriteLine("Enter a number:");
            int num = Convert.ToInt32(Console.ReadLine());

            if (num % 2 == 0)
            {
                Console.WriteLine("It is even number");
            }
            else
            {
                Console.WriteLine("It is odd number");
            }
            
        }
    }

输出:

Enter a number:11
It is odd number

输出:

Enter a number:12
It is even number

C#IF-else-if梯形图语句

C#if-else-if阶梯语句从多个语句执行一个条件。

句法:

if(condition1){
//code to be executed if condition1 is true
}else if(condition2){
//code to be executed if condition2 is true
}
else if(condition3){
//code to be executed if condition3 is true
}
...
else{
//code to be executed if all the conditions are false
}

C#If else-if示例

using System;    
public class IfExample
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Enter a number to check grade:");
            int num = Convert.ToInt32(Console.ReadLine());

            if (num <0 || num >100)
            {
                Console.WriteLine("wrong number");
            }
            else if(num >= 0 && num < 50){
                Console.WriteLine("Fail");
            }
            else if (num >= 50 && num < 60)
            {
                Console.WriteLine("D Grade");
            }
            else if (num >= 60 && num < 70)
            {
                Console.WriteLine("C Grade");
            }
            else if (num >= 70 && num < 80)
            {
                Console.WriteLine("B Grade");
            }
            else if (num >= 80 && num < 90)
            {
                Console.WriteLine("A Grade");
            }
            else if (num >= 90 && num <= 100)
            {
                Console.WriteLine("A+ Grade");
            }
        }
    }

输出:

Enter a number to check grade:66
C Grade

输出:

Enter a number to check grade:-2
wrong number
赞(0)
未经允许不得转载:srcmini » C# if-else语句

评论 抢沙发

评论前必须登录!