Loop Control Statements

Loop Control Statements
Loop Control Statements are used to repeat the set of statements until the condition is satisfied.

In C, we have three kind of looping constructs
  1. For loop
  2. While loop
  3. Do-While loop
These are also know as Iteration statements.

If we first check the condition and then decide whether to execute the set of statements or not then it is called Entry-Controlled Loops and if we execute the set of statements and then check a condition to execute the statements again or not, it is called Exit-Controlled Loops.

Entry Controlled Loop:


Exit Controlled Loop:



For Loop:
  

Syntax:
for(initialization; condition; increment/decrement)
{
   body of the loop;
}

For loop is having 3 parts so lets understand them:
Initialization: It is an assignment statement that is used to set loop control variable.

Condition: It is relational expression that determines when the loop will exit.

Increment/ decrement: Here inc./dec. operator part will define how the loop control variable will change each time loop is repeated.

For loop is entry controlled loop.
Here, the for loop continues to execute as long as the condition is TRUE.

All these initialization, condition, increment/decrement and statements are usual C expressions. So the general form can be re-written as:

for(expression; expression; expression)
{
   set of expressions;
}

First, the initialization will execute, then the condition will be checked. If condition is TRUE, the set of statements or loop body will be executed. Then the increment/decrement will be executed, and again control moves to the condition statement to decide whether to enter in the loop again or exit the loop and continue with the first statement after the scope of loop.

Flowchart:
Program: For loop
#include<stdio.h>
int main()
{
int k;
for(k=1;k<=5;k++)
printf("%d\n", k);
return 0;
}

Output:



While Loop:

Syntax:
while(condition)
{
   body of the loop;
}
In while loop, only condition is checked. When the condition is satisfied then the body of the loop is executed number of times depending on condition.

Here, initialization should be done before the loop. 
Loop continues as long as condition is TRUE.
Increment/Decrement part is done within the loop only.

While loop is entry controlled loop.

Flowchart
while loop
Program: While loop
#include<stdio.h>
int main()
{
int k;
k=1;
while(k<=5)
{
printf("%d\n", k);
k++;
}
}
Output:


Do-while loop:

Syntax:
   initialize;
   do
   {
          body of the loop;
          increment/decrement;
   }while(condition);

In do while, first initialize then you have to do the operation after that you have to check the condition.
Here, condition is checked after the statement is executed and statement is executed atleast once even if the condition is FALSE.
Do-while is exit controlled loop.

Flowchart

Program: do-while loop
#include<stdio.h>
int main()
{
int k;
k=1;

do
{
printf("%d\n", k);
k++;
}while(k<=5);
}

Output:



About C Language and My First Program in C Language:

C is a programming language developed at AT & T’s Bell Laboratories of USA in 1972. It was designed and written by a man named Dennis Ritchie. In the late seventies C began to replace the more familiar languages of that time. Possibly why C seems so popular is because it is reliable, simple and easy to use. Communicating with a computer involves speaking the language the computer understands, which immediately rules out English as the language of communication with computer. However, there is a close analogy between learning English language and learning C language. The classical method of learning English is to first learn the alphabets used in the language, then learn to combine these alphabets to form words, which in turn are combined to form sentences and sentences are combined to form paragraphs. Learning C is similar and easier. Instead of straight-away learning how to write programs, we must first know what alphabets, numbers and special symbols are used in C, then how using them constants, variables and keywords are constructed, and finally how are these combined to form an instruction.

But, before digging these topics deeper, we have to take a short look on a sample program in C, how it looks like. What are the general components and how it will work to get familiar with programming in C, all these can be seen from below program which prints “Hello World!” on screen.


C Program:-

#include<stdio.h>
int main()
{
    printf("Hello World!");
    //This is a comment. Above line will just print Hello World! on the screen.
    return 0;
    /* This is an example of multiline comment,
    instead of one line you can write multi-line comment which
    is used to provide readability of the code.
    These must start end with proper constructs as shown here. */
}

Output of above C Program:-

This is a sample program to print a simple message on screen. The first line is a general command to include an already created file “stdio.h” which contains standard Input-Output functions like printf() used in the program. So that programmers need not to worry about hardware, he can more focus on writing programs. Second line is the main() function (in other words, the main gate of home) from where the CPU will start executing the program. The curly braces are used to define the set of statements which are part of a single block. printf() is a function provided by C library, which is used to print something on screen. last line is used to return back or end the program. // is used to comment a line in program. Comment does not make any sense for compiler. Compiler just ignores the comments written. These are written just for programmer reading references which makes the program much easier to understand and maintain for a long time. To write a short single line comment we use double slash //. Everything after // in that line is treated as a comment. If a particular potion needs to be comments, we generally use /* to start comment and */ to end comment. This is an multi line comment. Everything between these /* and */ is completely ignored by compiler while compiling the program.
After writing your program you must compile it before executing. We can compile the programs using compilers, there are a lot of compilers for C language today like Turbo C, gcc etc. gcc (GNU Compiler Collection) is the most popular compiler now a days. It is free and open source so you can download and use it freely.

Post a Comment

If you have any doubts, Please let me know

Previous Post Next Post