While Loop in C

  • What is while loop in c ?
  • A while loop in C is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The loop continues to execute the block of code as long as the condition evaluates to true. It is particularly useful when the number of iterations is not known before entering the loop and depends on a condition being met during execution.

  • Syntax :
  • while (condition) {
    // Code to be executed
    }
    Condition : An expression evaluated before each iteration. If it is true, the loop body is executed. If it is false, the loop terminates.
    loop body : The block of code that is executed as long as the condition is true.

  • Example :
  • Here's an example that uses a while loop to print numbers from 0 to 4 :

    C Code
    #include <stdio.h> main() { int i = 0; while (i < 5) { printf("i = %d\n", i); i++; } }

    Output :
    i = 0
    i = 1
    i = 2
    i = 3
    i = 4

  • Problems & solutions :
  • C Code
    # Write a program to accept a number and display the Sum of its digits #include<stdio.h> main() { int n,s=0,r; printf("Enter a no:"); scanf("%d",&n); while(n>0) { r=n%10; s=s+r; n=n/10; } printf("The sum of digits=%d",s); }

    Output :
    Enter a no:215
    The sum of digits=8

    C Code
    # Write a program to accept a number and find length of number. #include<stdio.h> main() { int n,l; printf("Enter the no:"); scanf("%d",&n); l=0; while(n>0) { n=n/10; l=l+1; } printf("length=%d",l); }

    Output :
    Enter the no:1234
    length=4

    C Code
    # Write a program to enter a number and check whether the number is Neon or not. #include<stdio.h> main() { int i,n,s=0,t,r; printf("Enter the no:"); scanf("%d",&n); t=n; n=n*n; while(n>0) { r=n%10; s=s+r; n=n/10; } if(t==s) printf("Neon no"); else printf("Not neon no"); }

    Output :
    Enter the no:9
    Neon no

  • Break and Continue Statements :
  • break : Terminates the loop immediately.

    C Code
    #include <stdio.h> main(){ int i = 0; while (i < 10) { if (i == 5) { break; // Exit the loop } printf("i = %d\n", i); i++; } }

    Output :
    i = 0
    i = 1
    i = 2
    i = 3
    i = 4

    continue : Skips the rest of the loop body and proceeds with the next iteration.

    C Code
    #include <stdio.h> main(){ int i = 0; while (i < 10) { i++; if (i % 2 == 0) { continue; // Skip even numbers } printf("i = %d\n", i); } }

    Output :
    i = 1
    i = 3
    i = 5
    i = 7
    i = 9

  • Importance of while loop :
  • The while loop in C is an essential control flow statement that provides several key benefits and plays a crucial role in various programming scenarios. Here are some of the reasons why the while loop is important in C :
    Flexibility in Loop Control :
    The while loop allows for flexible iteration control based on dynamic conditions.
    Pre-Test Loop Structure :
    As a pre-test loop, the while loop checks the condition before executing the loop body.
    Handling Variable Iterations :
    While loops are particularly useful for scenarios where the number of iterations isn't fixed or known in advance. They allow for iteration until a certain condition is met, making them suitable for a variety of use cases.
    Resource Management :
    In systems programming and resource management, while loops can be used to wait for resources to become available or to perform cleanup tasks
    Error Handling and Validation :
    While loops are often used for input validation and error handling, ensuring that the program continues to prompt the user or reprocess data until valid input is received.

  • Conclusion :
  • The while loop is a versatile and powerful construct in C programming. Its importance lies in its flexibility, ability to handle variable iterations, suitability for dynamic conditions, and role in a wide range of programming scenarios, from input validation to complex algorithm implementation. By providing a straightforward mechanism for repeated execution based on a condition, while loops are fundamental to writing efficient, readable, and maintainable code in C.