Looping statemnets in C:

Looping statements are used to repeat the certain part of codes  until the condition becomes false. Its used to perform repetitive tasks.

Loop can be determines when looping count is constant and it can be execute certain times with the user’s inputs also.

There are three types of looping statements are there

  • While
  • Do-while
  • For

 

While statement:

While(condition)

{

Set of codes which needs to execute again and again until condition goes false.

}

Whenever while statement executes first it checks the condition. If the condition is true then it will execute the block of the statements until the condition goes false.

Example program:

Write a program to execute sum of n numbers.

Step to proceed.

First you need to get the n values from the user. Then you need to execute the loop with n times and get the sum of all the inputs and display the same.

Codes:

 

Do while:

Do while looping statement initially allows the code which has been  placed inside the block then it will check the condition towards the end of the block. If the condition is true then it will allow to repeat the block of the codes otherwise it will not allow the blocking code to be repeated.

syntax:

do

{

set of codes inside the blocks. must exec

}

Exercise:

Try to write the program to get the sum of the digits by using do while statements.

example : 123 as input then output will be 1+2+3 = 6. Like this you need to implement.