What is Loops in C language - Computer Times

Latest

Wednesday, 5 September 2018

What is Loops in C language

https://computertimeswithdinesh.blogspot.com/2018/09/what-is-loop-in-c-language.html









Looping in C Language

Loops have as purpose to repeat a statement a certain number of times or while a condition is fulfilled. We can say that when several parts (or a  Block) of a program is repeated again and again under a certain condition that is called a loop. There are three loops- 
1. while loop
2. do-while loop
3. for loop

The while loop-:

Its syntax is:
while (expression) 
{
   Statement 1;     
   Statement  2;
 }

and its functionality is simply to repeat statements while the condition set in expression is true. For example, we are going to make a program to countdown using a while-loop:

// custom countdown using while

#include <stdio.h>

void main ()
{
int n;
printf("Enter the starting number ");
scanf(“%d”,& n);
while (n>0)     
{     
  printf(“/t %d”, n) ;
      --n;    
}
printf(“Welcome to Computer Science Coaching Classes \n");
}

Output will be :-Enter the starting number  88, 7, 6, 5, 4, 3, 2, 1, 

When the program starts the user is prompted to insert a starting number for the countdown. Then the while loop begins, if the value entered by the user fulfills the condition n>0 (that n is greater than zero) the block that follows the condition will be executed and repeated while the condition (n>0) remains being true. 

Other examples of while loop -:

//while loop to print 10 natural numbers

int i=1;
while ( i<=10)
 {
        printf(“%d”,i);
        i=i+1;
   }

Output will be -:           
12345678910

//while loop to find factorial

int fact=1,num=5,i=1;
while (i<=num)
{
     fact=fact * i;
     i++;
}
printf(“factorial=%d”, fact);

Output will be -: factorial=120

  The do-while loop -:

Its syntax is :-
do

  Statements 2;  
  Statement 1; 
} while (condition);                       
             Its functionality is exactly the same as the while loop, except that condition in the do-while loop is evaluated after the execution of statement instead of before, granting at least one execution of a statement even if the condition is never fulfilled. For example,

#include <stdio.h>
void main()
{
int n;
do
{
   printf("Enter number (0 to end): ");
   scanf(“%d”, n);
   printf("\n You entered:=%d " , n );
while (n != 0);

}

output will be :Enter number (0 to end): 1234You entered: 1234Enter number (0 to end): 1602You entered: 1602

Enter number (0 to end): 0                                                                                 
You entered: 0                        
The do-while loop is usually used when the condition that has to determine the end of the loop is determined within the loop statement itself, like in the previous case, where the user input within the block is what is used to determine if the loop has to end. In fact if you never enter the value 0 in the previous example you can be prompted for more numbers forever.

The for loop

Its Syntax is:
for (initialization; condition; increase/ decrease) 

  statement1;    
  Statment2;
  }          
its main function is to repeat statements while the condition remains true, like the while loop. But in addition, the for loop provides specific locations to contain an initialization statement and an increased statement. So this loop is specially designed to perform a repetitive action with a counter which is initialized and increased on each iteration.
It works in the following way:

1. initialization is executed. Generally, it is an initial value of the setting for a counter variable. This is executed

only once.

2. condition is checked. If it is true the loop continues, otherwise, the loop ends and the statement is skipped

(not executed).

3. the statement is executed. As usual, it can be either a single statement or a block enclosed in braces { }.

4. finally, whatever is specified in the increase field is executed and the loop gets back to step 2.
// countdown using a for loop

#include <stdio.h>
void main ()
{
int n;
for ( n=10; n>0; n--)
{      
  printf(“%d”, n);}printf("Welcome to c!\n");
}

output will be:10 9 87654321 Welcome to c ! The initialization and increase fields are optional. They can remain empty, but in all cases, the semicolon signs between them must be written. For example we could write:
for (;n<10;) 

statement1

}    

Optionally, using the comma operator (,) we can specify more than one expression in any of the fields included in a for loop, like in initialization, for example. The comma operator (,) is an expression separator.
for ( n=0, i=100 ; n!=i ; n++, i-- )

{ 

  printf(“\n Hello”);

} 

n starts with a value of 0, and i with 100, the condition is n != i (that n is not equal to i). Because n is increased by one and i decreased by one, the loop's condition will become false after the 50th loop when both n and i will be equal to 50.that is the hello will be printing on 50 times.

1 comment: