Problem Analysis
Pattern in C language requires iteration of a set of programming instructions. Objective of pattern programs in C language is to provide insight into the working of nested loops.
Nesting of a loop is possible by placing one loop within another loop. Nesting of loops is done to achieve the desired objective and implement program logic.
C language support three types of loops –
- while loop
- for loop
- do…while loop
while loop:
First checks for a given terminating condition, if it is true then programming instructions placed within the loop are executed. After executing the programming instructions within the loop once while loop again checks for terminating condition, if terminating condition is true then it again executes programming instructions within the loop and if it is false then while loop is terminated.
The while loop is also called entry control loop because first it checks the terminating condition if it is false then programming instructions within the loop is not executed even once.
for loop:
for loop executes programming instructions within the loop multiple times. Number of times programming instructions within the loop is executed depends on the terminating condition of the for loop.
If the terminating condition of the for loop is false then programming instructions within the for loop are not executed even once. If the terminating condition of the for loop is true then programming instructions within the for loop is executed, after execution, terminating condition of the for loop is checked again if the terminating condition is true then programming instructions within the loop is executed again and if the terminating condition is false then for loop execution ends.
Thus programming instructions within the loop are executed until the terminating condition within the loop becomes false.
do…while loop:
do….while loop executes programming instructions within the loop at least once, thus do…while loop guarantees the execution of the programming instruction within the loop at least once. After the execution of the programming instructions once, terminating condition of the do…while loop is checked if it is true then programming within the loop is executed again and if the terminating condition is false than the do…while loop is terminated.
do…while loop is also called exit controlled loop as programming instruction within the loop is first executed then the loop terminating condition is checked if terminating condition is true then programming instructions are executed again otherwise the loop is terminated or we say it gets exit.
Thus, execution of all three loops is controlled by terminating conditions. If it is required to exit the loop before the terminating condition becomes false then loop control statements are used.
Loop Control Statements
Loop control statements alter execution sequence of programming instructions within the loop. C language has following loop control statements:
- break statements
- continue statements
- goto statements
break statement:
This statement terminates the loop and transfers program control to the statement immediately following the loop.
continue statement:
This statement skips execution of programming instructions and bounds the program counter to check terminating conditions and begins the execution of the programming instructions if the terminating condition is true.
goto statement:
The goto statement transfers the program control to a particular location within the program. The location where the program control is transferred is given a name or a label. Further execution of the program instructions is carried-out following the label.
Sometimes it is required to keep on executing the loop for this terminating condition should never become false. This is often the requirement of game programming. Loops having terminating conditions always true are called infinite loops.
Infinite loop
If the terminating condition of the loop always remains true then that loop becomes an infinite loop. When the terminating condition of the loop is absent then the compiler of the C language considers the terminating condition to be true and the loop execution is carried-out infinitely. Infinite loops are the most used programming constructs of game programming.
To terminate an infinite loop Ctrl+C keys are used.
Desired objective is to print a series ab abc abcd abcde using C language.
As we can see that the required output is ab abc abcd abcde is a series. First we want to print ab than we append ab with c and the output becomes abc than abc is appended with d and the output becomes abcd and abcd is appended with e to get abcde.
To achieve the desired output, a nested loop is used. Loop placed inside another loop is called an inner loop and within which the loop is placed is called the outer loop.
Terminating condition of the inner loop must be in association with the outer loop. This means that the terminating condition of the inner loop should be governed by the outer loop. Number of times the inner loop is executed is governed by the outer loop.
Solution to the Problem
Following is the program in C language to achieve the desired objective.
a ab abc abcd abcde program in C
#include<stdio.h> int main() { int i,j; for (i='A'; i<='E'; i++) { for (j='A'; j<=i; j++) { printf("%c", j); } printf(" "); } return 0; }
Output: A AB ABC ABCD ABCDE
Code Analysis In this code a nested loop is used. The loop with initialized variable ‘j’ is called inner loop and the loop with initialized variable ‘i’ is called outer loop. The terminating condition of the inner for loop is controlled by the outer loop. The inner loop is executed to achieve the desired objective. Since we want to display ‘a’ first then the inner loop should be executed till ‘a’ and should not be moved to ‘b’. To do this, the loop variable of the outer loop is used to set the terminating condition of the inner loop. Loop variable of the outer loop is initialized with ‘a’ and ends with ‘e’ (this is the terminating condition of the outer loop). Value of the loop variable of the outer loop is used to set the terminating condition of the inner loop. Thus the inner loop must execute till its loop variable value reaches the loop variable value of the outer loop. This is done by executing the following code:
for (i='A'; i<='E'; i++) { for (j='A'; j<=i; j++) { printf("%c", j); } printf(" "); }
In this code, ‘j’ is the loop variable of the inner loop and ‘i’ is the loop variable of the outer loop. ‘i’ is set to ‘A’ and ends with ‘E’, this forms the terminating condition. The inner loop is initialized with ‘A’ and ends with the value of the loop variable of the outer loop. Value of the inner loop variable is displayed to the user.
Conclusion
The objective was to display the series a ab abc abcd abcde. To develop a program in C language to achieve the desired objective the learner must have the understanding of the execution cycle of the nested loop. To develop the understanding of the loop the author has given a basic explanation of the loops in the Problem analysis section.
Using the explanation in the problem analysis section the technique to develop a C language program to achieve desired output is given in the problem description section.
C language program to achieve desired output is given in the section Solution to the Problem. Program to display a series in C language is simple but the understanding of the execution cycle of nested loop is critical as it is used in higher order programming such as – Game Programming.
Leave a Reply