01-C # entry (branch control statement)

Source: Internet
Author: User

To be honest, the book "C # Getting Started classic" is really great for beginners. The author first uses the console (command line) to debug the program without talking about the content, you can focus on the learned content to the maximum, instead of the fancy interface debugging.

Learning to download an ebook is inconvenient for both reviewing and taking notes.If I can take notes and finish the first seven chapters, I will buy this book on Amazon.

  • Ternary OPERATOR :?

This operator is used in many languages and has the same principle. The difference may be in the writing format. I used to excel. I always wanted to separate multiple expressions with commas. What is the ternary operator in C? In, the two returned expressions are separated by signs instead of signs. Note the following:

String h = (k <10 )? "K less than 10": "k greater than or equal to 10 ";

Of course, this expression is suitable for processing returned values after simple comparison, but it is not suitable for executing a large amount of code based on comparison results.

  • IF statement

IF statements do not return values (unlike? ), And can execute complex statement code blocks, surrounded by {}, it is OK. Similar to other languages: if ...... Elseif ...... Else ......, Let's take an example to see the compilation results:

String msg; double I, j; Console. writeLine ("enter a value of 1st:"); I = Convert. toDouble (Console. readLine (); Console. writeLine ("enter a value of 2nd:"); j = Convert. toDouble (Console. readLine (); if (I> j) {msg = "greater than";} else if (I = j) {msg = "equal ";} else {msg = "less than";} Console. writeLine ("first operand {0} second operand", msg); Console. readKey ();

This example is relatively simple, but shows a programming idea. In the past, if you are willing to write, you will add Console. WriteLine to each if statement block to output the results. Here, you only need one output statement. Pay more attention to this in the future.

  • Switch statement

Although the IF statement is easy to use, it is difficult to read discrete values when compared with IF statements. In addition, the switch matches the expression value with the option, rather than the if condition judgment (>, <). The value to be matched must be a constant value, which can be a literal value (1, 2, 3) or a constant:

Const string myName = "karli"; const string sexyName = "angelina"; const string sillyName = "ploppy"; string name; Console. writeLine ("enter your name:"); name = Console. readLine (); switch (name. toLower () {case myName: Console. writeLine ("You and I have the same name, {0}", myName); break; case sexyName: Console. writeLine ("Wow ~, {0} name is so sexy. ", SexyName); break; case sillyName: case" h ": {Console. WriteLine (" {0} This name is really too beautiful ~~ ", SillyName); Console. WriteLine (" of course ~ "); Break;} default: Console. WriteLine (" hello {0} ", name); break;} Console. ReadKey ();

A break is required for the statement executed in each case, including the default statement. In addition, case can also be stacked. If one of them meets the conditions, the subsequent statements will be executed. After case, you can use {} to enclose the statement blocks and execute multiple statements.

  • Conclusion

In fact, in the course of learning, it is very important to write and debug examples. It not only improves your impression, but also triggers your thinking. Of course, it would be nice to have some small programmers. If you have any need or ideas, contact me.

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.