Oct 13, 2019

LOOPING


WHAT IS LOOP:

 Loop is a mechanism that allow us to do the task again and again just by writing once”.
  Where looping is the continuous repetition of a statement or a block of statements. But for the  looping some work have to do that's we will have to initialize the loop, put conditions and do increment or decrement to the loop.  

INITIALIZATION:

  Initialization, it is a part of loop in which we initialized the variable that explained that from where the loop will be start. To do this we assigned a fixed value to the variable like suppose a is variable then use a = 0;

CONDITION:

 In this part of loop we explained what the condition at which the loop stop looping. To do this we have to put condition to the variable by considering above example a<=10. This mean the iteration will stop when the given condition become false. Condition can be called as Boolean condition.

INCREMENT/DECREMENT:

  The last part of looping, in which we increase the variable by a fixed value or decrease, this part explained that by end of each iteration how many value will increase or decrease. It can be made by two methods which is
          a.       Pre –increment or decrement operator
          b.      Post –increment or decrement  operator

PRE-INCREMENT OR DECREMENT OPERATOR:
  “This operator increase or decrease the value by one before assigning the value to the variable”. It means that first increase/decrease the value and then assigned the value to the variable.
Syntax:
++variable name   And   -- variable name
For example, suppose a is desired variable now do it.
++ a  or  - - a

POST-INCREMENT OR DECREMENT OPERATOR:
  This operator first assigned the value to the variable and then increase and decrease by one or by given value.
Syntax:
    Variable name ++  Or   Variable-name = Variable name + required value And
    Variable name - -  Or   Variable-name = Variable name - required value
For example, suppose a is desired variable now do it.
For increment (a ++, a = a+5 or a = a+4 etc.)
For decrement (a - -, a=a-5 or a=a-4 etc.)

LOOPING:

  The continuous repetition of any statement or block of statement is called looping”.
   This type of program is come in the iteration statement program type. Where we have three type of statement program type that is.
       1.       Sequential statement
       2.       Conditional statement
       3.       Iteration statement

TYPES OF LOOP:

Three type of method is used for looping.
       1.       For loop
       2.       While loop
       3.       Do while loop
 4.    Foreach loop

FOR LOOP:

  For loop is a control flow statement which is used for repeating a statement or block of statements, when the number of iteration is predefined”.

SYNTAX:
               for (initialization; condition; increment/decrement)
               {
                  Statement or block of statement;
               }
  Note that if only one statement execute under the control of loop no need to write the statement in between braces. Otherwise the statement must enclose in braces.

Example program:
·         Program that print number from 0 to 10.
#include<stdio.h>
void main (void)
 {
   int a;
   for (a=0; a<=10; a++)
   printf (“%d,”, a);
   getch ();
}

WHILE LOOP:

  While loop is a control flow statement use for looping when the number of iteration is not pre-defined “.
  It’s mean that this operator is use when we have to do iteration infinite time. But also it can use for the predefine iteration. Below is an example of this.

SYNTAX:
               initialization;
               while (condition)
               {
                  Statement or block of statement;
                  increment/decrement;
               }

Example program:
 Do above program using while loop.
#include<stdio.h>
void main (void)
 {
   int a;
   a=0;
   while(a<=10)
  {
    printf (“%d,”, a);
    a++;
  }
  getch ();
}


DO-WHILE LOOP:

Do while loop also a control flow statement use for iteration. It executes the block of code at least once”.
   It is used when the statement under control of loop should execute at least once, and then repeatedly execute of the block of statement   or not depending upon the given Boolean condition.

Syntax:
Initialization;
Do
{
Statement or block of statement;
Increment/decrement;
}while(condition);

Example program:
 Do above program using do while loop.
#include<stdio.h>
int main ()
{
  int a;
  a=0;
   do
       {
         printf("\n I love myself");
         a++;
       }while (a<=10);
    getch();
}

FOREACH LOOP:

 The foreach loop in C iterate a block of code on each element in an array or a collection of item”.
Foreach (element in iterate able-item )
{
  Body of foreach loop;
}

NESTED LOOP:

 Loop in a loop is called nested loop
 If in any program loop is used in a loop then that will called nested loop.

Example syntax:
               for (initialization; condition; increment/decrement)
               {
                      for (initialization; condition; increment/decrement)
                        {
                           Statement or block of statement which has to execute under control of for loop;
                        }

                }
 Example program:
  Write a program that prints,
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

#include<stdio.h>
void main (void)
{
   int a, b;
   for (a=1; a<=5; a++)
  {
     printf(“\n”);
     for (b=1; b <= a; b++)
     printf(“%d”, b);
  }
}

c# for loop, C#while loop, C++for loop, C++looping
Looping in C, C++


 Download Page article in PDF
           


0 comments:

Post a Comment

Please do not enter any spam link in the comment box