#if #else c_C#程序演示简单的if else语句的示例

#if #else c

Like other programming languages, simple if else statement in C# is used to execute one code block written in two different blocks based on the condition.

像其他编程语言一样,C#中的if语句很简单,用于根据条件执行编写在两个不同块中的一个代码块。

Syntax:

句法:

    if(test_condition){
	    //code section 1
    }
    else{
	    //code section 2
    }

If test_codition is true, statements written in "code section 1" will be executed, if test_condition is false, statements written in "code section 2" will be executed.

如果test_codition为true ,则将执行以“代码部分1”编写的语句,如果test_condition为false ,则将执行以“代码部分2”编写的语句。

简单的if else语句的C#示例 (C# example for simple if else statement)

In this program, there are two different codes – 1) Input age and check whether age is teenage or not, and 2) Input two numbers and check whether first number is divisible by second number or not

在此程序中,有两种不同的代码-1)输入年龄并检查年龄是否为少女,以及2)输入两个数字并检查第一个数字是否可被第二个数字整除

// C# program to demonstrate example of 
// simple if else statement
using System;
using System.IO;
using System.Text;

namespace IncludeHelp
{
    class Test
    {
        // Main Method 
        static void Main(string[] args)
        {
            //example 1: Input age and check it's teenage or not
            int age = 0;
            Console.Write("Enter age: ");
            age = Convert.ToInt32(Console.ReadLine());

            //checking condition
            if (age >= 13 && age <= 19)
                Console.WriteLine("{0} is teenage", age);
            else
                Console.WriteLine("{0} is teenage", age);

            //example 2: Input two integer numbers and check 
            //whether first number is divisible by second or not?
            int a, b;
            Console.Write("Enter first number : ");
            a = Convert.ToInt32(Console.ReadLine());
            Console.Write("Enter second number: ");
            b = Convert.ToInt32(Console.ReadLine());

            //checking condition
            if (a % b == 0)
                Console.WriteLine("{0} is divisible by {1}", a, b);
            else
                Console.WriteLine("{0} is not divisible by {1}", a, b);

            //hit ENTER to exit the program
            Console.ReadLine();
        }
    }
}

Output

输出量

Enter age: 17
17 is teenage
Enter first number : 120
Enter second number: 20
120 is divisible by 20


翻译自: https://www.includehelp.com/dot-net/simple-if-else-statement-example-in-c-sharp.aspx

#if #else c

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值