8. C-Nesting Of Loops

C-Loop Constructs: Previous                                                     Next: C-Programming Examples

How to Nest Loops??  

In C programming, C-Nesting Of Loops means when we have a loop in another loop. Nesting of loop is possible with every three loop constructs i.e. while, do while and for loop. In nested loop construction, the inner lop must terminate before the outer loop can mended.
Syntax:    Outer for loop
                {
                                Inner for loop
                                {
                                                Statements;             
                                }
                }

Eg:           for(i=0; i <= 20; i++)
{
for(j = 0; j <= 2; j++)
{
printf(“manisha”);
}
}
The variable`s name must be different for both loops.

Case control: -

After the conditional branches and loop construct and loop construct we have another type of construct which helps us to take a decision i.e. case control i.e. switch. switch is used to handle multiple choices. It works same as if else if ladder but every condition can`t convert from if else if to switch.
There are some following characteristics that must be exit for translating if else if ladder into switch :
  • More than two choices must exist.
  • Logical OR in every condition.
  • = =, relational operator is not allowed
  • Same variable must use in every condition and data type of that variable can only be integer and char.
The switch control statement starts with keyword ‘switch’ followed by comparing variable/constant and contains multiple case statements.
Syntax:    switch <variable>
                {
                                case variable/constant:
                                                statement(s);
                                                break;
                                case variable/constant:
                                                statement(s);
                                                break;
                                default:
                                                statement(s);
                }
Now, according to syntax the control flows like it compares the first case, if it is true then statement will get executed and controls come out of the loop with break statement. Only one case will get evaluated.
Eg:           Write a program to calculate bonus on entered basic salary of an employee. Bonus is to be given according to following deptno.
Detpno                                     Bonus
10                                            20% of basic
20                                            18% of basic
30                                            15% of basic
Solution:  #include<stdio.h>
                                void main( )
                                {
                                                float sal;
                                                int dptno;
                                                printf(“Enter the basic salary : ”);
                                                scanf(“%f”, &sal);
                                                printf(“Enter the department number : ”);
                                                scanf(“%d”, &dptno);
                                               
                                                switch(dptno)
                                                {
                                                                case 10:
                                                                                sal = sal * 1.10;
                                                                                break;
                                                                case 20:
                                                                                sal = sal * 1.18;
                                                                                break;
                                                                case 30:
                                                                                sal = sal * 1.15;      
                                                }
                                                printf(“Salary is : %f”, sal);
                                }
Output:

  • default block is not compulsory to write it’s just according to situation.
  • In every case, break statement is compulsory to write when we have different statement to print in different case statement block.
Eg:   Write a program to take input of character and check for vowel or consonant using switch case?
Solution:          #include<stdio.h>
                                                void main( )
                                                {
                                                                char ch;
                                                                printf(“Enter a character: ”);
                                                                scanf(“%c”, &ch);
                                               
                                                                switch(ch)
                                                                {
                                                                                case ‘a’:
case ‘e’:
case ‘i’:
case ‘o’:  
case ‘u’:
                                                                                                printf(“Character is Vowel”);
                                                                                                break;
                                                                                default:
                                                                                                printf(“Character is Consonant”);
                                                                }
                                                }
Output:

Jump statements: -

These statements are used to skip some statements and take the control out of the loop or at the specific specified statement within a program.
Three Jump statements are provided by C.
  1. break
  2. continue
  3. goto
  1. break: - the break statement can also be used with switch case, that we studied earlier. If you need to come out from running block on a specific condition without executing the rest of the statements, then break statement is used.
Syntax:    break;
Semicolon is compulsory. The break is used in while/for/do while also.
Eg:           Write a program to accept non-zero numbers only to an inputted limit and exits when user entered zero.
Solution:  #include<stdio.h>
                #include<conio.h>
                                                void main( )
                                                {
                                                                int limit, i, num;
               
                                                                printf(“Enter Limit : ”);
                                                                scanf(“%d”, &limit);
                                                                for(i=0; i < limit; i++)
                                                                {
                                                                                printf(“Enter Number : ”);
                                                                                scanf(“%d”, &num);
                                                                                if(num = = 0)
                                                                                {
                                                                                                printf(“Number is Zero”);
                                                                                                break;
                                                                                }
                                                                }
                                                                getch();
                                                }
Output:
  1. Continue: - the working of break and continue is just opposite to each other. The break statement take control just out of the current block, whereas continue take control at the starting of the loop. It is used with loops only.
Starting of loop
{
Statements;
continue;
Statements;
break;
}
Eg:           write a program to accept non-zero numbers only to an inputted limit and take input again when user entered zero.
Solution:  #include<stdio.h>
                #include<conio.h>
                                                void main( )
                                                {
                                                                int limit, i, num;
               
                                                                printf(“Enter Limit : ”);
                                                                scanf(“%d”, &limit);
                                                                for(i=0; i < limit; i++)
                                                                {
                                                                                printf(“Enter Number : ”);
                                                                                scanf(“%d”, &num);
                                                                                if(num = = 0)
                                                                                {
                                                                                                printf(“Number is Zero”);
                                                                                                continue;
                                                                                }
                                                                }
                                                                getch();
                                                }

  1. goto: - it is an unconditional jump statement. This statement is used to transfer the control at the specific statement of the program. It can be used at place of loops. Name of specific statement or location is refers to label followed by colon( : ). It means goto statement takes control where it finds the label.

Conditions for writing a label name in C Programming:

  • Spaces are not allowed.
  • First character must be a letter not a digit.
  • Underscore is only allowed.
  • Keywords are not allowed.
Syntax:
Statements;                                                                              Statements;
--------------;                                                                            --------------;
--------------;                                                                            --------------;
--------------;                                                                            --------------;
<label name> :                                                         goto <label name>;
Statements;                                                                              Statements;
--------------;                                                                            --------------;
--------------;                                                                            --------------;
--------------;                                                                            --------------;
goto <label name>;                                                   <label name> :
Statements;                                                                              Statements;
--------------;                                                                            --------------;
--------------;                                                                            --------------;
--------------;                                                                            --------------;


Eg:           write a program to take input only non-zero number.
Solution:  #include<stdio.h>
                #include<conio.h>
                                                void main( )
                                                {
                                                                int num;
                                                    num:
                                                                printf(“Enter Number : ”);
                                                                scanf(“%d”, &num);
                                                                if(num = = 0)
                                                                {
                                                                                printf(“Number is Zero”);
                                                                                goto num;
                                                                }
                                                }
C-Loop Constructs: Previous                                                     Next: C-Programming Examples