SlideShare a Scribd company logo
Control Statement in C

 www.eshikshak.co.in
TYPES OF PROGRAMMING
STRUCTURE
•All modern programming languages include
support for three basic families of programming
structures:
•Sequential structures
•Decision structures
•Looping structures
WHAT IS A DECISION STRUCTURE?

 •Decision structures consist of:
 •Some type of T/F test
 •One or more executable blocks of code
 •Which block of code executes depends on the
 result of the T/F test (“the condition”).
CONTROL STATEMENT

•All the statements written in ‘C’ program executes
from top to bottom one by one
•Control statements are used to execute or transfer
the execution control from one part of the program
to another part based on a conditions, Such
statements are known as Conditional Statements
CONTROL STATEMENT

    •There are three types of control statements
used in C language
       1.if-else statement
       2.switch statement
       3.goto statement
IF / ELSE IF / ELSE                SELECTION
STRUCTURES
 1) A simple if structure is called a single-selection
 structure because it either selects or ignores a
 single action.

 2) The if / else structure is called a double-selection
 structure because it selects between two different
 actions.

 3) Nested if / else structures test for multiple cases
 by placing if / else structures inside other if / else
 structures.
RELATIONAL OPERATORS

To develop valid conditions, we need to use
relational operators.
 Operator      Meaning        Operator         Meaning


    >        Greater Than        <=      Less Than or Equal to

            Greater Than or
    >=                           ==            Equality
               Equal to

    <         Less Than          !=           Inequality
IF-ELSE STATEMENT

 •It is used to execute a set of statements or single
 statement based on the evaluation of given
 condition
 •It is basically a two way decision statement and is
 used in conjunction with an expression
TWO WAY BRANCHING
SIMPLE IF STATEMENT

        syntax :if(expression)
        {
                        statement 1;
                               statement 2;
                  .
True
                               statement N;
                                                                      }
         statement-x;
        Note : If expression is true than the set of statements will be
        executed after that statement-x will also be executed.
        Otherwise, the set of statements will be skipped and
        statements-x will be executed.
Result of expression

•Based on the             True   Non-Zero
evaluation of the
conditional expression
the result will be as
follow
                         False     Zero
EXAMPLE
void main()
{
                   int i=5;
        if(i==5)
                   {
                   printf(“You are inside the if true block”);
                             printf(“nvalue of i is equal to 5”);
                   }
                   printf(“nYou are outside the if block”);
}
output :
You are inside the if true block
value of i is equal to 5
   You are outside the if block
EXAMPLE
void main()
{
                   int i=-5;
        if(i==5)
                   {
                   printf(“You are inside the if true block”);
                             printf(“nvalue of i is equal to 5”);
                   }
                   printf(“nYou are outside the if block”);
}
output :
 You are outside the if block
IF-ELSE STATEMENT SYNTAX
       if (expression)
{
                 statement 1;
True                    statement 2;
                        .
                        .
                        statement N;
}
else
{
                 statement 1;
                        statement 2;
                        .     False
                        .
                        statement N;
}
IF-ELSE STATEMENT SYNTAX

The if selection structure is often written as:

        if ( this logical expression is true )    no semi-colon!
                   statement ;

And, the if / else selection structure is often written as:

        if ( this logical expression is true )
                   statement ;
        else
                    statement ;
A VERY SIMPLE PROGRAM:

           #include <stdio.h>
           int main ( )
           {
             int a = 1, b = 2, c ;

             if (a > b)
    {
        c = a;
    }
             else
         {
        c = b;
    }
}
A VERY SIMPLE PROGRAM:

           #include <stdio.h>
           int main ( )
           {
             int a = 1, b = 2, c ;

              if (a > b)
           c = a;
    else
               c = b;
}
IF…ELSE IF…ELSE STATEMENT

 •To perform multi-path decisions
 •It is collection of if statement with association of
 else statement
IF…ELSE IF…ELSE STATEMENT -
SYNTAX
 if(expression1)
 {
 statement 1;
 }
          else if(expression2)
          {
                    statement 2;
          }
                    else if(expression3)
                    {
                              statement 3;
                    }
                           else
                           {
                               statement 4; //Also known as default
 statement
                           }
IF…ELSE IF…ELSE STATEMENT

 •The structure is also known as else if ladder
 •The conditions are evaluated from top of the ladder
 to downwards
 •if the expression1 evaluates to true than all the
 statements in this block will executed
 •Execution pointer will jump to the statement
 immediately after the closing curly braces
 •If all the given conditions evaluates to false than all
 the statements in else block will be executed
 •Else block is also known as default statement
IF…ELSE IF…ELSE STATEMENT -
EXAMPLE
 void main()
 {
          int num;
          num=4;
          if(num>1)
          {
                   printf(“It is positive value”);
          }
                    else if(num<1)
                    {
                                printf(“It is negative value”);
                   }
                                else
                                 {
                                             printf(“It is zero value”):
                                 }
 getch();
 }
EXAMPLE

•WAP to accept marks for 5 subjects from the user,
calculate total and percentage and find the result as
per the following criteria
  Criteria                      Result
  Greater than or equal to 70   Distinction
  Between 60-70                 First Class
  Between 50-60                 Second Class
  Between 40-50                 Pass Class
  Less than 40                  Fail
EXAMPLE

Calculate the comission of a salesman considering
three regions X,Y and Z depending on the sales
amount as follow
 Area Code     Sales Amount   Comission
 X             <1000          10%
               <5000          12%
               >=5000         15%
 Y             <1500          10%
               <7000          12%
               >=7000         15%
 Z             <1200          10%
               <6500          12%
               >=6500         15%
EXAMPLE

•Big Bazzar gives festival discount on purchase of
their products in the following percentages
       i.If purchase amount < 1000 than 5% discount
       ii.If purchase amount >=1000 than but <3000 then
10% discount
       iii.If purchase amount >=3000 but <5000 then 12%
discount
       iv.If purchase amount > 5000 then 15% discount

More Related Content

PPTX
Control Statement programming
PPTX
Functions in C
PPTX
Storage classes in c language
PPTX
Storage classes in C
PPTX
PDF
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
PPTX
If statements in c programming
PDF
Unit II chapter 4 Loops in C
Control Statement programming
Functions in C
Storage classes in c language
Storage classes in C
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
If statements in c programming
Unit II chapter 4 Loops in C

What's hot (20)

PPTX
Switch statement, break statement, go to statement
PDF
Learn C# Programming - Decision Making & Loops
PPSX
C lecture 4 nested loops and jumping statements slideshare
PPT
Repetition Structure
PPTX
Function C programming
PPTX
Loops in C
PDF
Function in C
PPTX
C Tokens
PPSX
Conditional statement
PPT
Generics in java
PPTX
Functions in c++
PPTX
Decision making statements in C programming
PPTX
Tokens in C++
PPTX
python conditional statement.pptx
PPTX
Operators in java
PPTX
Pure virtual function and abstract class
PPTX
Introduction to c++
PPTX
Data types in c++
PDF
Constructors and destructors
Switch statement, break statement, go to statement
Learn C# Programming - Decision Making & Loops
C lecture 4 nested loops and jumping statements slideshare
Repetition Structure
Function C programming
Loops in C
Function in C
C Tokens
Conditional statement
Generics in java
Functions in c++
Decision making statements in C programming
Tokens in C++
python conditional statement.pptx
Operators in java
Pure virtual function and abstract class
Introduction to c++
Data types in c++
Constructors and destructors
Ad

Viewers also liked (20)

PPTX
Control statement-Selective
PDF
Control statements
PPTX
C if else
PPTX
CONDITIONAL STATEMENT IN C LANGUAGE
PDF
Html phrase tags
PDF
Lecture15 comparisonoftheloopcontrolstructures.ppt
PDF
Lecture21 categoriesof userdefinedfunctions.ppt
PPT
Mesics lecture 5 input – output in ‘c’
PPT
Mesics lecture 3 c – constants and variables
PDF
Lecture 7 relational_and_logical_operators
PDF
Lecture7relationalandlogicaloperators 110823181038-phpapp02
PPT
Mesics lecture files in 'c'
PDF
Algorithm
PPT
Mesics lecture 7 iteration and repetitive executions
PDF
Unit 1.3 types of cloud
PPT
Mesics lecture 8 arrays in 'c'
PDF
Unit 1.1 introduction to cloud computing
PDF
Algorithm chapter 11
PDF
Unit 1.2 move to cloud computing
PDF
Lecture19 unionsin c.ppt
Control statement-Selective
Control statements
C if else
CONDITIONAL STATEMENT IN C LANGUAGE
Html phrase tags
Lecture15 comparisonoftheloopcontrolstructures.ppt
Lecture21 categoriesof userdefinedfunctions.ppt
Mesics lecture 5 input – output in ‘c’
Mesics lecture 3 c – constants and variables
Lecture 7 relational_and_logical_operators
Lecture7relationalandlogicaloperators 110823181038-phpapp02
Mesics lecture files in 'c'
Algorithm
Mesics lecture 7 iteration and repetitive executions
Unit 1.3 types of cloud
Mesics lecture 8 arrays in 'c'
Unit 1.1 introduction to cloud computing
Algorithm chapter 11
Unit 1.2 move to cloud computing
Lecture19 unionsin c.ppt
Ad

Similar to Mesics lecture 6 control statement = if -else if__else (20)

DOC
Lab 5 2012/2012
PPTX
Basics of Control Statement in C Languages
PDF
C++ Chapter II
PPTX
Decision Making and Branching
PDF
control statement
PPTX
Lec-5-IF-ELSE-SWITCH Programming Fundamentals.pptx
PDF
Decision control
PPTX
6 Control Structures-1.pptxAAAAAAAAAAAAAAAAAAAAA
PDF
Unit ii chapter 2 Decision making and Branching in C
PPT
L3 control
PPT
C language UPTU Unit3 Slides
PPTX
Conditional Statements in C.pptx
PDF
Decision Making Statements, Arrays, Strings
PPTX
Unit_3A_If_Else_Switch and a if else statment example
PDF
3-Conditional-if-else-switch btech computer in c.pdf
PPTX
CONTROL FLOW in C.pptx
PDF
if else.pdf
PPTX
Structured Programming Unit-5-Control-Structure.pptx
PDF
Selection & Making Decisions in c
PDF
Unit 2=Decision Control & Looping Statements.pdf
Lab 5 2012/2012
Basics of Control Statement in C Languages
C++ Chapter II
Decision Making and Branching
control statement
Lec-5-IF-ELSE-SWITCH Programming Fundamentals.pptx
Decision control
6 Control Structures-1.pptxAAAAAAAAAAAAAAAAAAAAA
Unit ii chapter 2 Decision making and Branching in C
L3 control
C language UPTU Unit3 Slides
Conditional Statements in C.pptx
Decision Making Statements, Arrays, Strings
Unit_3A_If_Else_Switch and a if else statment example
3-Conditional-if-else-switch btech computer in c.pdf
CONTROL FLOW in C.pptx
if else.pdf
Structured Programming Unit-5-Control-Structure.pptx
Selection & Making Decisions in c
Unit 2=Decision Control & Looping Statements.pdf

More from eShikshak (17)

PDF
Modelling and evaluation
PDF
Operators in python
PDF
Datatypes in python
PDF
Introduction to python
PPT
Introduction to e commerce
PDF
Chapeter 2 introduction to cloud computing
PDF
Unit 1.4 working of cloud computing
PPT
Mesics lecture 4 c operators and experssions
PPT
Mesics lecture 5 input – output in ‘c’
PDF
Lecture20 user definedfunctions.ppt
PDF
Lecture18 structurein c.ppt
PDF
Lecture17 arrays.ppt
PDF
Lecture13 control statementswitch.ppt
PDF
Lecturer23 pointersin c.ppt
PDF
Program development cyle
PDF
Language processors
PDF
Computer programming programming_langugages
Modelling and evaluation
Operators in python
Datatypes in python
Introduction to python
Introduction to e commerce
Chapeter 2 introduction to cloud computing
Unit 1.4 working of cloud computing
Mesics lecture 4 c operators and experssions
Mesics lecture 5 input – output in ‘c’
Lecture20 user definedfunctions.ppt
Lecture18 structurein c.ppt
Lecture17 arrays.ppt
Lecture13 control statementswitch.ppt
Lecturer23 pointersin c.ppt
Program development cyle
Language processors
Computer programming programming_langugages

Recently uploaded (20)

PDF
Getting Started with Data Integration: FME Form 101
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
Tartificialntelligence_presentation.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
Big Data Technologies - Introduction.pptx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PPTX
1. Introduction to Computer Programming.pptx
PPT
Teaching material agriculture food technology
PDF
Accuracy of neural networks in brain wave diagnosis of schizophrenia
Getting Started with Data Integration: FME Form 101
Group 1 Presentation -Planning and Decision Making .pptx
Advanced methodologies resolving dimensionality complications for autism neur...
Unlocking AI with Model Context Protocol (MCP)
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Tartificialntelligence_presentation.pptx
Encapsulation_ Review paper, used for researhc scholars
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Machine learning based COVID-19 study performance prediction
Network Security Unit 5.pdf for BCA BBA.
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
“AI and Expert System Decision Support & Business Intelligence Systems”
Big Data Technologies - Introduction.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Mobile App Security Testing_ A Comprehensive Guide.pdf
Assigned Numbers - 2025 - Bluetooth® Document
1. Introduction to Computer Programming.pptx
Teaching material agriculture food technology
Accuracy of neural networks in brain wave diagnosis of schizophrenia

Mesics lecture 6 control statement = if -else if__else

  • 1. Control Statement in C www.eshikshak.co.in
  • 2. TYPES OF PROGRAMMING STRUCTURE •All modern programming languages include support for three basic families of programming structures: •Sequential structures •Decision structures •Looping structures
  • 3. WHAT IS A DECISION STRUCTURE? •Decision structures consist of: •Some type of T/F test •One or more executable blocks of code •Which block of code executes depends on the result of the T/F test (“the condition”).
  • 4. CONTROL STATEMENT •All the statements written in ‘C’ program executes from top to bottom one by one •Control statements are used to execute or transfer the execution control from one part of the program to another part based on a conditions, Such statements are known as Conditional Statements
  • 5. CONTROL STATEMENT •There are three types of control statements used in C language 1.if-else statement 2.switch statement 3.goto statement
  • 6. IF / ELSE IF / ELSE SELECTION STRUCTURES 1) A simple if structure is called a single-selection structure because it either selects or ignores a single action. 2) The if / else structure is called a double-selection structure because it selects between two different actions. 3) Nested if / else structures test for multiple cases by placing if / else structures inside other if / else structures.
  • 7. RELATIONAL OPERATORS To develop valid conditions, we need to use relational operators. Operator Meaning Operator Meaning > Greater Than <= Less Than or Equal to Greater Than or >= == Equality Equal to < Less Than != Inequality
  • 8. IF-ELSE STATEMENT •It is used to execute a set of statements or single statement based on the evaluation of given condition •It is basically a two way decision statement and is used in conjunction with an expression
  • 10. SIMPLE IF STATEMENT syntax :if(expression) { statement 1; statement 2; . True statement N; } statement-x; Note : If expression is true than the set of statements will be executed after that statement-x will also be executed. Otherwise, the set of statements will be skipped and statements-x will be executed.
  • 11. Result of expression •Based on the True Non-Zero evaluation of the conditional expression the result will be as follow False Zero
  • 12. EXAMPLE void main() { int i=5; if(i==5) { printf(“You are inside the if true block”); printf(“nvalue of i is equal to 5”); } printf(“nYou are outside the if block”); } output : You are inside the if true block value of i is equal to 5 You are outside the if block
  • 13. EXAMPLE void main() { int i=-5; if(i==5) { printf(“You are inside the if true block”); printf(“nvalue of i is equal to 5”); } printf(“nYou are outside the if block”); } output : You are outside the if block
  • 14. IF-ELSE STATEMENT SYNTAX if (expression) { statement 1; True statement 2; . . statement N; } else { statement 1; statement 2; . False . statement N; }
  • 15. IF-ELSE STATEMENT SYNTAX The if selection structure is often written as: if ( this logical expression is true ) no semi-colon! statement ; And, the if / else selection structure is often written as: if ( this logical expression is true ) statement ; else statement ;
  • 16. A VERY SIMPLE PROGRAM: #include <stdio.h> int main ( ) { int a = 1, b = 2, c ; if (a > b) { c = a; } else { c = b; } }
  • 17. A VERY SIMPLE PROGRAM: #include <stdio.h> int main ( ) { int a = 1, b = 2, c ; if (a > b) c = a; else c = b; }
  • 18. IF…ELSE IF…ELSE STATEMENT •To perform multi-path decisions •It is collection of if statement with association of else statement
  • 19. IF…ELSE IF…ELSE STATEMENT - SYNTAX if(expression1) { statement 1; } else if(expression2) { statement 2; } else if(expression3) { statement 3; } else { statement 4; //Also known as default statement }
  • 20. IF…ELSE IF…ELSE STATEMENT •The structure is also known as else if ladder •The conditions are evaluated from top of the ladder to downwards •if the expression1 evaluates to true than all the statements in this block will executed •Execution pointer will jump to the statement immediately after the closing curly braces •If all the given conditions evaluates to false than all the statements in else block will be executed •Else block is also known as default statement
  • 21. IF…ELSE IF…ELSE STATEMENT - EXAMPLE void main() { int num; num=4; if(num>1) { printf(“It is positive value”); } else if(num<1) { printf(“It is negative value”); } else { printf(“It is zero value”): } getch(); }
  • 22. EXAMPLE •WAP to accept marks for 5 subjects from the user, calculate total and percentage and find the result as per the following criteria Criteria Result Greater than or equal to 70 Distinction Between 60-70 First Class Between 50-60 Second Class Between 40-50 Pass Class Less than 40 Fail
  • 23. EXAMPLE Calculate the comission of a salesman considering three regions X,Y and Z depending on the sales amount as follow Area Code Sales Amount Comission X <1000 10% <5000 12% >=5000 15% Y <1500 10% <7000 12% >=7000 15% Z <1200 10% <6500 12% >=6500 15%
  • 24. EXAMPLE •Big Bazzar gives festival discount on purchase of their products in the following percentages i.If purchase amount < 1000 than 5% discount ii.If purchase amount >=1000 than but <3000 then 10% discount iii.If purchase amount >=3000 but <5000 then 12% discount iv.If purchase amount > 5000 then 15% discount

Editor's Notes

  • #8: Instructor: There are multiple forms the structure may take. It is important that students understand the logic they are creating both when they nest if/else structures and when using a large if/else if/else structure. Creating a flowchart prior to programming these section of code typically aids this process. Narrator: A simple if() structure, also known as a single-selection structure, either selects or ignores a single action. The if/else structure, also called a double-selection structure, allows the program to select two different actions, based on a true or false result. Nested if/else structures can test for multiple cases by placing additional if/else structures inside other if/else structures. *
  • #11: Instructor: These structures are valid only for processing single statements. The statement after the if() will ONLY be processed if the expression is TRUE. The statement after the else will ONLY be processed if the above if() statement evaluated to FALSE (it is skipped if the if() portion is true). If() can also be used by itself (no else is required after an if statement). Else CANNOT be used by itself. It must always be preceded by an if statement. Students may relate to the analogy of driving down the highway one direction and deciding which exit to take. You can only take one exit from the highway to get to the location you wish so the conditions (road signs) must be true for the correct exit. The location you wish to go to (the variable) will determine which exit you take. If you get to the end of the highway (none of the previous exits were the correct one) then you have to take the final exit (the else statement). Narrator: The syntax of the if/else selection structures are shown here. For the if selection structure, the single statement will be executed ONLY if the logical expression given as an argument is true. For the if/else selection structure, the single statement again after the if selection will be executed ONLY if the logical expression given as an argument is true. Otherwise in this case it will complete the statement given after the else portion of the structure. This can be likened to a fork in the road in which one path must be taken. *
  • #16: * Instructor: This shows a more common way of writing code so it is easier for a programmer to read later. The only difference from the last description is the statements are now on their own lines. It is very important to not insert a semicolon where shown or the program will behave unexpectedly.
  • #18: *