Decision Making and Looping in C

 Loop in C


Introduction to Looping:

• Looping is one of the key concepts on any programming language.
• In looping, a sequence of statements are executed until some conditions for termination of the loop are satisfied. 
• A program loop therefore consists of two segments, one known as the body of the loop and the other known as the control statements.
Example: An iterative method to do this is to write the printf() statement 10 times.


Decision Making and Looping in C


  • Depending on the position of the control statements in the loop, a control structure may be classified either as an entry-controlled loop or as the exit-controlled loop.

Decision Making and Looping in C


The best example of looping is chrome browser offline game.


Decision Making and Looping in C

Types of Loops:

• The C language provides for three loop constructs for performing loop operations.

They are

1. The while statement

2. The do-while statement

3. The for statement


1. The while statement:

• The basic format of the while statement is:

   While(test condition)

   {

     body of the loop

    }

• The while is an entry–controlled loop statement.

• The test-condition is evaluated and if the condition is true, then the body of the loop is executed.

Flowchart of while loop



Decision Making and Looping in C

1. The while statement: Example


/* sum of first 100 natural number */
#include<stdio.h>
int main()
{
int i=1,sum=0;////Initialization
while(i<=100)////Condition
{
sum=sum+i;
i=i+1;            ////Increment/Decrement
}
printf("sum= %d\n", sum);
return 0;
}
ouput-
sum 5050


Example 2-

Display count of positive, negative and zeroes

2. The do statement:

• In while loop the body of the loop may not be executed at all if the condition is not satisfied at the very first attempt.
• Such situations can be handled with the help of the do statement.
• The basic format of the do statement is:

   do

   {

     body of the loop

    }

   while(test condition)


• test-condition is evaluated at the bottom of the loop, the do…..while construct provides an exit-controlled loop and therefore the body of the loop is always executed at least once.

Flowchart of do-while loop

Decision Making and Looping in C



2. The do statement: Example

/* sum of first 100 natural number */
#include<stdio.h>
int main()
{
int i=1,sum=0; ////Initialization
do
{
sum=sum+i;
i=i+1;              ////Increment/Decrement
}while(i<=100); ////Condition
printf("sum= %d\n", sum);
return 0;
}

output-
sum 5050


3. The for statement: 

• The for loop is another entry-controlled loop that provides a more concise loop control structure.

• The general form of the for loop is:

for(initialization; test-condition ; increment)

  {

    body of the loop

   }

The execution of the for statement is as follows:

  •  Initialization of the control variables is done first.
  • The value of the control variable is tested using the test-condition.
  • When the body of the loop is executed, the control is transferred back to the for statement after evaluating the last statement in the loop. Now, the control variable is either incremented or decremented as per the condition.

Flowchart of for loop

Decision Making and Looping in C

2. The for statement: Example

/* sum of first 100 natural number */
#include<stdio.h>
int main()
{
 int i,sum=0;
 for(i=1;i<=100;i++) ////Initialization,Condition and Increment/Decrement
{
sum=sum+i;
}
 printf("Sum= %d\n", sum);
 return 0;
}

output-
sum 5050

Example 2-

More examples-


Post a Comment

If you have any doubt or suggestions.please let me know.

Previous Post Next Post

Ads by google

Ads by google