C# basics ⑤-three major structures (sequence, branch, loop)

One, mind map

Two, three major structure description

1. Sequence structure

As the name implies, it is executed according to the writing order of the written code, from top to bottom.

2. Branch structure

Optionally execute the statement, if the condition is true, execute statement 1, and if the condition is false, execute statement 2

                                                       

3. Loop structure

Loop is the execution of a repetitive code. When the condition of the expression is satisfied, the statement block is executed, and when the condition is not satisfied, it terminates. But the program may not be executed once

                                                        

Three, actual combat drills

Note: The following examples only consider the correct input

1.if...else: Determine whether the year entered by the user is a leap year

Console.WriteLine("请输入年份");                   //提示用户输入年份
int year = Convert.ToInt32(Console.ReadLine());   //接收用户输入内容,转为int类型
bool result = (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);    //能整除,能整除4但不能被100整除

if (result)                          //括号里面方判断条件,result现在有两个值,True和False。
{
    Console.WriteLine("是闰年");     //如果条件为真,是闰年
}
else
{
    Console.WriteLine("不是闰年");   //如果条件为假,不是闰年
}
Console.ReadKey();

2.switch...case: Judge the student's grade according to the grade entered by the student

string str = "";                   //定义一个字符串变量
Console.WriteLine("请输入成绩");    
double result = Convert.ToDouble(Console.ReadLine());       //用户输入成绩为double类型

if (result >= 90)                  //成绩>=90:A
{
    str= "A";  
}
else if (result >=80)              //90>成绩>=80:B
{
   str = "B";
}
else if(result >=70)
{
    str = "C";                     //80>成绩>=70:C
}
else if(result >= 60)
{
    str = "D";                     //70>成绩>=60:D
}
else
{
   Console.WriteLine("等级为:E");   //成绩<60:E
}
Console.ReadKey();                 //等待用户输入指令

3.switch...case: Determine the level of the user's performance based on the user's input score

Console.WriteLine("请输入你的成绩");                       //提示用户输入
double result = Convert.ToDouble (Console.ReadLine());    //接受用户输入内容

switch (result)
{
    case 100:
    case 90:
        Console.WriteLine("优秀!");     //如果前一个case的输出语句与这个case的输出语句相同,则前一个可以省略
        break;
    case 80:
        Console.WriteLine("良好");
        break;
     case 70:
     case 60:
        Console.WriteLine("及格!");
        break;
     default:
         Console.WriteLine("不及格");
         break;
}
Console.ReadKey();                       //等待用户按下任意键退出

4.while: Calculate the cumulative sum of 1-100

int i = 1;                                       //i的初始值为1
int sum = 0;                                     //累加和的初始值为0

while (i <= 100)                                 //循环
{
    sum = sum + i;                               //和累加
    i++;                                         //每次循环加1
}
Console.WriteLine("1-100的累加和为:{0}", sum);   //在控制台输出结果
Console.ReadKey();                               //等待用户按下某键并退出
 

5. do...while: keep asking the user to enter a number, and then print twice the number, the program exits when the user enters q

string str = "";                           //定义一个字符串变量
int number = 0;                            //一个int类型变量,接收类型转换后的数字

do
{
    Console.WriteLine("请输入一个数字");      
    str = Console.ReadLine();              //接收用户输入的数字
 
    if (str != "q")                        //如果用户输入的数字不是q时
    {
        number = Convert.ToInt32 (str);    //将数字转为int类型
        Console.WriteLine("这个数的二倍是{0}", number * 2);   //打印这个数的二倍
    }
} while (str != "q");                      //当用户输入的输入不是q的时候就循环
Console.WriteLine("程序结束");              //输出结果
Console.ReadLine();                        //等待用户按下某键退出

6.for: Add the integers between 1-100 to get the current number whose cumulative value is greater than 20 (for example: 1+2+3+4+5+6=21). The result is 6

int sum = 0;                                 //一个int类型变量,计算和
for (int i = 1; i <= 100; i++)               //for循环,次数1-100
{
    sum += i;                                //和进行累加
    if (sum > 20)                            //如果和>20
   {
       Console.WriteLine("{0}-----------------------{1}",sum,i);  //输出结果
       break;                                                     //跳出循环,程序结束
    }
}
 Console.ReadKey();

7.while continue: Realize the calculation of the sum of all integers between 1 and 100 that are divisible by 7

int i = 1;                       //一个int类型变量并赋初值
int sum = 0;                     //和初始化为0

while (i <= 100)                 //while循环,上限100
{
    if (i % 7 == 0)              //如果能被7整除
   {
       i++;                      //i+1
       continue;                 //跳出当前循环,执行下一次循环
   }
   sum += i;                     //和进行累加
   i++;                          //i+1
}
Console.WriteLine("总和为:{0}", sum);         //输出结果
Console.ReadKey();                            //等待用户按下某键退出

8. Ternary expression: to achieve the same function as if...else, only one line to solve

Expression 1? Expression 2: Expression 3

Console.WriteLine("请输入姓名");         //输入姓名
string name = Console.ReadLine();       //接收用户输入内容
string result = name =="小邓" ?"系统提示此人很纯洁" : "此人很邪恶";    //三元表达式
//如果用户输入的内容是“小邓”,则控制台输出"系统提示此人很纯洁",反之

Console.WriteLine(result);
Console.ReadKey();

Fourth, expand

C# judgment: https://www.runoob.com/csharp/csharp-decision.html

C# loop: https://www.runoob.com/csharp/csharp-loops.html

 

All languages ​​are inseparable from these three major structures, which also play a great role in the execution of our code. When you learn a language, you will understand it. In the future, you will learn a similar process.

 

Guess you like

Origin blog.csdn.net/weixin_43319713/article/details/108277740