语句的类型包括:声明语句、表达式语句、选择语句、循环语句、跳转语句、异常语句
1.声明语句引:入新的变量或常量。 变量声明可以选择为变量赋值。 在常量声明中必须赋值。
例如:
int i = 0;//声明变量i 并赋值,也可以不赋值。
double d;
“//”表示注释一行,“/*…*/”可以注释一段区域,注释后的内容变绿。
2.表达式语句:用于计算值的表达式语句必须在变量中存储该值。
例如:
sum = i + j;//变量i和j在此之前必须先赋值。而且sum也需要声明类型。
int x = a + b; //或者在声明的同时进行运算。
3.选择语句:if, else, switch, case
4.循环语句:do, for, foreach, while
5.跳转语句:break, continue, default, return
6.异常语句:try-catch-finally
一、分支(选择)语句:
if(判断条件){程序代码,运算等}、if(){} else{}、if(){} else if(){}......else{}、if(){if(){} else if(){}} else{}
if是如果的意思,else是另外的意思,if后面跟()括号内为判断条件,如果符合条件则进入if语句执行命令。如果不符合则不进入if语句。else后不用加条件,但是必须与if配合使 用,else后也可加if,但if后需要条件。if-else可以嵌套。
//语句的分类//顺序语句,分支(选择)语句,循环语句//分支(选择)语句//格式1 if(){}//int a = 6;//if (a <= 100 && a >= 0) ;//{// Console.WriteLine("您输入的这个数是100以内的数。");//}//Console.WriteLine(a);//Console.ReadLine();//格式2 if(){}else{} 二选一//如果if满足,走if,else就不会走//如果if不满足,else就一定会走//Console.Write("请输入一个整数:");//int a = int.Parse(Console.ReadLine());//if (a > 10)//{ //Console.WriteLine("您输入的是一个大于10的数。");//}//else//{//Console.WriteLine("您输入的不是一个大于10的数。");//}//Console.ReadLine();//格式3 if(){} else if(){}...else{} 多选一//只要上面有一个if或者else if满足条件,执行了//从它以下的所有不需要再去判断读取//Console.Write("请输入一个月份:");//int month = int.Parse(Console.ReadLine ());//if (month > 2 && month < 6)//{// Console.WriteLine("您输入的是春天的月份!");//}//else if (month > 5 && month > 9)//{// Console.WriteLine("您输入的是夏天的月份!");//}//else if (month > 8 && month < 12)//{// Console.WriteLine("您输入的是秋天的月份!");//}//else if (month == 12 || month == 1 || month == 2)//{// Console.WriteLine("您输入的是冬天的月份!");//}//else//{// Console.WriteLine("您的输入有误!");//}//Console.ReadLine();//格式4 if的嵌套//首先规定好大的范围,再进去判断各种小的情况Console.Write("请输入一个月份:");int month = int.Parse(Console.ReadLine());if (month >= 1 && month <= 12){if (month > 2 && month < 6){Console.WriteLine("您输入的是春天的月份!");}else if (month > 5 && month > 9){Console.WriteLine("您输入的是夏天的月份!");}else if (month > 8 && month < 12){Console.WriteLine("您输入的是秋天的月份!");}else //month == 12 || month == 1 || month == 2 {Console.WriteLine("您输入的是冬天的月份!");}}else{Console.WriteLine("您的输入有误!");}Console.ReadLine();
二、switch case 格式 多选一
//switch case 多选一Console.WriteLine("1.汉堡包");Console.WriteLine("2.鸡腿");Console.WriteLine("3.鸡米花");Console.WriteLine("4.鸡肉卷");Console.Write("请输入您选择的商品编号:");string a = Console.ReadLine();//也可以是int类型:int a=int.Pars(Console.ReadLine());//switch格式switch (a)//括号内是变量名称 {case "1": //case后面必须要有一个空格,int类型的话就是:case 1Console.WriteLine("您选择的商品是:汉堡包!"); break;//作用是跳出最近的花括号case "2": Console.WriteLine("您选择的商品是:鸡腿!"); break;case "3": Console.WriteLine("您选择的商品是:鸡米花!");break;case "4": Console.WriteLine("您选择的商品是:鸡肉卷!"); break;default://相当于else,以上可能都不是 Console.WriteLine("您的输入有误!此商品不存在!"); break;}Console.ReadLine();//if格式//if (a == "1")//{// Console.WriteLine("您选择的商品是:汉堡包!");//}//else if (a == "2")//{// Console.WriteLine("您选择的商品是:鸡腿!");//}//else if (a == "3")//{// Console.WriteLine("您选择的商品是:鸡米花!");//}//else if (a == "4")//{// Console.WriteLine("您选择的商品是:鸡肉卷!");//}//else//{// Console.WriteLine("您的输入有误!此商品不存在!");//}//Console.ReadLine();