Mohammad  Aadil

Mohammad Aadil

  • NA
  • 15
  • 2.1k

post-increment and post-decrement

Feb 21 2013 6:13 AM
hello seniors ,my name is Aadil and yet i am a student of b.tech 
i have a problem with working style of operator  that is post-increment and post-decrement operator and i want anyone to solve this 
 i know that what does these operator do .. but the problem is i dont know how does these work . how does complier do these thing for example i have a program 


example of factorial :-


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


class myclass
{
    public int factorial(int num)
    {
        if (num == 1)
        {
            return 1;
        }
        else
        {
            Console.WriteLine(num);
       
            return factorial(num - 1) * num;
           
        }
 }


    class hello
    {
        static void Main(string[] args)
        {
            myclass cls = new myclass();
            Console.WriteLine(cls.factorial(4));
                Console.ReadLine();
        }
    } 
}


 result is 24 


*****************************    

now with decrement operator 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


class myclass
{
    public int factorial(int num)
    {
        if (num == 1)
        {
            return 1;
        }
        else
        {
            Console.WriteLine(num);
       
            return factorial(num--) * num;
           
        }


       
    }


    class hello
    {
        static void Main(string[] args)
        {
            myclass cls = new myclass();
            Console.WriteLine(cls.factorial(4));
                Console.ReadLine();
        }
    }
}
   

now the result is  44444....... in infinity it mean having bug n program it not executing the program is terminated itself so i want to know why these bug in this condition and when i use  " pre-decrement"  operator its working ? why so, so pls anyone explain me complete detail about these operator
                                                                                              thank you  

Answers (3)