CSharp – if Statement
With if statement you can set up conditional blocks of code.
if – else if – else
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | using System; namespace ConsoleApplication1 { class Program { static void Main() { Console.Write( "Enter a character: " ); char ch = ( char )Console.Read(); if (Char.IsUpper(ch)) { Console.WriteLine( "The character is an uppercase letter." ); } else if (Char.IsLower(ch)) { Console.WriteLine( "The character is a lowercase letter." ); } else if (Char.IsDigit(ch)) { Console.WriteLine( "The character is a number." ); } else { Console.WriteLine( "The character is not alphanumeric." ); } } // End Main() } } |
if – nested
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | using System; namespace ConsoleApplication1 { class Program { static void Main() { Console.Write( "Enter a character: " ); char c = ( char )Console.Read(); if (Char.IsLetter(c)) { if (Char.IsLower(c)) { Console.WriteLine( "The character is lowercase." ); } else { Console.WriteLine( "The character is uppercase." ); } } else { Console.WriteLine( "The character isn't an alphabetic character." ); } } // End Main() } } |
if – boolean
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | using System; namespace ConsoleApplication1 { class Program { static void Main() { // Change the values of these variables to test the results. bool Condition1 = true ; bool Condition2 = true ; bool Condition3 = true ; bool Condition4 = true ; if (Condition1) { // Condition1 is true. } else if (Condition2) { // Condition1 is false and Condition2 is true. } else if (Condition3) { if (Condition4) { // Condition1 and Condition2 are false. Condition3 and Condition4 are true. } else { // Condition1, Condition2, and Condition4 are false. Condition3 is true. } } else { // Condition1, Condition2, and Condition3 are false. } } // End Main() } } |
if – AND NOT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | using System; namespace ConsoleApplication1 { class Program { static void Main() { // NOT bool result = true ; if (!result) { Console.WriteLine( "The condition is true (result is false)." ); } else { Console.WriteLine( "The condition is false (result is true)." ); } // Short-circuit AND int m = 9; int n = 7; int p = 5; if (m >= n && m >= p) { Console.WriteLine( "Nothing is larger than m." ); } // AND and NOT if (m >= n && !(p > m)) { Console.WriteLine( "Nothing is larger than m." ); } // Short-circuit OR if (m > n || m > p) { Console.WriteLine( "m isn't the smallest." ); } // NOT and OR m = 4; if (!(m >= n || m >= p)) { Console.WriteLine( "Now m is the smallest." ); } // Output: // The condition is false (result is true). // Nothing is larger than m. // Nothing is larger than m. // m isn't the smallest. // Now m is the smallest. } // End Main() } } |
My Website: http://www.lucedigitale.com
References:
http://msdn.microsoft.com/it-it/library/5011f09h.aspx
http://csharp.net-tutorials.com/basics/if-statement/