SlideShare uma empresa Scribd logo
1 de 91
Baixar para ler offline
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

Mais conteúdo relacionado

Mais procurados

Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’
eShikshak
 
1584503386 1st chap
1584503386 1st chap1584503386 1st chap
1584503386 1st chap
thuhiendtk4
 

Mais procurados (17)

Important C program of Balagurusamy Book
Important C program of Balagurusamy BookImportant C program of Balagurusamy Book
Important C program of Balagurusamy Book
 
C programing Tutorial
C programing TutorialC programing Tutorial
C programing Tutorial
 
CP Handout#6
CP Handout#6CP Handout#6
CP Handout#6
 
C fundamentals
C fundamentalsC fundamentals
C fundamentals
 
C programming Workshop
C programming WorkshopC programming Workshop
C programming Workshop
 
C Programming Unit-1
C Programming Unit-1C Programming Unit-1
C Programming Unit-1
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c language
 
Input And Output
 Input And Output Input And Output
Input And Output
 
C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
 
Chowtodoprogram solutions
Chowtodoprogram solutionsChowtodoprogram solutions
Chowtodoprogram solutions
 
C operators
C operatorsC operators
C operators
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’
 
Programming fundamental 02
Programming fundamental 02Programming fundamental 02
Programming fundamental 02
 
Decision making statements in C programming
Decision making statements in C programmingDecision making statements in C programming
Decision making statements in C programming
 
Storage classes in c language
Storage classes in c languageStorage classes in c language
Storage classes in c language
 
1584503386 1st chap
1584503386 1st chap1584503386 1st chap
1584503386 1st chap
 
CP Handout#4
CP Handout#4CP Handout#4
CP Handout#4
 

Destaque

Destaque (6)

detailed information about Pointers in c language
detailed information about Pointers in c languagedetailed information about Pointers in c language
detailed information about Pointers in c language
 
Basic for Loop in C
Basic for Loop in CBasic for Loop in C
Basic for Loop in C
 
Memory allocation in c
Memory allocation in cMemory allocation in c
Memory allocation in c
 
C program language tutorial for loop while loop do while loop
C program language tutorial for loop while loop do while loopC program language tutorial for loop while loop do while loop
C program language tutorial for loop while loop do while loop
 
Structure in C
Structure in CStructure in C
Structure in C
 
C Pointers
C PointersC Pointers
C Pointers
 

Semelhante a C Prog. - Decision & Loop Controls

Intro to Arduino Programming.pdf
Intro to Arduino Programming.pdfIntro to Arduino Programming.pdf
Intro to Arduino Programming.pdf
HimanshuDon1
 
C Prog. - ASCII Values, Break, Continue
C Prog. -  ASCII Values, Break, ContinueC Prog. -  ASCII Values, Break, Continue
C Prog. - ASCII Values, Break, Continue
vinay arora
 
OWF12/PAUG Conf Days Pro guard optimizer and obfuscator for android, eric l...
OWF12/PAUG Conf Days Pro guard   optimizer and obfuscator for android, eric l...OWF12/PAUG Conf Days Pro guard   optimizer and obfuscator for android, eric l...
OWF12/PAUG Conf Days Pro guard optimizer and obfuscator for android, eric l...
Paris Open Source Summit
 
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
eugeniadean34240
 
Rhino script 101 creativity
Rhino script 101 creativityRhino script 101 creativity
Rhino script 101 creativity
R. Sosa
 

Semelhante a C Prog. - Decision & Loop Controls (20)

C and Data structure lab manual ECE (2).pdf
C and Data structure lab manual ECE (2).pdfC and Data structure lab manual ECE (2).pdf
C and Data structure lab manual ECE (2).pdf
 
CP Handout#5
CP Handout#5CP Handout#5
CP Handout#5
 
Intro to Arduino Programming.pdf
Intro to Arduino Programming.pdfIntro to Arduino Programming.pdf
Intro to Arduino Programming.pdf
 
Basic of Programming (Introduction to Programming)
Basic of Programming (Introduction to Programming)Basic of Programming (Introduction to Programming)
Basic of Programming (Introduction to Programming)
 
Object oriented programming system with C++
Object oriented programming system with C++Object oriented programming system with C++
Object oriented programming system with C++
 
C Prog. - ASCII Values, Break, Continue
C Prog. -  ASCII Values, Break, ContinueC Prog. -  ASCII Values, Break, Continue
C Prog. - ASCII Values, Break, Continue
 
Logical Expressions in C/C++. Mistakes Made by Professionals
Logical Expressions in C/C++. Mistakes Made by ProfessionalsLogical Expressions in C/C++. Mistakes Made by Professionals
Logical Expressions in C/C++. Mistakes Made by Professionals
 
Elements of programming
Elements of programmingElements of programming
Elements of programming
 
Fundamentals of computer programming by Dr. A. Charan Kumari
Fundamentals of computer programming by Dr. A. Charan KumariFundamentals of computer programming by Dr. A. Charan Kumari
Fundamentals of computer programming by Dr. A. Charan Kumari
 
the refernce of programming C notes ppt.pptx
the refernce of programming C notes ppt.pptxthe refernce of programming C notes ppt.pptx
the refernce of programming C notes ppt.pptx
 
Programing for problem solving ( airline reservation system)
Programing for problem solving ( airline reservation system)Programing for problem solving ( airline reservation system)
Programing for problem solving ( airline reservation system)
 
OWF12/PAUG Conf Days Pro guard optimizer and obfuscator for android, eric l...
OWF12/PAUG Conf Days Pro guard   optimizer and obfuscator for android, eric l...OWF12/PAUG Conf Days Pro guard   optimizer and obfuscator for android, eric l...
OWF12/PAUG Conf Days Pro guard optimizer and obfuscator for android, eric l...
 
C lecture 3 control statements slideshare
C lecture 3 control statements slideshareC lecture 3 control statements slideshare
C lecture 3 control statements slideshare
 
Write a program to check a given number is prime or not
Write a program to check a given number is prime or notWrite a program to check a given number is prime or not
Write a program to check a given number is prime or not
 
OOPS using C++
OOPS using C++OOPS using C++
OOPS using C++
 
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
 
12 lec 12 loop
12 lec 12 loop 12 lec 12 loop
12 lec 12 loop
 
Rhino script 101 creativity
Rhino script 101 creativityRhino script 101 creativity
Rhino script 101 creativity
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial
 
Control structure of c
Control structure of cControl structure of c
Control structure of c
 

Mais de vinay arora (20)

Search engine and web crawler
Search engine and web crawlerSearch engine and web crawler
Search engine and web crawler
 
Use case diagram (airport)
Use case diagram (airport)Use case diagram (airport)
Use case diagram (airport)
 
Use case diagram
Use case diagramUse case diagram
Use case diagram
 
Lab exercise questions (AD & CD)
Lab exercise questions (AD & CD)Lab exercise questions (AD & CD)
Lab exercise questions (AD & CD)
 
SEM - UML (1st case study)
SEM - UML (1st case study)SEM - UML (1st case study)
SEM - UML (1st case study)
 
6 java - loop
6  java - loop6  java - loop
6 java - loop
 
4 java - decision
4  java - decision4  java - decision
4 java - decision
 
3 java - variable type
3  java - variable type3  java - variable type
3 java - variable type
 
2 java - operators
2  java - operators2  java - operators
2 java - operators
 
1 java - data type
1  java - data type1  java - data type
1 java - data type
 
Uta005 lecture3
Uta005 lecture3Uta005 lecture3
Uta005 lecture3
 
Uta005 lecture1
Uta005 lecture1Uta005 lecture1
Uta005 lecture1
 
Uta005 lecture2
Uta005 lecture2Uta005 lecture2
Uta005 lecture2
 
Security & Protection
Security & ProtectionSecurity & Protection
Security & Protection
 
Process Synchronization
Process SynchronizationProcess Synchronization
Process Synchronization
 
CG - Output Primitives
CG - Output PrimitivesCG - Output Primitives
CG - Output Primitives
 
CG - Display Devices
CG - Display DevicesCG - Display Devices
CG - Display Devices
 
CG - Input Output Devices
CG - Input Output DevicesCG - Input Output Devices
CG - Input Output Devices
 
CG - Introduction to Computer Graphics
CG - Introduction to Computer GraphicsCG - Introduction to Computer Graphics
CG - Introduction to Computer Graphics
 
C Prog. - Strings (Updated)
C Prog. - Strings (Updated)C Prog. - Strings (Updated)
C Prog. - Strings (Updated)
 

Último

Último (20)

Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Basic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationBasic Intentional Injuries Health Education
Basic Intentional Injuries Health Education
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 

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 Conditional Part Vinay Arora CSED
  • 11. Multiple statements within if Vinay Arora CSED
  • 12. Demonstrating If - Else Vinay Arora CSED
  • 13. Flowchart If-Else Vinay Arora CSED
  • 14. Logical Operators with If-Else Vinay Arora CSED
  • 15. Logical Operators with If-Else Vinay Arora CSED
  • 16. Else-if ladder Vinay Arora CSED
  • 17. Logical Operators with If-Else Vinay Arora CSED
  • 18. Smallest amongst 3 nos. Vinay Arora CSED
  • 19. Smallest amongst 3 nos. Vinay Arora CSED
  • 20. Program using Logical OR, elseif Vinay Arora CSED
  • 21. Calculate Salary as per following table Vinay Arora CSED
  • 22. && - 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
  • 23. && - Logical AND (C Program) Vinay Arora CSED
  • 24. Vinay Arora CSED
  • 25. Vinay Arora CSED
  • 26. | | - Logical OR (C Program) Vinay Arora CSED
  • 27. Vinay Arora CSED
  • 28. Vinay Arora CSED
  • 29. ! - 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
  • 30. ! - Logical NOT (C Program) Vinay Arora CSED
  • 31. Vinay Arora CSED
  • 32. Vinay Arora CSED
  • 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 (C Program) Vinay Arora CSED
  • 36. Vinay Arora CSED
  • 37. Vinay Arora CSED
  • 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. Vinay Arora CSED
  • 41. Vinay Arora CSED
  • 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. Vinay Arora CSED
  • 46. Vinay Arora CSED
  • 47. 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
  • 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. Vinay Arora CSED
  • 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 – Infinite Loop 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-1 Output) 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-2 Output) 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-3 Output) 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-4 Output) 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-5 Output) 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-6 Output) 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-7 Output) Vinay Arora CSED
  • 91. Thnx… Vinay Arora CSED