Branching Out with if and switch in C#.NET
Branching Out with if and switch Code39 Generation In Visual C# Using Barcode maker for Visual Studio .NET Control to generate, create Code-39 image in .NET framework applications. Book I 5
Making Barcode In C# Using Barcode generation for .NET framework Control to generate, create barcode image in Visual Studio .NET applications. widowed (surely I covered all the options oh, wait), or 4 for none of your business To differentiate among these values, you could use the following series of if statements: Draw Code 39 Full ASCII In .NET Using Barcode drawer for Visual Studio .NET Control to generate, create Code-39 image in .NET framework applications. if (maritalStatus == 0) { // Must be unmarried // do something } else { if (maritalStatus == 1) { // Must be married // do something Print Code 39 In VB.NET Using Barcode encoder for .NET Control to generate, create Code 3 of 9 image in .NET applications. Getting Into the Program Flow
Generate Barcode In C#.NET Using Barcode drawer for VS .NET Control to generate, create barcode image in .NET applications. else
Code 128 Encoder In C#.NET Using Barcode printer for .NET framework Control to generate, create Code 128C image in VS .NET applications. And so on You can see that these repetitive if statements grow tiresome quickly Testing for multiple cases is such a common occurrence that C# provides a special construct to decide between a set of mutually exclusive conditions This control, the switch, works as follows: Creating GS1 128 In C#.NET Using Barcode generation for Visual Studio .NET Control to generate, create GS1 128 image in VS .NET applications. switch(maritalStatus) { case 0: // do the unmarried stuff break; case 1: // do the married stuff break; case 2: // do the divorced stuff break; case 3: // do the widowed stuff break; case 4: // get out of my face break; default: // Goes here if it fails to pass a case; // this is probably an error condition break; } Painting UPC-A In C#.NET Using Barcode creator for VS .NET Control to generate, create Universal Product Code version A image in .NET framework applications. The expression at the top of the switch statement is evaluated In this case, the expression is simply the variable maritalStatus The value of that expression is then compared against the value of each of the cases Control passes to the default clause if no match is found The argument to the switch statement can also be a string: Encode EAN 13 In Visual C# Using Barcode printer for VS .NET Control to generate, create EAN / UCC - 13 image in VS .NET applications. string s = Davis ; switch(s) { case Davis : Drawing Bar Code In C#.NET Using Barcode creator for Visual Studio .NET Control to generate, create bar code image in .NET framework applications. Branching Out with if and switch
Intelligent Mail Creation In C# Using Barcode drawer for Visual Studio .NET Control to generate, create Intelligent Mail image in .NET applications. // break; case Smith : // break; case Jones : // break; case Hvidsten : // break; default: // Goes break; } Code 3/9 Generator In Visual Basic .NET Using Barcode maker for VS .NET Control to generate, create ANSI/AIM Code 39 image in Visual Studio .NET applications. control will actually pass here
Creating Barcode In Java Using Barcode creation for Java Control to generate, create barcode image in Java applications. do Smith stuff
Recognize Code128 In .NET Framework Using Barcode recognizer for .NET Control to read, scan read, scan image in VS .NET applications. do Jones stuff
Painting Code 128C In VS .NET Using Barcode encoder for VS .NET Control to generate, create USS Code 128 image in Visual Studio .NET applications. do Hvidsten stuff
Painting Barcode In Visual Basic .NET Using Barcode generator for .NET Control to generate, create bar code image in VS .NET applications. here if it doesn t pass any cases
Printing GS1 - 12 In Java Using Barcode maker for Java Control to generate, create UPC-A image in Java applications. Using the switch statement involves these severe restrictions: The argument to the switch() must be one of the counting types (including char) or a string Floating-point values are excluded The various case values must refer to values of the same type as the switch expression The case values must be constant in the sense that their value must be known at compile time (A statement such as case x isn t legal unless x is a type of constant) Each clause must end in a break statement (or another exit command, such as return) The break statement passes control out of the switch You can omit a break statement if two cases lead to the same actions: A single case clause may have more than one case label, as in this example: EAN 128 Generation In .NET Using Barcode creation for Visual Studio .NET Control to generate, create EAN / UCC - 13 image in .NET applications. string s = Davis ; switch(s) { case Davis : case Hvidsten : // Do the same thing whether s is Davis or Hvidsten // since they re related break; case Smith : // do Smith stuff break; default: // Goes here if it doesn t pass any cases break; } Draw EAN / UCC - 13 In Visual Basic .NET Using Barcode maker for .NET framework Control to generate, create USS-128 image in VS .NET applications. This approach enables the program to perform the same operation, whether the input is Davis or Hvidsten The SwitchSyntaxTest example on the Web site illustrates a variety of advice about using switch The final section of this chapter supplies a small addendum to the switch story You can find the code at csharp102info Decoding Bar Code In Visual Studio .NET Using Barcode reader for .NET Control to read, scan read, scan image in .NET framework applications. Here We Go Loop-the-Loop
Book I 5
Here We Go Loop-the-Loop
The if statement enables a program to take different paths through the code being executed depending on the results of a bool expression This statement provides for drastically more interesting programs than programs without decision-making capability Adding the ability to execute a set of instructions repeatedly adds another quantum jump in capability Consider the CalculateInterest program from the section Introducing the if statement, earlier in this chapter Performing this simple interest calculation by using a calculator (or by hand, using a piece of paper) would be much easier than writing and executing a program If you could calculate the amount of principal for each of several succeeding years, that would even more useful A simple macro in a Microsoft Excel spreadsheet is still easier to handle, but at least you re getting closer What you need is a way for the computer to execute the same short sequence of instructions multiple times known as a loop
|
|
|
|
| ALL RIGHTS RESERVED. Business Refinery (c) 2006 - 2010. | Terms of Use | Privacy Policy |