Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

7. C# Language, Part 3: Intransitive Verbs > Selection Statements

Selection Statements

Selection statements let you selectively execute code based on some condition. C# supports two selection statements, if and switch, and one selection operator, called the CONDITIONAL OPERATOR. Let’s start with the simplest: the if statement.

image

Make a Note

image

The if command and the #if directive have almost identical syntax, but they behave very differently. The directive controls the code that the compiler includes in the output. The command controls execution. All of the code involved in a C# if command is included in the output.



Put on Your Thinking Hat

image

To get some practice with the if statement, try translating these situations into code:

If x is greater than y, increment x; otherwise, set y equal to 12.

If a is equal to 5, then set x equal to 5; otherwise, if x is equal to 12, set y equal to 21, or if it’s not, set y equal to x.


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint