Introduction Data type Variable-Constant Keyword-Identifier Operations Input&Output If-else Switch loops String Array Functions Recursion-Function Mat-Function Pointers File handling Read & write Error handling Read & write error handling


While-loop

Inside the while loop condition is evaluated and if the condition is true , then the body of the loop is executed, until the condition became false.

After the control is transferred out of the loop.


   Basic syntax
   while(condition){
    //body of the statements
  }
         

Example
  #include <stdio.h>
                  
    int main() {
      int count = 1; // Initialize a counter
                                                
       while (count <= 5) { 
        // Loop as long as count is less than or equal to 5
           printf("Printing numbers : %d\n", count);
           count++; // Increment the counter
        }
        return 0;
    }        
           

OutPut
Printing numbers : 1
Printing numbers : 2
Printing numbers : 3
Printing numbers : 4
Printing numbers : 5

                    

Do-while-loop

Its executes the body of the loop statements first before the testing the condition, after execution of loop again reacheck the condition in the while statement is evaluated, if the condition is true once again the body loop is executes util the condition became false, this loop will terminated


        Basic syntax
        do{
            //body of the loop 
        }
        while(condition);
        


Example
#include <stdio.h>

 int main() {
    int number;
                        
  do {
    printf("Enter a number: ");
    scanf("%d", &number);
                        
    if (number <= 0) {
    printf("Invalid input. Please enter positive number only.\n");
    }
  } while (number <= 0);
                                            
    printf("You entered a positive number: %d\n", number);
    printf("Successfully entered number");
    return 0;
}
                    
                    

OutPut
Enter a number: -2
Invalid input. Please enter positive number only.
Enter a number: -23
Invalid input. Please enter positive number only.
Enter a number: 23
You entered a positive number: 23
Successfully entered number
For-loop

This is a another type of loop this provides a more clarity & loop control structure

repeatedly execute a block of code a specific number of times.

used when you know in advance how many times you want to execute the loop

The for loop consists of three parts: initialization, condition, and increment/decrement.


Basic syntax
    for (initialization; condition; increment/decrement) {
        // Code to be executed
    }
    

The initialization part is executed once at the beginning.

The condition is evaluated before each iteration. If the condition is true, the loop continues; otherwise, it terminates.

The code block is executed as long as the condition remains true.

After each iteration, the increment or decrement part is executed, typically used to update the loop control variable


Example
         #include <stdio.h>

            int main() {
                for (int count = 1; count <= 5; count++) {
                    printf("Count: %d\n", count);
                }
            
                return 0;
            }
        

OutPut
Printing a numbers : 1
Printing a numbers : 2
Printing a numbers : 3
Printing a numbers : 4
Printing a numbers : 5


Open kannada !




@2022, Padma Education.

We cannot warrant full correctness of all content, we try to give our best. While using Padma education website, you agree to have read and accepted our terms and Conditions , privacy policy.