s1/c# Language and Database technology Basics/02-c# Grammar quick warm-up

Source: Internet
Author: User
Tags case statement

Switch structure: switch judgment in C # is more flexible, and the expression or variable that you judge can also be a string type.

The break statement is not required if no other statements are included in the case clause.

Attention:

1, the switch structure in the case clause is placed without order, you can put the default clause at the front, but note that any two case statements cannot have the same value.

2. The value in the case clause must be a constant expression, and no variable is allowed.

How to declare an array in Java:

data type [] array name;

data type [] array name;

However, in C # you can use the first method, that is, you cannot put the square brackets behind the array.

data type [] array name;

Arrays in C # and the syntax for initializing arrays in Java are the same.

The length property of the array allows you to get the lengths of the array, with the syntax as follows;

The array name. Length

As with Java, the elements that access the array are used in the following form, with the subscript starting at 0.

Array name [subscript]

Note: If you need to get the length of the array in your program, you should use the array name. Length "statement is obtained. Rather than the length defined by the array.

foreach Loop

Each is the meaning of each, and the Foreach loop structure is used to iterate over each item of a data set (which currently only learns an array), and you need to set an identifier (variable) to represent each of these items in turn. During a loop, you can perform the specified action on the data item.

Grammar:

foreach (element type element variable name in array)

{

code block

}

The "element" in the syntax is a read-only variable.

The execution of a foreach structure is that the loop body takes each element of the array sequentially, and then performs a loop-body operation on each element.

The foreach structure gets an element of the array in each loop, placing the value of the element in the variable temp of type int and then executing the code of the loop body once. The type of the TEMP variable must be the same as the data type of the array.

Note: The foreach structure cannot change the values of the elements in the array, and the For loop should be used if you need to change the values of the elements in the array.

Jump statement

Break statement: It can be used in a for-, while, Do-while, foreach-loop statement, except in the switch structure, by exiting a case statement with the escape statement. The break statement causes the program to jump out of the current loop structure and continue executing the statement following the loop.

Continue statement: The continue statement is similar to the break statement. The purpose of the continue statement, however, is to exit this loop of the current loop structure and start executing the next loop of the current loop structure instead of exiting the current loop structure.

In nested loops, when the break statement and the continue statement appear in the inner loop, only the execution of the inner loop is affected, and the outer layer loop has no effect. The break statement is a statement that jumps out of the loop to continue the outer loop, and the continue statement jumps out of the loop and starts the next loop.

Double cycle

In the double loop, we use the outer loop to control the number of outputs, and the inner loop to control the number of outputs per row.

Bubble Sort formula:

N numbers to line up, 22 compared to a small front.

Outer loop N-1, inner loop n-1-i.

If you want to sort in descending order, just change the greater-than sign in the program to less than the number.

s1/c# Language and Database technology Basics/02-c# Grammar quick warm-up

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.