Prevent a division by zero using the ? 1

image_pdfimage_print

/*
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

image_pdfimage_print

/*
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]

Demonstrate the short-circuit operators

image_pdfimage_print

   
 
/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Demonstrate the short-circuit operators. 
 
using System; 
 
public class SCops {    
  public static void Main() {    
    int n, d; 
 
    n = 10; 
    d = 2; 
    if(d != 0 &amp;&amp; (n % d) == 0) 
      Console.WriteLine(d + " is a factor of " + n); 
 
    d = 0; // now, set d to zero 
 
    // Since d is zero, the second operand is not evaluated. 
    if(d != 0 &amp;&amp; (n % d) == 0) 
      Console.WriteLine(d + " is a factor of " + n);  
     
    /* Now, try the same thing without short-circuit operator. 
       This will cause a divide-by-zero error.  */ 
    if(d != 0 &amp; (n % d) == 0) 
      Console.WriteLine(d + " is a factor of " + n); 
  }    
}


           
         
     


Demonstrate the relational and logical operators

image_pdfimage_print

   
 
/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Demonstrate the relational and logical operators. 
 
using System; 
 
public class RelLogOps {    
  public static void Main() {    
    int i, j; 
    bool b1, b2; 
 
    i = 10; 
    j = 11; 
    if(i < j) Console.WriteLine("i < j"); 
    if(i <= j) Console.WriteLine("i <= j"); 
    if(i != j) Console.WriteLine("i != j"); 
    if(i == j) Console.WriteLine("this won&#039;t execute"); 
    if(i >= j) Console.WriteLine("this won&#039;t execute"); 
    if(i > j) Console.WriteLine("this won&#039;t execute"); 
 
    b1 = true; 
    b2 = false; 
    if(b1 &amp; b2) Console.WriteLine("this won&#039;t execute"); 
    if(!(b1 &amp; b2)) Console.WriteLine("!(b1 &amp; b2) is true"); 
    if(b1 | b2) Console.WriteLine("b1 | b2 is true"); 
    if(b1 ^ b2) Console.WriteLine("b1 ^ b2 is true"); 
  }    
}

           
         
     


Demonstrate the difference between prefix postfix forms of ++

image_pdfimage_print

/*
C#: The Complete Reference
by Herbert Schildt

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
/*
Demonstrate the difference between prefix
postfix forms of ++.
*/
using System;

public class PrePostDemo {
public static void Main() {
int x, y;
int i;

x = 1;
Console.WriteLine(“Series generated using y = x + x++;”);
for(i = 0; i < 10; i++) { y = x + x++; // postfix ++ Console.WriteLine(y + " "); } Console.WriteLine(); x = 1; Console.WriteLine("Series generated using y = x + ++x;"); for(i = 0; i < 10; i++) { y = x + ++x; // prefix ++ Console.WriteLine(y + " "); } Console.WriteLine(); } } [/csharp]