C programming

Chapter 4

Loop control instructions

Why Loops ?

Sometimes we want from our program to repeat one lines or something same task repeating again and again.

for example : print HYTEK 100 times. or print first 100 integer etc.

here loops are comes to rescue us.Hence loops make it easy for a programmer to tell computer that a given set of instructions must be executed repeatedly.

Types of loops :

  1. while loop
  2. do – while loop
  3. For loop

we will look into these one by one don’t you worry about it.

While Loop :

syntax look like this :

while( condition is true){

//code
//code Note that this block keeps executing as long as the condition True
}

An example
// Hello world in while loop

#include<stdio.h >

void main()
{
int i;

i=1;
while(i<=10)
{
printf(“%dHello While Loop “,i);
i++;
}
}

Output:

1Hello While Loop
2Hello While Loop
3Hello While Loop
4Hello While Loop
5Hello While Loop
6Hello While Loop
7Hello While Loop
8Hello While Loop
9Hello While Loop
10Hello While Loop

NOTE: if the condition never becomes false,the while loop keeps getting executed such a loop os known as an infinte loop

increment or decrement operators

i++ i is increased by 1
i-- i is decreased by 1

printf("i-- = %d",i--);

The first decrements i and then prints it

printf("i-- = %d",i--);

This first prints i and then decrements it

* +++ Operator does not exist //important
* --- is also not exist

* += is compound assignment operator
just like -= += /= %= they are also present in c

do – while loop

The syntax of do-while loop looks like this

do{


//code;
//code;

}while(condition)


do-while loop works very simillar to while loop
while checks the condition & then executes the code do-while executes the code & then checks the condition.
do-while loop = while loop which executes at least once.

//Simple Do-While Loop

#include <stdio.h >

int main()
{
int n = 0;

do
{
printf( ” Enter No.( 0 for exit) :: ” );
scanf( “%d”, &n );
}while( n != 0 );

return 0;
}

Output:

Enter No.( 0 for exit) :: 2
Enter No.( 0 for exit) :: 6
Enter No.( 0 for exit) :: 9
Enter No.( 0 for exit) :: 0

For loop :

The syntax of for loop looks like this

for( initialze ; test ; increment or decrement){
//code;
//code;

}

intialize ; Setting a loop counter to an initial value .

Test ; Checking a condition

increment/decrement ; Updating the loop counter

an example:
// print 1 to N number

#include<stdio.h &gt;

void main()
{
int i,j,n;

printf("Enter the value N:>>\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\n%d",i);
}

}

Output:
Enter the value N:>>
6

1
2
3
4
5
6

The Break statement in c

The break statement is used to exit the loop irrespective of whether the condition is true or false

whenever a “break” is encountered inside the loop the control is sent outside the loop let us see this example.

// Break statement

#include <stdio.h &gt;


int  main()
{
int i;


for( i=1 ; i<=10 ; i++ )
{
if(i == 5)
{
break;
}
printf( "%d\t", i );
}


return 0;
}

Output:

1 2 3 4

To continue statement in C

The continue statement is used to immediately move to  the next iteration of the loop. The control is taken to the next iteration thus skipping everything below “continue” inside the loop for that iteration.

Let us look at an example

// continue statement

#include <stdio.h &gt;
Int main()
{
int i;


for( i=1 ; i<=10 ; i++ )
{
if(i == 5)
{
continue;
}
printf( "  %d", i );
}


return 0;
}

Output:

  1  2  3  4  6  7  8  9  10

Notes

1. Sometimes the name of the variable might not indicate the behaviour of the program
2. Break statement completely exist the loop
3. Continue statement skips the particular iteration of the loop.