C#条件语句(if,if-else,if-else-if阶梯,嵌套的if,switch,嵌套的switch)

2021年3月10日16:19:36 发表评论 1,007 次浏览

编程中的决策类似于现实生活中的决策。同样在编程中, 当满足某些条件时, 也需要执行特定的代码块。

编程语言使用控制语句根据某些条件来控制程序的执行流程。这些用于根据程序状态的更改使执行流程前进并分支。

C#的条件语句:

if语句

if语句检查给定条件。如果条件评估为真, 则将执行代码/状态块, 否则将不执行。

语法如下:

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

注意:

如果大括号{}不与if语句一起使用, 则仅将其旁边的语句视为与if语句关联。

例子:

if (condition)
   statement 1;
statement 2;

在此示例中, 仅将语句1视为与if语句关联。

流程图:

C#条件语句(if,if-else,if-else-if阶梯,嵌套的if,switch,嵌套的switch)1

例子:

// C# program to illustrate if statement
using System;
  
public class GFG {
  
     public static void Main( string [] args)
     {
         string name = "Geek" ;
         if (name == "Geek" ) {
             Console.WriteLine( "lsbin" );
         }
     }
}

输出如下:

lsbin

IF – else陈述

如果条件为真, if语句将评估代码, 如果条件不为真, 则else语句。如果条件为假, 它将告诉代码该怎么做。

语法如下:

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

流程图:

C#条件语句(if,if-else,if-else-if阶梯,嵌套的if,switch,嵌套的switch)2

例子:

// C# program to illustrate 
// if-else statement
using System;
  
public class GFG {
  
     public static void Main( string [] args)
     {
         string name = "Geek" ;
         if (name == "Geeks" ) {
             Console.WriteLine( "lsbinr" );
         }
         else {
             Console.WriteLine( "Geeks" );
         }
     }
}

输出如下:

Geeks

如果–否则–如果梯子声明

if-else-if阶梯语句从多个语句执行一个条件。执行从顶部开始, 并检查每个if条件。 if块的语句将被执行, 其评估结果为true。如果所有if条件的评估结果都不为true, 则评估最后一个else块。

语法如下:

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,if-else,if-else-if阶梯,嵌套的if,switch,嵌套的switch)3

例子:

// C# program to illustrate
// if-else-if ladder
using System;
  
class GFG {
  
     public static void Main(String[] args)
     {
         int i = 20;
  
         if (i == 10)
             Console.WriteLine( "i is 10" );
         else if (i == 15)
             Console.WriteLine( "i is 15" );
         else if (i == 20)
             Console.WriteLine( "i is 20" );
         else
             Console.WriteLine( "i is not present" );
     }
}

输出如下:

i is 20

嵌套– If语句

if语句中的if语句

称为嵌套if。这种情况下的if语句是另一个if or else语句的目标。如果不止一个条件需要为真, 并且其中一个条件是父条件的子条件, 则可以嵌套使用。

语法如下:

if (condition1) 
        {
             // code to be executed 
             // if condition2 is true 
             if (condition2) 
             {
                 // code to be executed 
                 // if condition2 is true 
             }
        }

流程图:

C#条件语句(if,if-else,if-else-if阶梯,嵌套的if,switch,嵌套的switch)4

例子:

// C# program to illustrate
// nested-if statement
using System;
  
class GFG {
  
     public static void Main(String[] args)
     {
         int i = 10;
  
         if (i == 10) {
  
             // Nested - if statement
             // Will only be executed if statement
             // above it is true
             if (i < 12)
                 Console.WriteLine( "i is smaller than 12 too" );
             else
                 Console.WriteLine( "i is greater than 15" );
         }
     }
}

输出如下:

i is smaller than 12 too

切换语句

switch语句是长if-else-if阶梯的替代方法。检查表达式的不同情况, 然后执行一次匹配。

打破

语句用于移出开关。如果不使用中断, 则控件将流向其下方的所有情况, 直到找到中断或切换结束。有

默认情况(可选)

在切换结束时, 如果大小写都不匹配, 则执行默认大小写。

语法如下:

switch (expression)
 {
case value1: // statement sequence
             break;
case value2: // statement sequence
             break;
.
.
.
case valueN: // statement sequence
             break;
default: // default statement sequence
}

交换机流程图–案例:

Switch-Case语句流程图

例子:

// C# example for switch case
using System;
  
public class GFG 
{
     public static void Main(String[] args)
     {
         int number = 30;
         switch (number)
         {
         case 10: Console.WriteLine( "case 10" );
                  break ;
         case 20: Console.WriteLine( "case 20" );
                  break ;
         case 30: Console.WriteLine( "case 30" );
                  break ;
         default : Console.WriteLine( "None matches" ); 
                  break ;
         }
     }
}

输出如下:

case 30

嵌套开关

C#中允许使用嵌套Switch大小写。在这种情况下, 开关位于其他开关盒内部。内部切换是在父切换的一种情况下出现的。

例子:

// C# example for nested switch case
using System;
  
public class GFG 
{
     public static void Main(String[] args)
     {
         int j = 5;
  
         switch (j)
         {
             case 5: Console.WriteLine(5);
                     switch (j - 1)
                    {
                     case 4: Console.WriteLine(4);
                             switch (j - 2)
                           {
                             case 3: Console.WriteLine(3);
                                     break ;
                          }
                     break ;
                 }
                 break ;
             case 10: Console.WriteLine(10);
                      break ;
             case 15: Console.WriteLine(15);
                      break ;
             default : Console.WriteLine(100);
                      break ;
         }
  
     }
}

输出如下:

5
4
3

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: