Monday, July 26, 2010

Introduction To Loops

Conditional Statements

                  1)IF Conditon

                  2)SWITCH Condition

I)                  IF Condion

Syntax1:

      if 

           Statement 1;

   Else

      Statement 2;

 Syntax2:

  If 

        {

           statement 1;

         statement 2;

         }

 else

     {

      statement 3;

      statement 4;

 

     }

Syntax3:

If    //nested if condition

      Statement 1;

   Else if

               Statement 2;

    

Else if

       

         Statement 3;

   Else

    

        Statement 4;

 

Example:

      Comparing 2 numbers

private void button1_Click(object sender, EventArgs e)

        {

             int i=10,j=20;

                 if(I>j)

 

                MessageBox.Show("i is greater than j");

              

             Else

             

               Messagebox.show(“j is greater than I”);

 

 

        }

 

I)                  SWITCH Condition:

 

Syntax:

 

               Switch

              

                 {

                          case 1:

 

                                statement 1;

                                statement 2;

                                break;

         

                       case 2:

                       

                          statement 3;

                          statement 4;

  

                       break;

 

                   default:

                     

                       statement 5;

                       statement 6;

 

                          break;

    

        

  }

 

LOOPS:

1)    For Loop:

2) while Loop:

3) Do while Loop:

 

1       Executing body once is called as one iteration

2       When number of iterations are defined use for loop

3       When number of iterations are defined use while loop

4       When number of iterations are not defined but to execute at least once use do-while loop

Syntax:

 

   For (Initialization; condition;++/--)

         {

            statement 1;

           statement2;

       }

 

Example1: write a program to print from 1 to 10?

class Program

    {

        static void Main(string[] args)

        {

            for (int i = 1; i <= 10; i++)

 

                Console.WriteLine(i);

            Console.ReadLine();

 

 

        }

    }

Syntax of while loop:

 

      While(condition)

         {

 

            statement1;

            statement2;

            statement3;

   

        }

Syntax of do while loop:

 

       Do

          {

 

          statement 1;

          statement 2;

          statement 3;

 

         }

 

    while(condition)


No comments:

Post a Comment