Prevent a division by zero using the ? 1 : Operators : Language Basics C# Source Code


Custom Search

C# Source Code » Language Basics » Operators »

 

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); 
    } 
  } 
} 


           
       
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Language Basics
» Operators