Nested if Statements in Visual C#.NET

Printer QR in Visual C#.NET Nested if Statements
Listing 321 Nested if Statements
Generate QR In C#.NET
Using Barcode generator for Visual Studio .NET Control to generate, create QR Code image in .NET framework applications.
1 class TicTacToeTrivia 2 { 3 static void Main() 4 { 5 int input; // Declare a variable to store the input 6 7 SystemConsoleWrite( 8 "What is the maximum number " + 9 "of turns in tictactoe " + 10 "(Enter 0 to exit): "); 11 12 // intParse() converts the ReadLine() 13 // return to an int data type 14 input = intParse(SystemConsoleReadLine()); 15 16 if (input <= 0) 17 // Input is less than or equal to 0 18 SystemConsoleWriteLine("Exiting"); 19 else 20 if (input < 9) 21 // Input is less than 9 22 SystemConsoleWriteLine( 23 "Tictactoe has more than {0}" + 24 " maximum turns", input); 25 else 26 if(input>9) 27 // Input is greater than 9 28 SystemConsoleWriteLine( 29 "Tictactoe has fewer than {0}" + 30 " maximum turns", input); 31 else 32 // Input equals 9 33 34 35 36 37 SystemConsoleWriteLine( "Correct, " + "tictactoe has a max of 9 turns"); } }
Barcode Generator In Visual C#
Using Barcode generator for VS .NET Control to generate, create barcode image in VS .NET applications.
Output 313 shows the results of Listing 321
QR Encoder In VS .NET
Using Barcode creation for ASP.NET Control to generate, create QR Code image in ASP.NET applications.
Output 313
QR Encoder In .NET
Using Barcode encoder for .NET Control to generate, create QR Code ISO/IEC18004 image in .NET applications.
[View full width]
Denso QR Bar Code Creation In Visual Basic .NET
Using Barcode maker for VS .NET Control to generate, create Denso QR Bar Code image in .NET applications.
What's the maximum number of turns in tictactoe (Enter 0 to exit): 9 Correct, tictactoe has a max of 9 turns
Create Bar Code In C#
Using Barcode generator for VS .NET Control to generate, create barcode image in Visual Studio .NET applications.
Assume the user enters a 9 when prompted at line 14 Here is the execution path
Bar Code Encoder In Visual C#.NET
Using Barcode creator for .NET framework Control to generate, create bar code image in .NET framework applications.
1 Line 16: Check if input is less than 0 Since it is not, jump to line 20 2 Line 20: Check if input is less than 9 Since it is not, jump to line 26 3 4
Code 39 Creation In Visual C#.NET
Using Barcode generator for VS .NET Control to generate, create USS Code 39 image in .NET framework applications.
1 2 3 Line 26: Check if input is greater than 9 Since it is not, jump to line 33 4 Line 33: Display that the answer was correct Listing 321 contains nested if statements To clarify the nesting, the lines are indented However, as you learned in 1, whitespace does not affect the execution path Without indenting and without newlines, the execution would be the same The code that appears in the nested if statement in Listing 322 is equivalent
Print Code 128B In Visual C#.NET
Using Barcode drawer for .NET framework Control to generate, create USS Code 128 image in .NET framework applications.
Listing 322 if/else Formatted Sequentially
Generating Data Matrix 2d Barcode In Visual C#
Using Barcode generation for .NET framework Control to generate, create Data Matrix ECC200 image in VS .NET applications.
if (input < 0) SystemConsoleWriteLine("Exiting"); else if (input < 9) SystemConsoleWriteLine( "Tictactoe has more than {0}" + " maximum turns", input); else if(input>9) SystemConsoleWriteLine( "Tictactoe has less than {0}" + " maximum turns", input); else SystemConsoleWriteLine( "Correct, tictactoe has a maximum of 9 turns");
Data Matrix 2d Barcode Drawer In Java
Using Barcode creator for Java Control to generate, create DataMatrix image in Java applications.
Although the latter format is more common, in each situation, use the format that results in the clearest code
Barcode Generator In .NET Framework
Using Barcode encoder for ASP.NET Control to generate, create barcode image in ASP.NET applications.
Code Blocks ({})
Drawing Bar Code In Java
Using Barcode printer for Java Control to generate, create barcode image in Java applications.
In the previous if statement examples, only one statement follows if and else, a single SystemConsoleWriteLine(), similar to Listing 323
Reading EAN13 In .NET Framework
Using Barcode scanner for Visual Studio .NET Control to read, scan read, scan image in .NET framework applications.
Listing 323 if Statement with No Code Block
Bar Code Maker In .NET Framework
Using Barcode creation for ASP.NET Control to generate, create barcode image in ASP.NET applications.
if(input<9) SystemConsoleWriteLine("Exiting");
Make EAN-13 In Java
Using Barcode drawer for Java Control to generate, create EAN 13 image in Java applications.
However, sometimes you might need to execute multiple statements Take, for example, the highlighted code block in the radius calculation in Listing 324
Draw Bar Code In Visual Studio .NET
Using Barcode printer for .NET framework Control to generate, create barcode image in Visual Studio .NET applications.
Listing 324 if Statement Followed by a Code Block
Bar Code Encoder In VB.NET
Using Barcode generator for VS .NET Control to generate, create bar code image in VS .NET applications.
class CircleAreaCalculator { static void Main() { double radius; // Declare a variable to store the radius double area; // Declare a variable to store the area SystemConsoleWrite("Enter the radius of the circle: "); // doubleParse converts the ReadLine() // return to a double radius = doubleParse(SystemConsoleReadLine()); if(radius>=0) { // Calculate the area of the circle area = 314*radius*radius; SystemConsoleWriteLine( "The area of the circle is: {0}", area); } else { SystemConsoleWriteLine( "{0} is not a valid radius", radius); } } }
Barcode Maker In Java
Using Barcode generator for Java Control to generate, create bar code image in Java applications.
Output 314 shows the results of Listing 324
Code 39 Extended Creator In Java
Using Barcode maker for Java Control to generate, create ANSI/AIM Code 39 image in Java applications.
Output 314
Enter the radius of the circle: 3 The area of the circle is: 2826
In this example, the if statement checks whether the radius is positive If so, the area of the circle is calculated and displayed; otherwise, an invalid radius message is displayed Notice that in this example, two statements follow the first if However, these two statements appear within curly braces The curly braces combine the statements into a single unit called a code block If you omit the curly braces that create a code block in Listing 324, only the statement immediately following the Boolean expression executes conditionally Subsequent statements will execute regardless of the if statement's Boolean expression The invalid code is shown in Listing 325