Looping Structure:- Loops are used at the place where the query or the code is executed till the end of the condition or say till the co...

Loop for,while and do while in c

/
0 Comments
Looping Structure:-

  • Loops are used at the place where the query or the code is executed till the end of the condition or say till the condition is not satisfied. It can be your desired situation.
  • In C and all other modern programming languages, iteration statements (also called loops) allow a set of instructions to be repeatedly executed until a certain condition is reached. This condition may be predetermined (as in the for loop) or open-ended (as in the while and do-while loops). 

Why we use Loop?

  • If we want to execute any instruction much time then we required a loop.Otherwise, we have to write that much time instructions.Suppose we want to print "hello world " 5 times.
  • Loops are used to make program short and to write program easily.We use a loop instead of rewriting instruction through this program will short make work will decease.Loops are in all languages but the syntax is different.Using loops is good habits of programmers because it decreases the work.

Our program will be like this

#include <stdio.h> 
#include<conio.h>
int main()
 {
 int a; 
a=1; //else it will take any garbage value... 
while(a <= 5) {
 a++;// it will increament value by 1
 printf("hello world \n"); 
}
 getch();
 }

Output:



There are 3 types of loops 
1. While loop 
2. Do while loop 
3. For loop 

While loop

  • While loop is simplest among all of them
  •  An entry controlled loop, as the condition is 1st checked(tested) and then the program is evaluated.
  • where the statement is either an empty statement, a single statement, or a block of statements. The condition may be any expression, and true is any nonzero value. The loop iterates while the condition is true. When the condition becomes false, program control passes to the line of code immediately following the loop.
  • syntax is:
      while(condition) {
        Program/query;
       }

  •   Like for loops, while loops check the test condition at the top of the loop, which means that the body of the loop will not execute if the condition is false to begin with. This feature may eliminate the need to perform a separate conditional test before the loop.


Program and output are above.

Do… while loop

  • This an exit controlled loop where the loop is 1st executed and then tested.Its mean condition check we loop is completed.
  •  A condition may arise where the body must be performed before execution at that time they do …    while loop is used …
  • Unlike for and while loops, which test the loop condition at the top of the loop, the do-while loop checks its condition at the bottom of the loop. This means that a do-while loop always executes at least once. The general form of the do-while loop is 

   The syntax for do while loop is :
   do {
   Program / query
   } while (condition)

  • Theory
  • Although the curly braces are not necessary when only one statement is present, they are usually used to avoid confusion (to you, not the compiler) with the while. The do-while loop iterates until the condition becomes false.
  • Perhaps the most common use of the do-while loop is in a menu selection function. When the user enters a valid response, it is returned as the value of the function. Invalid responses cause a prompt. 
  •  end of the loop. For this reason, the do - while statement is used less frequently than the while statement described in above For illustrative purposes, however, the programming examples shown below are repeated below using the do - while statement for the conditional loops


Program:
   #include<stdio.h>
    #include<conio.h>
    void main() {
    int a;
    a=1; //else it will take any garbage value... 
    do { 
    a++;
   printf("do while loop \n");
   }
  while(a <= 5);
  getch(); 
  }

Output :



For loop: -

  •    An entry controlled loop.
  •     It's widely used and pretty easy.
  • The general design of the for loop is reflected in some form or another in all procedural programming languages. However, in C, it provides unexpected flexibility and power.

  •     The general form of the for statement is-  
              for( initialization ; test-condition ; increment)
             { Program/query }
  • Theory
  • The for loop allows many variations, but its most common form works like this: The initialization is an assignment statement that is used to set the loop control variable. The condition is a relational expression that determines when the loop exits. The increment defines how the loop control variable changes each time the loop is repeated. You must separate these three major sections by semicolons. The for loop continues to execute as long as the condition is true. Once the condition becomes false, program execution resumes on the statement following the for.
  • The for statement, like the while and the do - while statements, can be used to carry out looping actions where the number of passes through the loop is not known in advance. 
Program:-
  #include<stdio.h>
  #include<conio.h>
  void main() {
 int a;
 for(a=1;a<=5;a++) // a=1 else it will take any garbage value...
 {
 printf("for loop \n");
 }
 getch();
 }


Output:

No comments:

Cybercry 2018. Powered by Blogger.

Popular Posts