/* Learning C# by Jesse Liberty Publisher: O'Reilly ISBN: 0596003765 */ using System; public class ThreeInputValues { static void Main() { int valueOne = 10; int valueTwo = 20; int maxValue = valueOne > valueTwo ? valueOne : valueTwo; Console.WriteLine("ValueOne: {0}, valueTwo: {1}, maxValue: {2}", valueOne, valueTwo, maxValue); } }
Language Basics
Illustrates the use of the Boolean logical operators
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example3_4.cs illustrates the use of
the Boolean logical operators
*/
public class Example3_4
{
public static void Main()
{
bool result;
// use of the Boolean logical AND operator
result = (1 == 1) && (2 > 1);
System.Console.WriteLine(“(1 == 1) && (2 > 1) is ” + result);
result = (1 == 1) && (2 < 1);
System.Console.WriteLine("(1 == 1) && (2 < 1) is " + result);
// use of the Boolean logical OR operator
result = (1 == 1) || (1 == 0);
System.Console.WriteLine("(1 == 1) || (1 == 0) is " + result);
result = (1 == 0) || (1 == 0);
System.Console.WriteLine("(1 == 0) || (1 == 0) is " + result);
// use of the Boolean logical NOT operator
result = !(1 == 0);
System.Console.WriteLine("!(1 == 0) is " + result);
result = !(1 == 1);
System.Console.WriteLine("!(1 == 1) is " + result);
}
}
[/csharp]
Illustrates the use of the comparison operators
/* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110 */ /* Example3_3.cs illustrates the use of the comparison operators */ public class Example3_3 { public static void Main() { bool result; // false expressions result = 10 == 1; System.Console.WriteLine("10 == 1 is " + result); result = 10 < 1; System.Console.WriteLine("10 < 1 is " + result); result = 10 <= 1; System.Console.WriteLine("10 <= 1 is " + result); // true expressions result = 10 != 1; System.Console.WriteLine("10 != 1 is " + result); result = 10 > 1; System.Console.WriteLine("10 > 1 is " + result); result = 10 >= 1; System.Console.WriteLine("10 >= 1 is " + result); int intValue1 = 10; int intValue2 = 1; result = intValue1 != intValue2; System.Console.WriteLine("intValue1 != intValue2 is " + result); } }
Self decrease
/* Learning C# by Jesse Liberty Publisher: O'Reilly ISBN: 0596003765 */ using System; public class SelfMiValues { static void Main() { int original = 10; int result; // increment then assign result = --original; Console.WriteLine("After prefix: {0}, {1}", original, result); // assign then increment result = original--; Console.WriteLine("After postfix: {0}, {1}", original, result); } }
Self increment
/* Learning C# by Jesse Liberty Publisher: O'Reilly ISBN: 0596003765 */ using System; public class SelfValues { static void Main() { int original = 10; int result; // increment then assign result = ++original; Console.WriteLine("After prefix: {0}, {1}", original, result); // assign then increment result = original++; Console.WriteLine("After postfix: {0}, {1}", original, result); } }
Prevent a division by zero using the ? 1
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Prevent a division by zero using the ?.
using System;
public class NoZeroDiv {
public static void Main() {
int result;
int i;
for(i = -5; i < 6; i++) { result = i != 0 ? 100 / i : 0; if(i != 0) Console.WriteLine("100 / " + i + " is " + result); } } } [/csharp]
Side-effects can be important
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Side-effects can be important.
using System;
public class SideEffects {
public static void Main() {
int i;
i = 0;
/* Here, i is still incremented even though
the if statement fails. */
if(false & (++i < 100))
Console.WriteLine("this won't be displayed");
Console.WriteLine("if statement executed: " + i); // displays 1
/* In this case, i is not incremented because
the short-circuit operator skips the increment. */
if(false && (++i < 100))
Console.WriteLine("this won't be displayed");
Console.WriteLine("if statement executed: " + i); // still 1 !!
}
}
[/csharp]