C Programming - Decision & Loop
           Control

 Organized By: Vinay Arora
                Assistant Professor, CSED
                Thapar University, Patiala
Decision Control

   The if statement



   The if-else statement



   The conditional operators




                               Vinay Arora
                                  CSED
Forms of if




              Vinay Arora
                 CSED
Forms of if (contd.)




                  Vinay Arora
                     CSED
Relational Operator




                 Vinay Arora
                    CSED
Demonstrating - If




                 Vinay Arora
                    CSED
Flowchart




            Vinay Arora
               CSED
Demonstrating - If




                 Vinay Arora
                    CSED
Flowchart




            Vinay Arora
               CSED
Expression in Conditional Part




                 Vinay Arora
                    CSED
Multiple statements within if




                  Vinay Arora
                     CSED
Demonstrating If - Else




                 Vinay Arora
                    CSED
Flowchart If-Else




                    Vinay Arora
                       CSED
Logical Operators with If-Else




                 Vinay Arora
                    CSED
Logical Operators with If-Else




                 Vinay Arora
                    CSED
Else-if ladder




                 Vinay Arora
                    CSED
Logical Operators with If-Else




                 Vinay Arora
                    CSED
Smallest amongst 3 nos.




                 Vinay Arora
                    CSED
Smallest amongst 3 nos.




                 Vinay Arora
                    CSED
Program using Logical OR, elseif




                 Vinay Arora
                    CSED
Calculate Salary as per following table




                    Vinay Arora
                       CSED
&& - Logical AND
  C allows usage of three logical operators, namely, &&, || and !



  These are to be read as ‘AND’ ‘OR’ and ‘NOT’ respectively.



  Don’t use the single symbol | and &. These single symbols also have a
  meaning.



  The first two operators, && and ||, allow two or more conditions to be
  combined in an if statement.

                                 Vinay Arora
                                    CSED
&& - Logical AND (C Program)




               Vinay Arora
                  CSED
Vinay Arora
   CSED
Vinay Arora
   CSED
| | - Logical OR (C Program)




                 Vinay Arora
                    CSED
Vinay Arora
   CSED
Vinay Arora
   CSED
! - Logical NOT
  This operator reverses the result of the expression it operates on.

  For example, if the expression evaluates to a non-zero value, then
  applying ! operator to it results into a 0.

  Vice versa, if the expression evaluates to zero then on applying !
  operator to it makes it 1, a non-zero value.




                                  Vinay Arora
                                     CSED
! - Logical NOT (C Program)




               Vinay Arora
                  CSED
Vinay Arora
   CSED
Vinay Arora
   CSED
&&, ||, ! Operator




                 Vinay Arora
                    CSED
? : - Conditional Operator
  The conditional operators ? and : are sometimes called Ternary
  Operators since they take three arguments.

  They form a kind of foreshortened if-then-else.

  Their general form is, expression 1 ? expression 2 : expression 3

  If expression 1 is true (that is, if its value is non-zero), then the value
  returned will be expression 2, otherwise the value returned will be
  expression 3.




                                   Vinay Arora
                                      CSED
Conditional Operator (C Program)




                Vinay Arora
                   CSED
Vinay Arora
   CSED
Vinay Arora
   CSED
goto Statement

   goto is used to switch the control flow.

   In a difficult programming situation it seems so easy to use a goto to
   take the control.

   In most of the scenarios use of goto is depreciated.

   goto can be replaced by if-else, switch, for.




                                   Vinay Arora
                                      CSED
goto Statement - Program




                Vinay Arora
                   CSED
Vinay Arora
   CSED
Vinay Arora
   CSED
switch Statement
   The control statement that allows us to make a decision from the number
   of choices is called a switch.




  The integer expression following the keyword switch is any C expression that
  will yield an integer value.


                                    Vinay Arora
                                       CSED
switch Statement - Flowchart




                 Vinay Arora
                    CSED
switch Statement - Program




                Vinay Arora
                   CSED
Vinay Arora
   CSED
Vinay Arora
   CSED
Loops
  Repetitive instructions is done through a Loop control instruction.

  This involves repeating some portion of the program either a
  specified number of times or until a particular condition is being
  satisfied.

  There are three methods by way of which we can repeat a part of a
  program. They are:

  (a) Using a while statement
  (b) Using a do-while statement
  (c) Using a for statement

                               Vinay Arora
                                  CSED
while Loop - Flowchart




                Vinay Arora
                   CSED
while Loop – general form




                 Vinay Arora
                    CSED
while Loop – forms of conditions




                 Vinay Arora
                    CSED
while Loop – C Program




                Vinay Arora
                   CSED
Vinay Arora
   CSED
While Loop – Program 1
        //Program to demonstrate simple while loop
        #include<stdio.h>
        #include<conio.h>

         void main()
          {
           int i=1;
           clrscr();

           while (i<=10)
           {
            printf("%dn",i);
            i=i+1;
           }
          getch();
         }

                                Vinay Arora
                                   CSED
Output




         Vinay Arora
            CSED
While Loop – Program 2
  //Program to demonstrate simple while loop with decrement operator
  #include<stdio.h>
  #include<conio.h>

   void main()
    {
     int i=5;
     clrscr();

     while (i>=1)
     {
      printf("%dn",i);
      i=i-1;
     }
    getch();
   }

                                Vinay Arora
                                   CSED
Output




         Vinay Arora
            CSED
While Loop – Program 3
  /* Program to demonstrate simple while loop taking incremental value
     as float */
  #include<stdio.h>
  #include<conio.h>

   void main()
    {
     float i=10.0;
     clrscr();

     while (i<=10.5)
     {
      printf("nCivil Engineering at Thapar");
      i=i+.1;
     }
    getch();
   }
                                  Vinay Arora
                                     CSED
Output




         Vinay Arora
            CSED
While Loop – Program 4
  //Demonstrating simple while loop with integer value out of range
  #include<stdio.h>
  #include<conio.h>

  void main()
   {
    int i=1;
    clrscr();

     while (i<=32767)
     {
      printf("%dn",i);
      i=i+1;
     }
    getch();
   }

                                  Vinay Arora
                                     CSED
Output – Infinite Loop




                 Vinay Arora
                    CSED
While Loop – Program 5
     //Program to demonstrate simple while loop
     #include<stdio.h>
     #include<conio.h>

      void main()
       {
        int i=1;
        clrscr();

        while (i<=10);
        {
         printf("%dn",i);
         i=i+1;
        }
       getch();
      }

                             Vinay Arora
                                CSED
Output




         Vinay Arora
            CSED
While Loop – Program 6
  //Program to demonstrate post increment operator in while loop
  #include<stdio.h>
  #include<conio.h>

  void main()
   {
    int i=1;
    clrscr();

     while (i<=10)
     {
      printf("%dn",i);
      i=i++;
     }
    getch();
   }

                                  Vinay Arora
                                     CSED
Output




         Vinay Arora
            CSED
While Loop – Program 7
 //Program to demonstrate compound assignment operator within while loop
 #include<stdio.h>
 #include<conio.h>

 void main()
  {
   int i=1;
   clrscr();

    while (i<=5)
    {
     printf("%dn",i);
     i+=1;
    }
   getch();
  }

                                  Vinay Arora
                                     CSED
Output




         Vinay Arora
            CSED
While Loop – Program 8
  //Program to demonstrate post increment operator with while loop
  #include<stdio.h>
  #include<conio.h>

  void main()
   {
    int i=0;
    clrscr();

    while (i++ < 5)
    {
     printf("%dn",i);

     }
    getch();
   }

                                  Vinay Arora
                                     CSED
Output




         Vinay Arora
            CSED
While Loop – Program 9
 //Program to find out even numbers between 1-10
 #include<stdio.h>
 #include<conio.h>

 void main()
  {
   int i=1;
   clrscr();

    while (i<=10)
    {
     if (i%2==0)
           printf("%dn",i);
     i=i+1;
    }
   getch();
  }
                                 Vinay Arora
                                    CSED
Output




         Vinay Arora
            CSED
Do-while Loop




    While



                              Do while


                Vinay Arora
                   CSED
Do-while Loop – Program 10
       //Program to demonstrate DO-WHILE loop
       #include<stdio.h>
       #include<conio.h>

       void main()
        {
         int i=1;
         clrscr();

        /*
         while(i<1)
          {
           printf("hello i am at Thapar");
          }
        */

        do
        {
         printf("hello i am at Thapar");
        }
        while(i<1);

        getch();
        }


                                             Vinay Arora
                                                CSED
Output




         Vinay Arora
            CSED
Program Transformation




               Unary Post Increment
                    Operator



                 Vinay Arora
                    CSED
Program Transformation




                   Compound
              Assignment Operator



                 Vinay Arora
                    CSED
For Loop
  The for allows us to specify three things about a loop in a single line:




                                 Vinay Arora
                                    CSED
For Loop (Program-1)
        //Program to demonstrate simple For loop
        #include<stdio.h>
        #include<conio.h>

        void main()
         {
          int i;
          clrscr();

         for (i=1; i<=10; i=i+1)
          printf("%dn",i);

             getch();
         }



                             Vinay Arora
                                CSED
For Loop (Program-1 Output)




                  Vinay Arora
                     CSED
For Loop (Program-2)
         //Program to demonstrate simple For loop
         #include<stdio.h>
         #include<conio.h>

         void main()
          {
           int i;
           clrscr();

           for (i=1; i<=10;)
            {
             printf("%dn",i);
             i=i+1;
            }
            getch();
          }

                             Vinay Arora
                                CSED
For Loop (Program-2 Output)




                  Vinay Arora
                     CSED
For Loop (Program-3)
         //Program to demonstrate simple For loop
         //Print numbers from 1 to 5
         #include<stdio.h>
         #include<conio.h>

         void main()
          {
           int i=1;
           clrscr();

           for (;i<=5;i=i+1)
            {
             printf("%dn",i);
            }
            getch();
          }

                             Vinay Arora
                                CSED
For Loop (Program-3 Output)




                  Vinay Arora
                     CSED
For Loop (Program-4)
         //Program to demonstrate simple For loop
         #include<stdio.h>
         #include<conio.h>

         void main()
          {
           int i=1;
           clrscr();

           for (;i<=5;)
            {
             printf("%dn",i);
             i=i+1;
            }
            getch();
          }

                             Vinay Arora
                                CSED
For Loop (Program-4 Output)




                  Vinay Arora
                     CSED
For Loop (Program-5)
         //Program to demonstrate simple For loop
         #include<stdio.h>
         #include<conio.h>

         void main()
          {
           int i;
           clrscr();

           for (i=0;i++<5;)
            {
             printf("%dn",i);
            }
            getch();
          }

                             Vinay Arora
                                CSED
For Loop (Program-5 Output)




                  Vinay Arora
                     CSED
For Loop (Program-6)
        //Program to demonstrate simple For loop
        #include<stdio.h>
        #include<conio.h>

         void main()
          {
           int i;
           clrscr();

          for (i=0;++i<5;)
           {
            printf("%dn",i);
           }
           getch();
         }


                                Vinay Arora
                                   CSED
For Loop (Program-6 Output)




                  Vinay Arora
                     CSED
For Loop (Program-7)
        //Program to demonstrate NESTED For loop
        #include<stdio.h>
        #include<conio.h>

        void main()
         {
          int i,j;
          clrscr();

          for (i=1;i<=5;i=i+1)
           {
            printf("n");
            for (j=1;j<=i;j=j+1)
             {
              printf(" *");
             }
           }
           getch();
         }
                                   Vinay Arora
                                      CSED
For Loop (Program-7 Output)




                  Vinay Arora
                     CSED
Thnx…



  Vinay Arora
     CSED

C Prog. - Decision & Loop Controls

  • 1.
    C Programming -Decision & Loop Control Organized By: Vinay Arora Assistant Professor, CSED Thapar University, Patiala
  • 2.
    Decision Control The if statement The if-else statement The conditional operators Vinay Arora CSED
  • 3.
    Forms of if Vinay Arora CSED
  • 4.
    Forms of if(contd.) Vinay Arora CSED
  • 5.
    Relational Operator Vinay Arora CSED
  • 6.
    Demonstrating - If Vinay Arora CSED
  • 7.
    Flowchart Vinay Arora CSED
  • 8.
    Demonstrating - If Vinay Arora CSED
  • 9.
    Flowchart Vinay Arora CSED
  • 10.
    Expression in ConditionalPart Vinay Arora CSED
  • 11.
    Multiple statements withinif Vinay Arora CSED
  • 12.
    Demonstrating If -Else Vinay Arora CSED
  • 13.
    Flowchart If-Else Vinay Arora CSED
  • 14.
    Logical Operators withIf-Else Vinay Arora CSED
  • 15.
    Logical Operators withIf-Else Vinay Arora CSED
  • 16.
    Else-if ladder Vinay Arora CSED
  • 17.
    Logical Operators withIf-Else Vinay Arora CSED
  • 18.
    Smallest amongst 3nos. Vinay Arora CSED
  • 19.
    Smallest amongst 3nos. Vinay Arora CSED
  • 20.
    Program using LogicalOR, elseif Vinay Arora CSED
  • 21.
    Calculate Salary asper following table Vinay Arora CSED
  • 22.
    && - LogicalAND C allows usage of three logical operators, namely, &&, || and ! These are to be read as ‘AND’ ‘OR’ and ‘NOT’ respectively. Don’t use the single symbol | and &. These single symbols also have a meaning. The first two operators, && and ||, allow two or more conditions to be combined in an if statement. Vinay Arora CSED
  • 23.
    && - LogicalAND (C Program) Vinay Arora CSED
  • 24.
  • 25.
  • 26.
    | | -Logical OR (C Program) Vinay Arora CSED
  • 27.
  • 28.
  • 29.
    ! - LogicalNOT This operator reverses the result of the expression it operates on. For example, if the expression evaluates to a non-zero value, then applying ! operator to it results into a 0. Vice versa, if the expression evaluates to zero then on applying ! operator to it makes it 1, a non-zero value. Vinay Arora CSED
  • 30.
    ! - LogicalNOT (C Program) Vinay Arora CSED
  • 31.
  • 32.
  • 33.
    &&, ||, !Operator Vinay Arora CSED
  • 34.
    ? : -Conditional Operator The conditional operators ? and : are sometimes called Ternary Operators since they take three arguments. They form a kind of foreshortened if-then-else. Their general form is, expression 1 ? expression 2 : expression 3 If expression 1 is true (that is, if its value is non-zero), then the value returned will be expression 2, otherwise the value returned will be expression 3. Vinay Arora CSED
  • 35.
    Conditional Operator (CProgram) Vinay Arora CSED
  • 36.
  • 37.
  • 38.
    goto Statement goto is used to switch the control flow. In a difficult programming situation it seems so easy to use a goto to take the control. In most of the scenarios use of goto is depreciated. goto can be replaced by if-else, switch, for. Vinay Arora CSED
  • 39.
    goto Statement -Program Vinay Arora CSED
  • 40.
  • 41.
  • 42.
    switch Statement The control statement that allows us to make a decision from the number of choices is called a switch. The integer expression following the keyword switch is any C expression that will yield an integer value. Vinay Arora CSED
  • 43.
    switch Statement -Flowchart Vinay Arora CSED
  • 44.
    switch Statement -Program Vinay Arora CSED
  • 45.
  • 46.
  • 47.
    Loops Repetitiveinstructions is done through a Loop control instruction. This involves repeating some portion of the program either a specified number of times or until a particular condition is being satisfied. There are three methods by way of which we can repeat a part of a program. They are: (a) Using a while statement (b) Using a do-while statement (c) Using a for statement Vinay Arora CSED
  • 48.
    while Loop -Flowchart Vinay Arora CSED
  • 49.
    while Loop –general form Vinay Arora CSED
  • 50.
    while Loop –forms of conditions Vinay Arora CSED
  • 51.
    while Loop –C Program Vinay Arora CSED
  • 52.
  • 53.
    While Loop –Program 1 //Program to demonstrate simple while loop #include<stdio.h> #include<conio.h> void main() { int i=1; clrscr(); while (i<=10) { printf("%dn",i); i=i+1; } getch(); } Vinay Arora CSED
  • 54.
    Output Vinay Arora CSED
  • 55.
    While Loop –Program 2 //Program to demonstrate simple while loop with decrement operator #include<stdio.h> #include<conio.h> void main() { int i=5; clrscr(); while (i>=1) { printf("%dn",i); i=i-1; } getch(); } Vinay Arora CSED
  • 56.
    Output Vinay Arora CSED
  • 57.
    While Loop –Program 3 /* Program to demonstrate simple while loop taking incremental value as float */ #include<stdio.h> #include<conio.h> void main() { float i=10.0; clrscr(); while (i<=10.5) { printf("nCivil Engineering at Thapar"); i=i+.1; } getch(); } Vinay Arora CSED
  • 58.
    Output Vinay Arora CSED
  • 59.
    While Loop –Program 4 //Demonstrating simple while loop with integer value out of range #include<stdio.h> #include<conio.h> void main() { int i=1; clrscr(); while (i<=32767) { printf("%dn",i); i=i+1; } getch(); } Vinay Arora CSED
  • 60.
    Output – InfiniteLoop Vinay Arora CSED
  • 61.
    While Loop –Program 5 //Program to demonstrate simple while loop #include<stdio.h> #include<conio.h> void main() { int i=1; clrscr(); while (i<=10); { printf("%dn",i); i=i+1; } getch(); } Vinay Arora CSED
  • 62.
    Output Vinay Arora CSED
  • 63.
    While Loop –Program 6 //Program to demonstrate post increment operator in while loop #include<stdio.h> #include<conio.h> void main() { int i=1; clrscr(); while (i<=10) { printf("%dn",i); i=i++; } getch(); } Vinay Arora CSED
  • 64.
    Output Vinay Arora CSED
  • 65.
    While Loop –Program 7 //Program to demonstrate compound assignment operator within while loop #include<stdio.h> #include<conio.h> void main() { int i=1; clrscr(); while (i<=5) { printf("%dn",i); i+=1; } getch(); } Vinay Arora CSED
  • 66.
    Output Vinay Arora CSED
  • 67.
    While Loop –Program 8 //Program to demonstrate post increment operator with while loop #include<stdio.h> #include<conio.h> void main() { int i=0; clrscr(); while (i++ < 5) { printf("%dn",i); } getch(); } Vinay Arora CSED
  • 68.
    Output Vinay Arora CSED
  • 69.
    While Loop –Program 9 //Program to find out even numbers between 1-10 #include<stdio.h> #include<conio.h> void main() { int i=1; clrscr(); while (i<=10) { if (i%2==0) printf("%dn",i); i=i+1; } getch(); } Vinay Arora CSED
  • 70.
    Output Vinay Arora CSED
  • 71.
    Do-while Loop While Do while Vinay Arora CSED
  • 72.
    Do-while Loop –Program 10 //Program to demonstrate DO-WHILE loop #include<stdio.h> #include<conio.h> void main() { int i=1; clrscr(); /* while(i<1) { printf("hello i am at Thapar"); } */ do { printf("hello i am at Thapar"); } while(i<1); getch(); } Vinay Arora CSED
  • 73.
    Output Vinay Arora CSED
  • 74.
    Program Transformation Unary Post Increment Operator Vinay Arora CSED
  • 75.
    Program Transformation Compound Assignment Operator Vinay Arora CSED
  • 76.
    For Loop The for allows us to specify three things about a loop in a single line: Vinay Arora CSED
  • 77.
    For Loop (Program-1) //Program to demonstrate simple For loop #include<stdio.h> #include<conio.h> void main() { int i; clrscr(); for (i=1; i<=10; i=i+1) printf("%dn",i); getch(); } Vinay Arora CSED
  • 78.
    For Loop (Program-1Output) Vinay Arora CSED
  • 79.
    For Loop (Program-2) //Program to demonstrate simple For loop #include<stdio.h> #include<conio.h> void main() { int i; clrscr(); for (i=1; i<=10;) { printf("%dn",i); i=i+1; } getch(); } Vinay Arora CSED
  • 80.
    For Loop (Program-2Output) Vinay Arora CSED
  • 81.
    For Loop (Program-3) //Program to demonstrate simple For loop //Print numbers from 1 to 5 #include<stdio.h> #include<conio.h> void main() { int i=1; clrscr(); for (;i<=5;i=i+1) { printf("%dn",i); } getch(); } Vinay Arora CSED
  • 82.
    For Loop (Program-3Output) Vinay Arora CSED
  • 83.
    For Loop (Program-4) //Program to demonstrate simple For loop #include<stdio.h> #include<conio.h> void main() { int i=1; clrscr(); for (;i<=5;) { printf("%dn",i); i=i+1; } getch(); } Vinay Arora CSED
  • 84.
    For Loop (Program-4Output) Vinay Arora CSED
  • 85.
    For Loop (Program-5) //Program to demonstrate simple For loop #include<stdio.h> #include<conio.h> void main() { int i; clrscr(); for (i=0;i++<5;) { printf("%dn",i); } getch(); } Vinay Arora CSED
  • 86.
    For Loop (Program-5Output) Vinay Arora CSED
  • 87.
    For Loop (Program-6) //Program to demonstrate simple For loop #include<stdio.h> #include<conio.h> void main() { int i; clrscr(); for (i=0;++i<5;) { printf("%dn",i); } getch(); } Vinay Arora CSED
  • 88.
    For Loop (Program-6Output) Vinay Arora CSED
  • 89.
    For Loop (Program-7) //Program to demonstrate NESTED For loop #include<stdio.h> #include<conio.h> void main() { int i,j; clrscr(); for (i=1;i<=5;i=i+1) { printf("n"); for (j=1;j<=i;j=j+1) { printf(" *"); } } getch(); } Vinay Arora CSED
  • 90.
    For Loop (Program-7Output) Vinay Arora CSED
  • 91.
    Thnx… VinayArora CSED