Monday, June 28, 2021

Loop Control Instructions

Loop Control Instruction



Loop control instruction is used to write a program that executes over and over again till the program meets the need. Loop control executes again and again and gives variation in detail. Some programmer needs to find for example table of 8 for which he has to write a big program but loop control instruction helps them to write a short program for the same process which you will learn below.

There are three methods to write a program for loop control:

  1. While Statement
  2. Do-while loop Statement
  3. For loop Statement

While Statement

While statement is used to write a loop program that executes till the condition meets.

Syntax of while statement is as follows:

    while (/* condition */)

    {

        /* code */

    }

For example:

Write a program for multiplication of 8 by while loop?

#include <stdio.h>

int main()

{

    int n, i=1;

    printf("Enter a Number ");

    scanf("%d", &n);

    while (i <= 10)

    {

 

        printf("%d * %d = %d \n", n, i, n * i);

        ++i;

    }

 

    return 0;

}

The output of the multiplication table


Note:

In loop control instruction, if the condition never becomes false then it keeps executing till memory end or you force to stop the program. Such type of loop program is infinite loop.

For example

Write a program for multiplication of 8 till infinite by while loop?

#include <stdio.h>

int main()

{

    int n, i=1;

    printf("Enter a Number ");

    scanf("%d", &n);

    while (i > 10)

    {

 

        printf("%d * %d = %d \n", n, i, n * i);

        ++i;

    }

 

    return 0;

}

Now we will discuss

Increment and Decrement operator

Increment (i++) – i will be increased by 1

Decrement (i--) – i will be decreased by 1

For example

Write a program which give output in increasing order 1-10?

 #include<stdio.h>

 

int main(){

    int i=1;

    while (i<=10){

        printf("%d\n",i);

        i++;

    }

return 0;

}

Compound Assignment Operator

+=, -=, *=, /=, and %= are compound assignment

Operator

Name of Operator

==

Equal to

!=

Not equal to

+=

Addition assignment operator

-=

Subtraction assignment operator

*=

Multiplication assignment operator

/=

Division assignment operator

%=

Modular assignment operator

 

For example,

a+=b; This means a=a+b.

a-=b; This means a=a-b.

a*=b; This means a=a*b.

a/=b; This means a=a/b.

Do-While Loop

A do-While statement is used to write a loop program that executes code and then check the condition.

Syntax of Do-While Statement

  do

    {

        /* code */

    } while (/* condition */);

For example

Question Write a program which follows the following reasoning

2,3,5,8,12,17,23

Answer:

#include <stdio.h>

 

int main()

{

    int a=2, b = 0;

    do

    {

        a += b;

        ++b;

        printf("Value of a is %d\n", a);

    } while (b <=6);

    return 0;

}



For loop

For loop is most popular among programmer. For loop allow us to specify three things in a single-line.

Syntax of For loop:

for (size_t i = 0; i < count; i++)

        {

            /* code */

        }

Size_t i=0: Here we initialize or we command from where to start.

i < count: Testing the condition.

i++: Here we give an increment operator or modify the loop in executing value.

For example

Question. Write a program for multiplication table by for loop?

Answer

#include<stdio.h>

 

int main(){

    int a,mul;

    printf("Multiplication table: ");

    scanf("%d",&a);

   

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

    {

        mul=a*(i+1);

        printf("%d * %d = %d\n",a,i+1,mul);

    }

   

return 0;

}

Output

Nested Loops

 Loops can be nested like
For example:

Question Write a program to print pattern 1 using for loop. 

          Pattern 1

         *

         * *

         * * *

         * * * *

         * * * * *

Answer

          #include <stdio.h>

          int main() {

          int i, j, rows;

          printf("Enter the number of rows: ");

          scanf("%d", &rows);

          for (i = 1; i <= rows; ++i) {

          for (j = 1; j <= i; ++j) {

          printf("* ");

          }

          printf("\n");

          }

          return 0;

          }

OUTPUT:


Break Statement

To exit the loop whether the condition is true or false, we use the break statement. When break is encountered then loop gets exit.
For example:
Question. Write a program that uses break statement in C?
Answer
 #include<stdio.h>

int main(){
    int a;
    for ( a = 0; a < 5; a++)
    {
        printf("Incresing value %d\n", a);
        if (a==3)
        {
            break;
        }   
    }  
return 0;
}
 Output

Continue Statement

Continue statement skips to the next iteration. It skips any code between loops.
For example:
Question Write a program to show how the continue statement works?
Answer
#include<stdio.h>  
int main(){  
int a;   
for(a=1;a<=5;a++){      
if(a==3){  
continue;    
}    
printf("%d \n",a);    
}   
return 0;  
}  
Output

For more concepts, you can query with us
Keep visiting Our Website MessWithAge

No comments:

Post a Comment

Please do not enter any spam link in the comment box

Best of Our Website

Popular Posts