Looping in C Programming Language

 Looping in C Programming Language Use: When we want to perform specific task repeatedly then we can use looping. In C Language there are three type of loop: For Loop While Loop Do while Loop Nested for Loop Each Loop statement requires three things: Initialization  Test Condition Increment/Decrement Operation 1. For Loop: For Loop is a repetition control structure which allow us to write a loop that is executed a specific number of time. Syntax:               for( initialization ; test condition; increment/decrement operation)                 {                    ------                    body of for loop;                    ------               } 2. While Loop: While Loop is use to per to p...

Decision Making Statements in C Programming Language

 Decision Making Statements in C Programming Language

  • Decision making statement are use to take decision from condition.
  • There are different decision making statement:
    1. If Statements
    2. Conditional Operator ? :
    3. Switch Case Statements
    4. Goto Statements
1). If Statements:
  • If Statement is a conditional statements.
  • It is use to take any decision.
  • To take any decision, we apply one or more conditions.
  • These conditions can be true or false.
  • Example:
            Student is pass or fail:
            if (marks>=35)
            {
                    pass
            }
            {    
                    fail
            }

  • There are four kinds of If Statements:
    1. Simple If Statement
    2. If-Else Statement
    3. Nested If Statement
    4. Else If Ladder Statement
           1. Simple If Statement:
    • In Simple If Statement, condition is checked.
    • If condition is true then true block is executed.
    • There is no else part.
    • Syntax:
                    if(condition)
                    {
                        true block;
                        ----------
                        ----------
                    }
                    Statement x;
                    ----------    
                    ----------
    • Flow Chart:
                     

    • Example:
                - Write a program to consider medical allowance = 2000 if age of person is more than 50.
                   otherwise consider MA=1000. Take age from user as a input.
                    
                    #include<stdio.h>
                    #include<conio.h>
                    void main()
                    {
                            int age, ma=1000;
                         clrscr();
                            printf("\n Enter age:");
                            scanf("%d", &age);
                            if(age>50)
                            {
                                    ma=2000;
                            }
                            printf("\n MA is %d", ma);
                            getch();
                  }

            2. If-Else Statement:
    • if-else statement is use to check condition.
    • If condition is true then True block will be executed.
    • If condition is false then False block will be executed.
    • Syntax:
                    if (condition)
                    {    
                        -------        
                        True Block;
                        --------
                    }
                    else
                    {    
                        -------
                        False Block;
                    }
    • Flow Chart:


    • Example: - Write a program to consider medical allowance = 2000 if age of person is more than 50.
                   otherwise consider MA=1000. Take age from user as a input. (using if-else statement)

                    #include<stdio.h>
                    #include<conio.h>
                    void main()
                    {
                            int age, ma;
                         clrscr();
                            printf("\n Enter age:");
                            scanf("%d", &age);
                            if(age>50)
                            {
                                    ma=2000;
                            }
                            else
                            {
                                    ma=1000;
                            }
                            printf("\n MA is %d", ma);
                            getch();
                  }

            3. Nested If Statement:
    • Nested If Statement is use to take more than one decision.
    • Nested if statement has one or more if statement in main if statement.
    • Syntax:
                    if(condition1)
                    {
                        -------
                        if(condition2)
                        {
                                true block1;
                        }
                        {
                                false block1;
                        }
                    }
                    else
                    {
                        ------
                        if(condition3)
                        {
                                true block2;
                        }
                        {
                                false block2;
                        }
                    }
    • Flow Chart:
                    

          4. Else If Ladder:
    • else if ladder is use to check multiple conditions.
    • In this, one by one condition will be check.
    • If condition is true then that block will be executed.
    • If all condition are false then else part will be executed.
    • Syntax:
                    if (condition1)
                    {
                        --- block1---;
                    }
                    elseif (condition2)
                    {
                        ---block2---;
                    }
                    elseif (condition3)
                    {
                        ---block3---;
                    }
                    elseif (condition4)
                    {
                        ---block4---;
                    }
                    --------
                    --------
                    else
                    {
                        ---false block---;
                    }
    • Flow Chart:
        

            

2). Switch Case Statement:
  • Switch case statement is decision making statement which is used to create menu driven program.
  • Syntax:
            switch(expression)
            {
                case value1: block1;
                                     break;
                case value2: block2;
                                     break;
                case value3: block3;
                                     break;
                ---------
                ---------
                ---------
                default: false block;
                             break;
        }
  • In switch statement we pass expression or value.
  • Based on expression, any one case is selected and execute that block.
  • Each case ends with break keyword.
  • If expression doesn't match with any cases then default block will be executed.
  • Flow Chart:

3). Conditional Operator: (? :) 
  • Conditional operator is used to check single condition.
  • Syntax:
         var_name = condition ? True Block : False Block;
  • In this condition is check. If statement is true then True Block is assigned to variable.
  • If Condition is false then False Block is assigned to variable name.
  • Example:
         - if age>10, consider fees=25 otherwise fees=50.
            fees = (age>10) ? 25 : 50;

4). Goto Statement:
  • Goto statement is use to pass the control of execution at specific position.
  • Syntax1:
           goto label_name;
            .....
            ...
           label_name;     
            .....
            ......
            ...
  • Syntax2:
            .....
            .........
            label_name;
            .......
            ......
            ....
            goto label_name;
            .......
            ...
  • In first syntax label name is specified after goto statement so control will pass to specific label.
  • This is known as forward jump or forward goto statement.
  • In second syntax label name is specified before goto statement which is known as backward jump or backward goto statement.
  • goto statement must require label name.

Comments

Popular posts from this blog

Data Types in C Program

Different Modules to solve the program/problem

Operators in C Program