SlideShare a Scribd company logo
1 of 67
Structured Programming Language
Conditional Statements in C
Mohammad Imam Hossain,
Lecturer, CSE, UIU
• if
• if-else
• if-else if-else
• Nested if
• switch
Introduction
• Till now, The instructions were executed in the
same order in which they appeared within the
program. (Sequence)
• Branching: one of several possible actions will be
carried out, depending on the outcome of the
logical test.
• Selection: One group of statements is selected
from several available groups.
Flow Chart of if Statement
if
condition
Body of if
True
False
Statement just below if
Syntax of if
if(condition)
{
statement/s that will execute if the
condition is true
}
statement/s just below if
Syntax of if
if(condition)
{
statement/s that will execute if the
condition is true
}
statement/s just below if
Syntax of if
if(condition)
{
statement/s that will execute if the
condition is true
}
statement/s just below if
True
Syntax of if
if(condition)
{
statement/s that will execute if the
condition is true
}
statement/s just below if
True
Syntax of if
if(condition)
{
statement/s that will execute if the
condition is true
}
statement/s just below if
True
Syntax of if
if(condition)
{
statement/s that will execute if the
condition is true
}
statement/s just below if
Syntax of if
if(condition)
{
statement/s that will execute if the
condition is true
}
statement/s just below if
Syntax of if
if(condition)
{
statement/s that will execute if the
condition is true
}
statement/s just below if
False
Syntax of if
if(condition)
{
statement/s that will execute if the
condition is true
}
statement/s just below if
False
Problem
• A program that will display a
number if the user enters
negative number otherwise
won’t display the number.
Solution
#include <stdio.h>
int main()
{
return 0;
}
Solution
#include <stdio.h>
int main()
{
int num;
printf("Enter the number: ");
scanf("%d",&num);
return 0;
}
Solution
#include <stdio.h>
int main()
{
int num;
printf("Enter the number: ");
scanf("%d",&num);
if(num<0){
}
return 0;
}
Solution
#include <stdio.h>
int main()
{
int num;
printf("Enter the number: ");
scanf("%d",&num);
if(num<0){
printf("You entered %dn",num);
}
return 0;
}
Solution
#include <stdio.h>
int main()
{
int num;
printf("Enter the number: ");
scanf("%d",&num);
if(num<0){
printf("You entered %dn",num);
}
printf("Exitn");
return 0;
}
Flow Chart of
if else
Statement
if
condition
Body of if
True
False
Statement just below if else
Body of else
Syntax of if else
if(condition)
{
statement/s that will execute if the condition is true
}
else
{
statement/s that will execute if the condition is false
}
statement/s just below if else
Syntax of if else
if(condition)
{
statement/s that will execute if the condition is true
}
else
{
statement/s that will execute if the condition is false
}
statement/s just below if else
true
Syntax of if else
if(condition)
{
statement/s that will execute if the condition is true
}
else
{
statement/s that will execute if the condition is false
}
statement/s just below if else
true
Syntax of if else
if(condition)
{
statement/s that will execute if the condition is true
}
else
{
statement/s that will execute if the condition is false
}
statement/s just below if else
true
Syntax of if else
if(condition)
{
statement/s that will execute if the condition is true
}
else
{
statement/s that will execute if the condition is false
}
statement/s just below if else
truefalse
Syntax of if else
if(condition)
{
statement/s that will execute if the condition is true
}
else
{
statement/s that will execute if the condition is false
}
statement/s just below if else
truefalse
Syntax of if else
if(condition)
{
statement/s that will execute if the condition is true
}
else
{
statement/s that will execute if the condition is false
}
statement/s just below if else
truefalse
Problem
• A program to check
whether an integer is even
or odd.
Solution#include <stdio.h>
int main()
{
return 0;
}
Solution#include <stdio.h>
int main()
{
int num;
printf("Enter the number: ");
scanf("%d",&num);
return 0;
}
Solution#include <stdio.h>
int main()
{
int num;
printf("Enter the number: ");
scanf("%d",&num);
if(num%2==0){
}
else{
}
return 0;
}
Solution#include <stdio.h>
int main()
{
int num;
printf("Enter the number: ");
scanf("%d",&num);
if(num%2==0){
printf("Evenn");
}
else{
}
return 0;
}
Solution#include <stdio.h>
int main()
{
int num;
printf("Enter the number: ");
scanf("%d",&num);
if(num%2==0){
printf("Evenn");
}
else{
printf("Oddn");
}
return 0;
}
Solution#include <stdio.h>
int main()
{
int num;
printf("Enter the number: ");
scanf("%d",&num);
if(num%2==0){
printf("Evenn");
}
else{
printf("Oddn");
}
printf("Exitn");
return 0;
}
Flow Chart of
if-else if-else
Statement
Syntax of if – else if -else
if (condtion1)
{
statement/s that will execute if condition 1 is true.
}
else if (condition2)
{
statement/s that will execute if the condition 2 is true.
}
...
...
else if (condition n)
{
statement/s that will execute if the condition n is true.
}
else
{
statement/s that will execute if all conditions are false.
}
statement/s below if-else if-else
Syntax of if – else if -else
if (condtion1)
{
statement/s that will execute if condition 1 is true.
}
else if (condition2)
{
statement/s that will execute if the condition 2 is true.
}
...
...
else if (condition n)
{
statement/s that will execute if the condition n is true.
}
else
{
statement/s that will execute if all conditions are false.
}
statement/s below if-else if-else
true
Syntax of if – else if -else
if (condtion1)
{
statement/s that will execute if condition 1 is true.
}
else if (condition2)
{
statement/s that will execute if the condition 2 is true.
}
...
...
else if (condition n)
{
statement/s that will execute if the condition n is true.
}
else
{
statement/s that will execute if all conditions are false.
}
statement/s below if-else if-else
true
Syntax of if – else if -else
if (condtion1)
{
statement/s that will execute if condition 1 is true.
}
else if (condition2)
{
statement/s that will execute if the condition 2 is true.
}
...
...
else if (condition n)
{
statement/s that will execute if the condition n is true.
}
else
{
statement/s that will execute if all conditions are false.
}
statement/s below if-else if-else
true
Syntax of if – else if -else
if (condtion1)
{
statement/s that will execute if condition 1 is true.
}
else if (condition2)
{
statement/s that will execute if the condition 2 is true.
}
...
...
else if (condition n)
{
statement/s that will execute if the condition n is true.
}
else
{
statement/s that will execute if all conditions are false.
}
statement/s below if-else if-else
truefalse
true
Syntax of if – else if -else
if (condtion1)
{
statement/s that will execute if condition 1 is true.
}
else if (condition2)
{
statement/s that will execute if the condition 2 is true.
}
...
...
else if (condition n)
{
statement/s that will execute if the condition n is true.
}
else
{
statement/s that will execute if all conditions are false.
}
statement/s below if-else if-else
truefalse
true
Syntax of if – else if -else
if (condtion1)
{
statement/s that will execute if condition 1 is true.
}
else if (condition2)
{
statement/s that will execute if the condition 2 is true.
}
...
...
else if (condition n)
{
statement/s that will execute if the condition n is true.
}
else
{
statement/s that will execute if all conditions are false.
}
statement/s below if-else if-else
truefalse
true
Syntax of if – else if -else
if (condtion1)
{
statement/s that will execute if condition 1 is true.
}
else if (condition2)
{
statement/s that will execute if the condition 2 is true.
}
...
...
else if (condition n)
{
statement/s that will execute if the condition n is true.
}
else
{
statement/s that will execute if all conditions are false.
}
statement/s below if-else if-else
truefalse
truefalse
true
Syntax of if – else if -else
if (condtion1)
{
statement/s that will execute if condition 1 is true.
}
else if (condition2)
{
statement/s that will execute if the condition 2 is true.
}
...
...
else if (condition n)
{
statement/s that will execute if the condition n is true.
}
else
{
statement/s that will execute if all conditions are false.
}
statement/s below if-else if-else
truefalse
truefalse
true
Syntax of if – else if -else
if (condtion1)
{
statement/s that will execute if condition 1 is true.
}
else if (condition2)
{
statement/s that will execute if the condition 2 is true.
}
...
...
else if (condition n)
{
statement/s that will execute if the condition n is true.
}
else
{
statement/s that will execute if all conditions are false.
}
statement/s below if-else if-else
truefalse
truefalse
true
Syntax of if – else if -else
if (condtion1)
{
statement/s that will execute if condition 1 is true.
}
else if (condition2)
{
statement/s that will execute if the condition 2 is true.
}
...
...
else if (condition n)
{
statement/s that will execute if the condition n is true.
}
else
{
statement/s that will execute if all conditions are false.
}
statement/s below if-else if-else
truefalse
truefalse
truefalse
Syntax of if – else if -else
if (condtion1)
{
statement/s that will execute if condition 1 is true.
}
else if (condition2)
{
statement/s that will execute if the condition 2 is true.
}
...
...
else if (condition n)
{
statement/s that will execute if the condition n is true.
}
else
{
statement/s that will execute if all conditions are false.
}
statement/s below if-else if-else
truefalse
truefalse
truefalse
Problem
• A program to compare two integers and show the
corresponding result (<,=,>).
Solution
#include <stdio.h>
int main()
{
return 0;
}
Solution
#include <stdio.h>
int main()
{
int num1,num2;
printf("Enter two integers: ");
scanf("%d %d",&num1,&num2);
return 0;
}
Solution
#include <stdio.h>
int main()
{
int num1,num2;
printf("Enter two integers: ");
scanf("%d %d",&num1,&num2);
if(num1>num2){
printf("first number is biggern");
}
return 0;
}
Solution
#include <stdio.h>
int main()
{
int num1,num2;
printf("Enter two integers: ");
scanf("%d %d",&num1,&num2);
if(num1>num2){
printf("first number is biggern");
}
else if(num1==num2){
printf("Both numbers are equaln");
}
return 0;
}
Solution
#include <stdio.h>
int main()
{
int num1,num2;
printf("Enter two integers: ");
scanf("%d %d",&num1,&num2);
if(num1>num2){
printf("first number is biggern");
}
else if(num1==num2){
printf("Both numbers are equaln");
}
else{
printf("num2 is biggern");
}
return 0;
}
Syntax of Nested if
if(condition)
{
if(condition)
{
statement;
}
else
{
statement;
}
}
else
{
statement;
}
Nested if else statement is
same like if else
statement, where new
block of if else statement
is defined in existing if or
else block statement.
Problem
• A program to check for a valid username and
password. If the username and password is correct
then show “Login Successful” message in the output.
Solution#include <stdio.h>
int main()
{
return 0;
}
Solution#include <stdio.h>
int main()
{
char uname;
int pass;
printf("Enter the user-name and password: ");
scanf(" %c %d",&uname,&pass);
return 0;
}
Solution#include <stdio.h>
int main()
{
char uname;
int pass;
printf("Enter the user-name and password: ");
scanf(" %c %d",&uname,&pass);
if(uname=='i'){
}
else{
}
return 0;
}
Solution#include <stdio.h>
int main()
{
char uname;
int pass;
printf("Enter the user-name and password: ");
scanf(" %c %d",&uname,&pass);
if(uname=='i'){
if(pass==1234){
printf("Login successfuln");
}
else{
printf("Incorrect passwordn");
}
}
else{
}
return 0;
}
Solution#include <stdio.h>
int main()
{
char uname;
int pass;
printf("Enter the user-name and password: ");
scanf(" %c %d",&uname,&pass);
if(uname=='i'){
if(pass==1234){
printf("Login successfuln");
}
else{
printf("Incorrect passwordn");
}
}
else{
printf("Incorrect user-name, try again.");
}
return 0;
}
Flow Chart of switch
Syntax of switch
switch(expression)
{
case constant-expression :
statement(s);
break; ///optional
case constant-expression :
statement(s);
break; ///optional
… …
/// you can have any number of case statements.
default : ///Optional
statement(s);
}
When the variable being
switched on is equal to a case,
the statements following that
case will execute until a break
statement is reached.
Syntax of switch
switch(expression)
{
case constant-expression :
statement(s);
break; ///optional
case constant-expression :
statement(s);
break; ///optional
… …
/// you can have any number of case statements.
default : ///Optional
statement(s);
}
When a break statement is
reached, the switch terminates,
and the flow of control jumps to
the next line following the
switch statement.
Syntax of switch
switch(expression)
{
case constant-expression :
statement(s);
break; ///optional
case constant-expression :
statement(s);
break; ///optional
… …
/// you can have any number of case statements.
default : ///Optional
statement(s);
}
Not every case needs to contain
a break. If no break appears, the
flow of control will fall
through to subsequent cases
until a break is reached.
Problem
• A program that takes input a grade(char) from user
and categorize it as “Excellent”, ”Well done”, ” You
passed”, “Better try again”, “Invalid grade”.
Solution
#include <stdio.h>
int main()
{
char ch;
scanf(" %c",&ch);
switch(ch){
case 'A':
printf("Excellentn");
break;
case 'B':
case 'C':
printf("Well donen");
break;
case 'D':
printf("You passedn");
break;
case 'F':
printf("Try againn");
break;
default:
printf("Invalid graden");
}
printf("Exitn");
return 0;
}
References
• https://www.programiz.com/c-programming
• https://www.tutorialspoint.com/cprogramming
• http://www.codeforwin.in/2015/05/basic-
programming-practice-problems.html
• Programming with C, Schaum’s Outlines 3rd Edition,
chapter 6  6.1,6.2,6.7

More Related Content

Similar to SPL 7 | Conditional Statements in C

CONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.pptCONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.pptSanjjaayyy
 
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
 
programming c language.
programming c language. programming c language.
programming c language. Abdul Rehman
 
If statements in c programming
If statements in c programmingIf statements in c programming
If statements in c programmingArchana Gopinath
 
Control statements in java
Control statements in javaControl statements in java
Control statements in javaManojkumar C
 
1. Control Structure in C.pdf
1. Control Structure in C.pdf1. Control Structure in C.pdf
1. Control Structure in C.pdfRanjeetaSharma8
 
Chapter 8 - Conditional Statement
Chapter 8 - Conditional StatementChapter 8 - Conditional Statement
Chapter 8 - Conditional StatementDeepak Singh
 
Nesting of if else statement & Else If Ladder
Nesting of if else statement & Else If Ladder Nesting of if else statement & Else If Ladder
Nesting of if else statement & Else If Ladder Vishvesh Jasani
 
C Control Statements.docx
C Control Statements.docxC Control Statements.docx
C Control Statements.docxJavvajiVenkat
 
unit 2-Control Structures.pptx
unit 2-Control Structures.pptxunit 2-Control Structures.pptx
unit 2-Control Structures.pptxishaparte4
 
Conditional Statements [4]
Conditional Statements [4]Conditional Statements [4]
Conditional Statements [4]ecko_disasterz
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statementsSaad Sheikh
 
Fcp chapter 2 upto_if_else (2)
Fcp chapter 2 upto_if_else (2)Fcp chapter 2 upto_if_else (2)
Fcp chapter 2 upto_if_else (2)Jagdish Kamble
 

Similar to SPL 7 | Conditional Statements in C (20)

control statement
control statement control statement
control statement
 
CONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.pptCONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.ppt
 
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
 
programming c language.
programming c language. programming c language.
programming c language.
 
Control Statement programming
Control Statement programmingControl Statement programming
Control Statement programming
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
If statements in c programming
If statements in c programmingIf statements in c programming
If statements in c programming
 
Control statements in java
Control statements in javaControl statements in java
Control statements in java
 
1. Control Structure in C.pdf
1. Control Structure in C.pdf1. Control Structure in C.pdf
1. Control Structure in C.pdf
 
Chapter 8 - Conditional Statement
Chapter 8 - Conditional StatementChapter 8 - Conditional Statement
Chapter 8 - Conditional Statement
 
Nesting of if else statement & Else If Ladder
Nesting of if else statement & Else If Ladder Nesting of if else statement & Else If Ladder
Nesting of if else statement & Else If Ladder
 
C Control Statements.docx
C Control Statements.docxC Control Statements.docx
C Control Statements.docx
 
unit 2-Control Structures.pptx
unit 2-Control Structures.pptxunit 2-Control Structures.pptx
unit 2-Control Structures.pptx
 
Conditional Statements [4]
Conditional Statements [4]Conditional Statements [4]
Conditional Statements [4]
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
 
175035 cse lab-03
175035 cse lab-03175035 cse lab-03
175035 cse lab-03
 
Fcp chapter 2 upto_if_else (2)
Fcp chapter 2 upto_if_else (2)Fcp chapter 2 upto_if_else (2)
Fcp chapter 2 upto_if_else (2)
 
Decision Making.pptx
Decision Making.pptxDecision Making.pptx
Decision Making.pptx
 
Selection structure
Selection structureSelection structure
Selection structure
 

More from Mohammad Imam Hossain

DS & Algo 4 - Graph and Shortest Path Search
DS & Algo 4 - Graph and Shortest Path SearchDS & Algo 4 - Graph and Shortest Path Search
DS & Algo 4 - Graph and Shortest Path SearchMohammad Imam Hossain
 
DS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL IntroductionDS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL IntroductionMohammad Imam Hossain
 
DBMS 3 | ER Diagram to Relational Schema
DBMS 3 | ER Diagram to Relational SchemaDBMS 3 | ER Diagram to Relational Schema
DBMS 3 | ER Diagram to Relational SchemaMohammad Imam Hossain
 
DBMS 5 | MySQL Practice List - HR Schema
DBMS 5 | MySQL Practice List - HR SchemaDBMS 5 | MySQL Practice List - HR Schema
DBMS 5 | MySQL Practice List - HR SchemaMohammad Imam Hossain
 
TOC 8 | Derivation, Parse Tree & Ambiguity Check
TOC 8 | Derivation, Parse Tree & Ambiguity CheckTOC 8 | Derivation, Parse Tree & Ambiguity Check
TOC 8 | Derivation, Parse Tree & Ambiguity CheckMohammad Imam Hossain
 

More from Mohammad Imam Hossain (20)

DS & Algo 6 - Offline Assignment 6
DS & Algo 6 - Offline Assignment 6DS & Algo 6 - Offline Assignment 6
DS & Algo 6 - Offline Assignment 6
 
DS & Algo 6 - Dynamic Programming
DS & Algo 6 - Dynamic ProgrammingDS & Algo 6 - Dynamic Programming
DS & Algo 6 - Dynamic Programming
 
DS & Algo 5 - Disjoint Set and MST
DS & Algo 5 - Disjoint Set and MSTDS & Algo 5 - Disjoint Set and MST
DS & Algo 5 - Disjoint Set and MST
 
DS & Algo 4 - Graph and Shortest Path Search
DS & Algo 4 - Graph and Shortest Path SearchDS & Algo 4 - Graph and Shortest Path Search
DS & Algo 4 - Graph and Shortest Path Search
 
DS & Algo 3 - Offline Assignment 3
DS & Algo 3 - Offline Assignment 3DS & Algo 3 - Offline Assignment 3
DS & Algo 3 - Offline Assignment 3
 
DS & Algo 3 - Divide and Conquer
DS & Algo 3 - Divide and ConquerDS & Algo 3 - Divide and Conquer
DS & Algo 3 - Divide and Conquer
 
DS & Algo 2 - Offline Assignment 2
DS & Algo 2 - Offline Assignment 2DS & Algo 2 - Offline Assignment 2
DS & Algo 2 - Offline Assignment 2
 
DS & Algo 2 - Recursion
DS & Algo 2 - RecursionDS & Algo 2 - Recursion
DS & Algo 2 - Recursion
 
DS & Algo 1 - Offline Assignment 1
DS & Algo 1 - Offline Assignment 1DS & Algo 1 - Offline Assignment 1
DS & Algo 1 - Offline Assignment 1
 
DS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL IntroductionDS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL Introduction
 
DBMS 1 | Introduction to DBMS
DBMS 1 | Introduction to DBMSDBMS 1 | Introduction to DBMS
DBMS 1 | Introduction to DBMS
 
DBMS 10 | Database Transactions
DBMS 10 | Database TransactionsDBMS 10 | Database Transactions
DBMS 10 | Database Transactions
 
DBMS 3 | ER Diagram to Relational Schema
DBMS 3 | ER Diagram to Relational SchemaDBMS 3 | ER Diagram to Relational Schema
DBMS 3 | ER Diagram to Relational Schema
 
DBMS 2 | Entity Relationship Model
DBMS 2 | Entity Relationship ModelDBMS 2 | Entity Relationship Model
DBMS 2 | Entity Relationship Model
 
DBMS 7 | Relational Query Language
DBMS 7 | Relational Query LanguageDBMS 7 | Relational Query Language
DBMS 7 | Relational Query Language
 
DBMS 4 | MySQL - DDL & DML Commands
DBMS 4 | MySQL - DDL & DML CommandsDBMS 4 | MySQL - DDL & DML Commands
DBMS 4 | MySQL - DDL & DML Commands
 
DBMS 5 | MySQL Practice List - HR Schema
DBMS 5 | MySQL Practice List - HR SchemaDBMS 5 | MySQL Practice List - HR Schema
DBMS 5 | MySQL Practice List - HR Schema
 
TOC 10 | Turing Machine
TOC 10 | Turing MachineTOC 10 | Turing Machine
TOC 10 | Turing Machine
 
TOC 9 | Pushdown Automata
TOC 9 | Pushdown AutomataTOC 9 | Pushdown Automata
TOC 9 | Pushdown Automata
 
TOC 8 | Derivation, Parse Tree & Ambiguity Check
TOC 8 | Derivation, Parse Tree & Ambiguity CheckTOC 8 | Derivation, Parse Tree & Ambiguity Check
TOC 8 | Derivation, Parse Tree & Ambiguity Check
 

Recently uploaded

Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersChitralekhaTherkar
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 

Recently uploaded (20)

Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of Powders
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 

SPL 7 | Conditional Statements in C

  • 1. Structured Programming Language Conditional Statements in C Mohammad Imam Hossain, Lecturer, CSE, UIU • if • if-else • if-else if-else • Nested if • switch
  • 2. Introduction • Till now, The instructions were executed in the same order in which they appeared within the program. (Sequence) • Branching: one of several possible actions will be carried out, depending on the outcome of the logical test. • Selection: One group of statements is selected from several available groups.
  • 3. Flow Chart of if Statement if condition Body of if True False Statement just below if
  • 4. Syntax of if if(condition) { statement/s that will execute if the condition is true } statement/s just below if
  • 5. Syntax of if if(condition) { statement/s that will execute if the condition is true } statement/s just below if
  • 6. Syntax of if if(condition) { statement/s that will execute if the condition is true } statement/s just below if True
  • 7. Syntax of if if(condition) { statement/s that will execute if the condition is true } statement/s just below if True
  • 8. Syntax of if if(condition) { statement/s that will execute if the condition is true } statement/s just below if True
  • 9. Syntax of if if(condition) { statement/s that will execute if the condition is true } statement/s just below if
  • 10. Syntax of if if(condition) { statement/s that will execute if the condition is true } statement/s just below if
  • 11. Syntax of if if(condition) { statement/s that will execute if the condition is true } statement/s just below if False
  • 12. Syntax of if if(condition) { statement/s that will execute if the condition is true } statement/s just below if False
  • 13. Problem • A program that will display a number if the user enters negative number otherwise won’t display the number.
  • 15. Solution #include <stdio.h> int main() { int num; printf("Enter the number: "); scanf("%d",&num); return 0; }
  • 16. Solution #include <stdio.h> int main() { int num; printf("Enter the number: "); scanf("%d",&num); if(num<0){ } return 0; }
  • 17. Solution #include <stdio.h> int main() { int num; printf("Enter the number: "); scanf("%d",&num); if(num<0){ printf("You entered %dn",num); } return 0; }
  • 18. Solution #include <stdio.h> int main() { int num; printf("Enter the number: "); scanf("%d",&num); if(num<0){ printf("You entered %dn",num); } printf("Exitn"); return 0; }
  • 19.
  • 20. Flow Chart of if else Statement if condition Body of if True False Statement just below if else Body of else
  • 21. Syntax of if else if(condition) { statement/s that will execute if the condition is true } else { statement/s that will execute if the condition is false } statement/s just below if else
  • 22. Syntax of if else if(condition) { statement/s that will execute if the condition is true } else { statement/s that will execute if the condition is false } statement/s just below if else true
  • 23. Syntax of if else if(condition) { statement/s that will execute if the condition is true } else { statement/s that will execute if the condition is false } statement/s just below if else true
  • 24. Syntax of if else if(condition) { statement/s that will execute if the condition is true } else { statement/s that will execute if the condition is false } statement/s just below if else true
  • 25. Syntax of if else if(condition) { statement/s that will execute if the condition is true } else { statement/s that will execute if the condition is false } statement/s just below if else truefalse
  • 26. Syntax of if else if(condition) { statement/s that will execute if the condition is true } else { statement/s that will execute if the condition is false } statement/s just below if else truefalse
  • 27. Syntax of if else if(condition) { statement/s that will execute if the condition is true } else { statement/s that will execute if the condition is false } statement/s just below if else truefalse
  • 28. Problem • A program to check whether an integer is even or odd.
  • 30. Solution#include <stdio.h> int main() { int num; printf("Enter the number: "); scanf("%d",&num); return 0; }
  • 31. Solution#include <stdio.h> int main() { int num; printf("Enter the number: "); scanf("%d",&num); if(num%2==0){ } else{ } return 0; }
  • 32. Solution#include <stdio.h> int main() { int num; printf("Enter the number: "); scanf("%d",&num); if(num%2==0){ printf("Evenn"); } else{ } return 0; }
  • 33. Solution#include <stdio.h> int main() { int num; printf("Enter the number: "); scanf("%d",&num); if(num%2==0){ printf("Evenn"); } else{ printf("Oddn"); } return 0; }
  • 34. Solution#include <stdio.h> int main() { int num; printf("Enter the number: "); scanf("%d",&num); if(num%2==0){ printf("Evenn"); } else{ printf("Oddn"); } printf("Exitn"); return 0; }
  • 35. Flow Chart of if-else if-else Statement
  • 36. Syntax of if – else if -else if (condtion1) { statement/s that will execute if condition 1 is true. } else if (condition2) { statement/s that will execute if the condition 2 is true. } ... ... else if (condition n) { statement/s that will execute if the condition n is true. } else { statement/s that will execute if all conditions are false. } statement/s below if-else if-else
  • 37. Syntax of if – else if -else if (condtion1) { statement/s that will execute if condition 1 is true. } else if (condition2) { statement/s that will execute if the condition 2 is true. } ... ... else if (condition n) { statement/s that will execute if the condition n is true. } else { statement/s that will execute if all conditions are false. } statement/s below if-else if-else true
  • 38. Syntax of if – else if -else if (condtion1) { statement/s that will execute if condition 1 is true. } else if (condition2) { statement/s that will execute if the condition 2 is true. } ... ... else if (condition n) { statement/s that will execute if the condition n is true. } else { statement/s that will execute if all conditions are false. } statement/s below if-else if-else true
  • 39. Syntax of if – else if -else if (condtion1) { statement/s that will execute if condition 1 is true. } else if (condition2) { statement/s that will execute if the condition 2 is true. } ... ... else if (condition n) { statement/s that will execute if the condition n is true. } else { statement/s that will execute if all conditions are false. } statement/s below if-else if-else true
  • 40. Syntax of if – else if -else if (condtion1) { statement/s that will execute if condition 1 is true. } else if (condition2) { statement/s that will execute if the condition 2 is true. } ... ... else if (condition n) { statement/s that will execute if the condition n is true. } else { statement/s that will execute if all conditions are false. } statement/s below if-else if-else truefalse true
  • 41. Syntax of if – else if -else if (condtion1) { statement/s that will execute if condition 1 is true. } else if (condition2) { statement/s that will execute if the condition 2 is true. } ... ... else if (condition n) { statement/s that will execute if the condition n is true. } else { statement/s that will execute if all conditions are false. } statement/s below if-else if-else truefalse true
  • 42. Syntax of if – else if -else if (condtion1) { statement/s that will execute if condition 1 is true. } else if (condition2) { statement/s that will execute if the condition 2 is true. } ... ... else if (condition n) { statement/s that will execute if the condition n is true. } else { statement/s that will execute if all conditions are false. } statement/s below if-else if-else truefalse true
  • 43. Syntax of if – else if -else if (condtion1) { statement/s that will execute if condition 1 is true. } else if (condition2) { statement/s that will execute if the condition 2 is true. } ... ... else if (condition n) { statement/s that will execute if the condition n is true. } else { statement/s that will execute if all conditions are false. } statement/s below if-else if-else truefalse truefalse true
  • 44. Syntax of if – else if -else if (condtion1) { statement/s that will execute if condition 1 is true. } else if (condition2) { statement/s that will execute if the condition 2 is true. } ... ... else if (condition n) { statement/s that will execute if the condition n is true. } else { statement/s that will execute if all conditions are false. } statement/s below if-else if-else truefalse truefalse true
  • 45. Syntax of if – else if -else if (condtion1) { statement/s that will execute if condition 1 is true. } else if (condition2) { statement/s that will execute if the condition 2 is true. } ... ... else if (condition n) { statement/s that will execute if the condition n is true. } else { statement/s that will execute if all conditions are false. } statement/s below if-else if-else truefalse truefalse true
  • 46. Syntax of if – else if -else if (condtion1) { statement/s that will execute if condition 1 is true. } else if (condition2) { statement/s that will execute if the condition 2 is true. } ... ... else if (condition n) { statement/s that will execute if the condition n is true. } else { statement/s that will execute if all conditions are false. } statement/s below if-else if-else truefalse truefalse truefalse
  • 47. Syntax of if – else if -else if (condtion1) { statement/s that will execute if condition 1 is true. } else if (condition2) { statement/s that will execute if the condition 2 is true. } ... ... else if (condition n) { statement/s that will execute if the condition n is true. } else { statement/s that will execute if all conditions are false. } statement/s below if-else if-else truefalse truefalse truefalse
  • 48. Problem • A program to compare two integers and show the corresponding result (<,=,>).
  • 50. Solution #include <stdio.h> int main() { int num1,num2; printf("Enter two integers: "); scanf("%d %d",&num1,&num2); return 0; }
  • 51. Solution #include <stdio.h> int main() { int num1,num2; printf("Enter two integers: "); scanf("%d %d",&num1,&num2); if(num1>num2){ printf("first number is biggern"); } return 0; }
  • 52. Solution #include <stdio.h> int main() { int num1,num2; printf("Enter two integers: "); scanf("%d %d",&num1,&num2); if(num1>num2){ printf("first number is biggern"); } else if(num1==num2){ printf("Both numbers are equaln"); } return 0; }
  • 53. Solution #include <stdio.h> int main() { int num1,num2; printf("Enter two integers: "); scanf("%d %d",&num1,&num2); if(num1>num2){ printf("first number is biggern"); } else if(num1==num2){ printf("Both numbers are equaln"); } else{ printf("num2 is biggern"); } return 0; }
  • 54. Syntax of Nested if if(condition) { if(condition) { statement; } else { statement; } } else { statement; } Nested if else statement is same like if else statement, where new block of if else statement is defined in existing if or else block statement.
  • 55. Problem • A program to check for a valid username and password. If the username and password is correct then show “Login Successful” message in the output.
  • 57. Solution#include <stdio.h> int main() { char uname; int pass; printf("Enter the user-name and password: "); scanf(" %c %d",&uname,&pass); return 0; }
  • 58. Solution#include <stdio.h> int main() { char uname; int pass; printf("Enter the user-name and password: "); scanf(" %c %d",&uname,&pass); if(uname=='i'){ } else{ } return 0; }
  • 59. Solution#include <stdio.h> int main() { char uname; int pass; printf("Enter the user-name and password: "); scanf(" %c %d",&uname,&pass); if(uname=='i'){ if(pass==1234){ printf("Login successfuln"); } else{ printf("Incorrect passwordn"); } } else{ } return 0; }
  • 60. Solution#include <stdio.h> int main() { char uname; int pass; printf("Enter the user-name and password: "); scanf(" %c %d",&uname,&pass); if(uname=='i'){ if(pass==1234){ printf("Login successfuln"); } else{ printf("Incorrect passwordn"); } } else{ printf("Incorrect user-name, try again."); } return 0; }
  • 61. Flow Chart of switch
  • 62. Syntax of switch switch(expression) { case constant-expression : statement(s); break; ///optional case constant-expression : statement(s); break; ///optional … … /// you can have any number of case statements. default : ///Optional statement(s); } When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.
  • 63. Syntax of switch switch(expression) { case constant-expression : statement(s); break; ///optional case constant-expression : statement(s); break; ///optional … … /// you can have any number of case statements. default : ///Optional statement(s); } When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.
  • 64. Syntax of switch switch(expression) { case constant-expression : statement(s); break; ///optional case constant-expression : statement(s); break; ///optional … … /// you can have any number of case statements. default : ///Optional statement(s); } Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached.
  • 65. Problem • A program that takes input a grade(char) from user and categorize it as “Excellent”, ”Well done”, ” You passed”, “Better try again”, “Invalid grade”.
  • 66. Solution #include <stdio.h> int main() { char ch; scanf(" %c",&ch); switch(ch){ case 'A': printf("Excellentn"); break; case 'B': case 'C': printf("Well donen"); break; case 'D': printf("You passedn"); break; case 'F': printf("Try againn"); break; default: printf("Invalid graden"); } printf("Exitn"); return 0; }
  • 67. References • https://www.programiz.com/c-programming • https://www.tutorialspoint.com/cprogramming • http://www.codeforwin.in/2015/05/basic- programming-practice-problems.html • Programming with C, Schaum’s Outlines 3rd Edition, chapter 6  6.1,6.2,6.7