SlideShare a Scribd company logo
1 of 10
GUIDEDBY: SUBMITTEDBY:
VIKAS SOMANI JYOTI JAIN
•In selection structure, the program is executed
based upon the given condition.
•Only instructions that satisfy the given
condition are executed.
• There are 4 types of selection structure:
• Simple if
• if…else
•nested if..else
•Else if ladder
 A nested if…else statement is an if…else statement with another
if…else statements inside it.
 Example :
if (score >= 90)
printf(“An”);
else if (score >= 80)
printf(“Bn”);
else if (score >= 70)
printf(“Cn”);
else if (score >= 60)
printf(“Dn”)
else
printf(“Fn”);
 The else if statement means that if the above condition is not satisfied,
then try checking this condition.If any one of the condition is already
satisfied, then ignore the rest of the available conditions
if (test expression)
statements to be executed if
test expression is true;
else
if(test expression 1)
statements to be executed
if test expressions 1 is true;
else
if (test expression 2)
.
.
.
else
statements to be
executed if all test
expressions are false;
Write a C program to relate two integers entered by user using = or > or < sign.
#include <stdio.h>
int main(){
int numb1, numb2;
printf("Enter two integers to check".n);
scanf("%d %d",&numb1,&numb2);
if(numb1==numb2) //checking whether two integers are equal.
printf("Result: %d=%d",numb1,numb2);
else
if(numb1>numb2) //checking whether numb1 is greater than numb2.
printf("Result: %d>%d",numb1,numb2);
else
printf("Result: %d>%d",numb2,numb1);
return 0;
}
Output
Enter 2 integers to check
5
3
Result: 5>3
 If the test expression is true, it will execute the
code before else part but, if it is false, the
control of the program jumps to the else part
and check test expression 1 and the process
continues. If all the test expression are false
then, the last statement is executed.
 The ANSI standard specifies that 15 levels of
nesting may be continued.
 Else if ladder means, an simple if statement occurs with in
the else part of simple if else statement.
 if statement evaluates the condition inside the parenthesis
and if it is true, then it executes one line followed by if
statement or the sequence of steps bound of { }.
 This construct is known as the else if ladder. The conditions
are evaluated from the top of the ladder to downwards. As
soon as a true condition is found, the statement associated
with it is executed and the control is transferred to the
statement-x (skipping the rest of the ladder). When all the n
conditions become false, then the final else containing the
default statement will be executed
if ( condition 1)
{
statement - 1;
}
else if (condition 2)
{
statement - 2;
}
else if ( condition n)
{
statement - n;
}
else
{
default statement;
}
statement-x;
#include <stdio.h>
void main()
{
int choice;
printf("[1] Addn");
printf("[2] Editn");
printf("[3] Deleten");
printf("[4] Viewn");
printf("[5] Exitn");
printf("Enter your choice: ");
scanf("%d", &choice);
if(choice == 1)
printf("Add option selected");
else if(choice == 2)
printf("Edit option selected");
else if(choice == 3)
printf("Delete option selected");
else if(choice == 4)
printf("View option selected");
else if(choice == 5)
printf("Exit option selected");
else
printf("Invalid option selected");
}
[1] Add
[2] Edit
[3] Delete
[4] View
[5] Exit
Enter your choice: 4
Selection structure

More Related Content

What's hot

Cse lecture-6-c control statement
Cse lecture-6-c control statementCse lecture-6-c control statement
Cse lecture-6-c control statementFarshidKhan
 
Mesics lecture 6 control statement = if -else if__else
Mesics lecture 6   control statement = if -else if__elseMesics lecture 6   control statement = if -else if__else
Mesics lecture 6 control statement = if -else if__elseeShikshak
 
Conditional statement
Conditional statementConditional statement
Conditional statementMaxie Santos
 
Java conditional statements
Java conditional statementsJava conditional statements
Java conditional statementsKuppusamy P
 
C language control statements
C language  control statementsC language  control statements
C language control statementssuman Aggarwal
 
Module 2- Control Structures
Module 2- Control StructuresModule 2- Control Structures
Module 2- Control Structuresnikshaikh786
 
The Three Basic Selection Structures in C++ Programming Concepts
The Three Basic Selection Structures in C++ Programming ConceptsThe Three Basic Selection Structures in C++ Programming Concepts
The Three Basic Selection Structures in C++ Programming ConceptsTech
 
Diploma ii cfpc u-3 handling input output and control statements
Diploma ii  cfpc u-3 handling input output and control statementsDiploma ii  cfpc u-3 handling input output and control statements
Diploma ii cfpc u-3 handling input output and control statementsRai University
 
Mca i pic u-3 handling input output and control statements
Mca i pic u-3 handling input output and control statementsMca i pic u-3 handling input output and control statements
Mca i pic u-3 handling input output and control statementsRai University
 
Control structure C++
Control structure C++Control structure C++
Control structure C++Anil Kumar
 

What's hot (20)

Control statement in c
Control statement in cControl statement in c
Control statement in c
 
Decision making and looping
Decision making and loopingDecision making and looping
Decision making and looping
 
Cse lecture-6-c control statement
Cse lecture-6-c control statementCse lecture-6-c control statement
Cse lecture-6-c control statement
 
Mesics lecture 6 control statement = if -else if__else
Mesics lecture 6   control statement = if -else if__elseMesics lecture 6   control statement = if -else if__else
Mesics lecture 6 control statement = if -else if__else
 
Java control flow statements
Java control flow statementsJava control flow statements
Java control flow statements
 
Conditional statement
Conditional statementConditional statement
Conditional statement
 
Chap 5(decision making-branching)
Chap 5(decision making-branching)Chap 5(decision making-branching)
Chap 5(decision making-branching)
 
Control statement-Selective
Control statement-SelectiveControl statement-Selective
Control statement-Selective
 
C# conditional branching statement
C# conditional branching statementC# conditional branching statement
C# conditional branching statement
 
Java conditional statements
Java conditional statementsJava conditional statements
Java conditional statements
 
C language control statements
C language  control statementsC language  control statements
C language control statements
 
Module 2- Control Structures
Module 2- Control StructuresModule 2- Control Structures
Module 2- Control Structures
 
Branching in C
Branching in CBranching in C
Branching in C
 
M C6java6
M C6java6M C6java6
M C6java6
 
LOOPS AND DECISIONS
LOOPS AND DECISIONSLOOPS AND DECISIONS
LOOPS AND DECISIONS
 
The Three Basic Selection Structures in C++ Programming Concepts
The Three Basic Selection Structures in C++ Programming ConceptsThe Three Basic Selection Structures in C++ Programming Concepts
The Three Basic Selection Structures in C++ Programming Concepts
 
M C6java5
M C6java5M C6java5
M C6java5
 
Diploma ii cfpc u-3 handling input output and control statements
Diploma ii  cfpc u-3 handling input output and control statementsDiploma ii  cfpc u-3 handling input output and control statements
Diploma ii cfpc u-3 handling input output and control statements
 
Mca i pic u-3 handling input output and control statements
Mca i pic u-3 handling input output and control statementsMca i pic u-3 handling input output and control statements
Mca i pic u-3 handling input output and control statements
 
Control structure C++
Control structure C++Control structure C++
Control structure C++
 

Similar to Selection structure

If statements in C
If statements in CIf statements in C
If statements in CMegha Sharma
 
If statements in c programming
If statements in c programmingIf statements in c programming
If statements in c programmingArchana Gopinath
 
Selection Statements in C Programming
Selection Statements in C ProgrammingSelection Statements in C Programming
Selection Statements in C ProgrammingKamal Acharya
 
Programming in java - Concepts- Operators- Control statements-Expressions
Programming in java - Concepts- Operators- Control statements-ExpressionsProgramming in java - Concepts- Operators- Control statements-Expressions
Programming in java - Concepts- Operators- Control statements-ExpressionsLovelitJose
 
Decision Making Statement in C ppt
Decision Making Statement in C pptDecision Making Statement in C ppt
Decision Making Statement in C pptMANJUTRIPATHI7
 
Control statements in java
Control statements in javaControl statements in java
Control statements in javaManojkumar C
 
programming c language.
programming c language. programming c language.
programming c language. Abdul Rehman
 
Programming (if else)
Programming (if else)Programming (if else)
Programming (if else)Papon Sarker
 
Selection Control Structures
Selection Control StructuresSelection Control Structures
Selection Control StructuresPRN USM
 
Chapter 4(1)
Chapter 4(1)Chapter 4(1)
Chapter 4(1)TejaswiB4
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionalish sha
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionalish sha
 

Similar to Selection structure (20)

If statements in C
If statements in CIf statements in C
If statements in C
 
If statements in c programming
If statements in c programmingIf statements in c programming
If statements in c programming
 
Selection Statements in C Programming
Selection Statements in C ProgrammingSelection Statements in C Programming
Selection Statements in C Programming
 
Programming in java - Concepts- Operators- Control statements-Expressions
Programming in java - Concepts- Operators- Control statements-ExpressionsProgramming in java - Concepts- Operators- Control statements-Expressions
Programming in java - Concepts- Operators- Control statements-Expressions
 
Decision Making.pptx
Decision Making.pptxDecision Making.pptx
Decision Making.pptx
 
Control Statement programming
Control Statement programmingControl Statement programming
Control Statement programming
 
Decision Making Statement in C ppt
Decision Making Statement in C pptDecision Making Statement in C ppt
Decision Making Statement in C ppt
 
Control statements in java
Control statements in javaControl statements in java
Control statements in java
 
programming c language.
programming c language. programming c language.
programming c language.
 
Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branching
 
SPL 7 | Conditional Statements in C
SPL 7 | Conditional Statements in CSPL 7 | Conditional Statements in C
SPL 7 | Conditional Statements in C
 
Lecture 26.07.2014
Lecture 26.07.2014Lecture 26.07.2014
Lecture 26.07.2014
 
Using decision statements
Using decision statementsUsing decision statements
Using decision statements
 
Programming (if else)
Programming (if else)Programming (if else)
Programming (if else)
 
Selection Control Structures
Selection Control StructuresSelection Control Structures
Selection Control Structures
 
Chapter 4(1)
Chapter 4(1)Chapter 4(1)
Chapter 4(1)
 
CONTROL STMTS.pptx
CONTROL STMTS.pptxCONTROL STMTS.pptx
CONTROL STMTS.pptx
 
Decision control
Decision controlDecision control
Decision control
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
 

Selection structure

  • 2. •In selection structure, the program is executed based upon the given condition. •Only instructions that satisfy the given condition are executed. • There are 4 types of selection structure: • Simple if • if…else •nested if..else •Else if ladder
  • 3.  A nested if…else statement is an if…else statement with another if…else statements inside it.  Example : if (score >= 90) printf(“An”); else if (score >= 80) printf(“Bn”); else if (score >= 70) printf(“Cn”); else if (score >= 60) printf(“Dn”) else printf(“Fn”);  The else if statement means that if the above condition is not satisfied, then try checking this condition.If any one of the condition is already satisfied, then ignore the rest of the available conditions
  • 4. if (test expression) statements to be executed if test expression is true; else if(test expression 1) statements to be executed if test expressions 1 is true; else if (test expression 2) . . . else statements to be executed if all test expressions are false;
  • 5. Write a C program to relate two integers entered by user using = or > or < sign. #include <stdio.h> int main(){ int numb1, numb2; printf("Enter two integers to check".n); scanf("%d %d",&numb1,&numb2); if(numb1==numb2) //checking whether two integers are equal. printf("Result: %d=%d",numb1,numb2); else if(numb1>numb2) //checking whether numb1 is greater than numb2. printf("Result: %d>%d",numb1,numb2); else printf("Result: %d>%d",numb2,numb1); return 0; } Output Enter 2 integers to check 5 3 Result: 5>3
  • 6.  If the test expression is true, it will execute the code before else part but, if it is false, the control of the program jumps to the else part and check test expression 1 and the process continues. If all the test expression are false then, the last statement is executed.  The ANSI standard specifies that 15 levels of nesting may be continued.
  • 7.  Else if ladder means, an simple if statement occurs with in the else part of simple if else statement.  if statement evaluates the condition inside the parenthesis and if it is true, then it executes one line followed by if statement or the sequence of steps bound of { }.  This construct is known as the else if ladder. The conditions are evaluated from the top of the ladder to downwards. As soon as a true condition is found, the statement associated with it is executed and the control is transferred to the statement-x (skipping the rest of the ladder). When all the n conditions become false, then the final else containing the default statement will be executed
  • 8. if ( condition 1) { statement - 1; } else if (condition 2) { statement - 2; } else if ( condition n) { statement - n; } else { default statement; } statement-x;
  • 9. #include <stdio.h> void main() { int choice; printf("[1] Addn"); printf("[2] Editn"); printf("[3] Deleten"); printf("[4] Viewn"); printf("[5] Exitn"); printf("Enter your choice: "); scanf("%d", &choice); if(choice == 1) printf("Add option selected"); else if(choice == 2) printf("Edit option selected"); else if(choice == 3) printf("Delete option selected"); else if(choice == 4) printf("View option selected"); else if(choice == 5) printf("Exit option selected"); else printf("Invalid option selected"); } [1] Add [2] Edit [3] Delete [4] View [5] Exit Enter your choice: 4