In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "a brief introduction to C#process control statements". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian to study and learn "a brief introduction to C#process control statements" together.
C#flow control statement
C#flow control statements are very similar in both languages, but this section discusses some of their nuances.
1. Branching statements
Branch statements change the flow of execution of a runtime program based on specific conditions.
If, else and if
These are the same in both languages.
3. switch statement
In both languages, switch statements provide conditional multi-branch operations. But somewhat differently, Java allows you to "jump" over one case and execute the next, unless you use a break statement at the end of the case. However, C#requires a break or goto statement at the end of each case, and if neither exists, the compiler generates the following error:
Control cannot fall through from one case label to another.
Note, however, that where no code is specified to execute, control overrides subsequent cases when the cases match. When goto is used in a switch statement, we can only jump to another case block in the same switch. If we want to jump to default case, we can use "goto default;" otherwise we need to use "goto case cond;" where cond is the matching condition of the case we want to jump to. Another difference in Java's switch statement is that in Java we can only use switch statements for integer types, whereas C#allows us to use switch statements for string variables.
For example, the following program is legal in C#but illegal in Java:
switch (args[0]) { case "copy": ... break; case "move": ... goto case "delete"; break; case "del": case "remove": case "delete": ... break; default: ... break; }
4. Goto's Return
In Java, goto is an unimplemented reserved keyword. However, we can use statements with break or continue tags for similar purposes as goto.
C#allows goto statements to jump to labeled statements. Note, however, that in order to jump to a particular label, the goto statement must be within the scope of that label. In other words, goto cannot be used to jump into a statement block (although it can jump out of a statement block), jump out of a class, or exit a try... Finally block in catch statement. Note, however, that in most cases we discourage you from using goto because it violates good object-oriented programming practices.
5. loop statement
A loop statement repeats a specified block of code until a given condition is met.
1). for loop
In both languages, the syntax and operations of the for loop are the same:
for (initialization; condition; expression) statement;
2). foreach loop
C#introduces a new loop type called foreach loop (similar to Visual Basic's For Each). The foreach loop allows you to traverse every item in a container class that supports the IEnumerable interface (for example, arrays). The following code illustrates how to use the foreach statement to output the contents of an array:
public static void Main() { int[] arr1= new int[] {1,2,3,4,5,6}; foreach ( int i in arr1) { Console.WriteLine("Value is {0}", i); } }
In the Arrays in C#section, we'll look at arrays in C#in more detail.
3). while and do... while loop
In both languages, while and do... The syntax and operation of the while statement are the same:
while (condition) { //statements } //As usual, don't forget the trailing ; in do... while loops: do { //statements } while(condition); Thank you for reading, the above is the "C#flow control statement of the simple introduction" of the content, after learning this article, I believe you have a deeper understanding of the C#flow control statement of the simple introduction of this problem, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.