SlideShare a Scribd company logo
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

control statement
control statement control statement
control statement
Kathmandu University
 
CONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.pptCONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.ppt
Sanjjaayyy
 
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
alish 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_selection
alish sha
 
programming c language.
programming c language. programming c language.
programming c language.
Abdul Rehman
 
Control Statement programming
Control Statement programmingControl Statement programming
Control Statement programming
University of Potsdam
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
Mohammed Khan
 
If statements in c programming
If statements in c programmingIf statements in c programming
If statements in c programming
Archana Gopinath
 
Control statements in java
Control statements in javaControl statements in java
Control statements in java
Manojkumar C
 
1. Control Structure in C.pdf
1. Control Structure in C.pdf1. Control Structure in C.pdf
1. Control Structure in C.pdf
RanjeetaSharma8
 
Chapter 8 - Conditional Statement
Chapter 8 - Conditional StatementChapter 8 - Conditional Statement
Chapter 8 - Conditional Statement
Deepak 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.docx
JavvajiVenkat
 
unit 2-Control Structures.pptx
unit 2-Control Structures.pptxunit 2-Control Structures.pptx
unit 2-Control Structures.pptx
ishaparte4
 
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 statements
Saad Sheikh
 
175035 cse lab-03
175035 cse lab-03175035 cse lab-03
175035 cse lab-03
Mahbubay Rabbani Mim
 
Basics of Control Statement in C Languages
Basics of Control Statement in C LanguagesBasics of Control Statement in C Languages
Basics of Control Statement in C Languages
Chandrakant Divate
 
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
 
Unit_3A_If_Else_Switch and a if else statment example
Unit_3A_If_Else_Switch and a if else statment exampleUnit_3A_If_Else_Switch and a if else statment example
Unit_3A_If_Else_Switch and a if else statment example
umaghosal12101974
 

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
 
Basics of Control Statement in C Languages
Basics of Control Statement in C LanguagesBasics of Control Statement in C Languages
Basics of Control Statement in C Languages
 
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)
 
Unit_3A_If_Else_Switch and a if else statment example
Unit_3A_If_Else_Switch and a if else statment exampleUnit_3A_If_Else_Switch and a if else statment example
Unit_3A_If_Else_Switch and a if else statment example
 

More from Mohammad Imam Hossain

DS & Algo 6 - Offline Assignment 6
DS & Algo 6 - Offline Assignment 6DS & Algo 6 - Offline Assignment 6
DS & Algo 6 - Offline Assignment 6
Mohammad Imam Hossain
 
DS & Algo 6 - Dynamic Programming
DS & Algo 6 - Dynamic ProgrammingDS & Algo 6 - Dynamic Programming
DS & Algo 6 - Dynamic Programming
Mohammad Imam Hossain
 
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
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 Search
Mohammad Imam Hossain
 
DS & Algo 3 - Offline Assignment 3
DS & Algo 3 - Offline Assignment 3DS & Algo 3 - Offline Assignment 3
DS & Algo 3 - Offline Assignment 3
Mohammad Imam Hossain
 
DS & Algo 3 - Divide and Conquer
DS & Algo 3 - Divide and ConquerDS & Algo 3 - Divide and Conquer
DS & Algo 3 - Divide and Conquer
Mohammad Imam Hossain
 
DS & Algo 2 - Offline Assignment 2
DS & Algo 2 - Offline Assignment 2DS & Algo 2 - Offline Assignment 2
DS & Algo 2 - Offline Assignment 2
Mohammad Imam Hossain
 
DS & Algo 2 - Recursion
DS & Algo 2 - RecursionDS & Algo 2 - Recursion
DS & Algo 2 - Recursion
Mohammad Imam Hossain
 
DS & Algo 1 - Offline Assignment 1
DS & Algo 1 - Offline Assignment 1DS & Algo 1 - Offline Assignment 1
DS & Algo 1 - Offline Assignment 1
Mohammad 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 Introduction
Mohammad Imam Hossain
 
DBMS 1 | Introduction to DBMS
DBMS 1 | Introduction to DBMSDBMS 1 | Introduction to DBMS
DBMS 1 | Introduction to DBMS
Mohammad Imam Hossain
 
DBMS 10 | Database Transactions
DBMS 10 | Database TransactionsDBMS 10 | Database Transactions
DBMS 10 | Database Transactions
Mohammad 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 Schema
Mohammad Imam Hossain
 
DBMS 2 | Entity Relationship Model
DBMS 2 | Entity Relationship ModelDBMS 2 | Entity Relationship Model
DBMS 2 | Entity Relationship Model
Mohammad Imam Hossain
 
DBMS 7 | Relational Query Language
DBMS 7 | Relational Query LanguageDBMS 7 | Relational Query Language
DBMS 7 | Relational Query Language
Mohammad Imam Hossain
 
DBMS 4 | MySQL - DDL & DML Commands
DBMS 4 | MySQL - DDL & DML CommandsDBMS 4 | MySQL - DDL & DML Commands
DBMS 4 | MySQL - DDL & DML Commands
Mohammad 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 Schema
Mohammad Imam Hossain
 
TOC 10 | Turing Machine
TOC 10 | Turing MachineTOC 10 | Turing Machine
TOC 10 | Turing Machine
Mohammad Imam Hossain
 
TOC 9 | Pushdown Automata
TOC 9 | Pushdown AutomataTOC 9 | Pushdown Automata
TOC 9 | Pushdown Automata
Mohammad 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 Check
Mohammad 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

Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
Jyoti Chand
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptxBeyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
EduSkills OECD
 
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching AptitudeUGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
S. Raj Kumar
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience
Wahiba Chair Training & Consulting
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
B. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdfB. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdf
BoudhayanBhattachari
 
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) CurriculumPhilippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
MJDuyan
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
RAHUL
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
Celine George
 
ZK on Polkadot zero knowledge proofs - sub0.pptx
ZK on Polkadot zero knowledge proofs - sub0.pptxZK on Polkadot zero knowledge proofs - sub0.pptx
ZK on Polkadot zero knowledge proofs - sub0.pptx
dot55audits
 
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
Nguyen Thanh Tu Collection
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
siemaillard
 
math operations ued in python and all used
math operations ued in python and all usedmath operations ued in python and all used
math operations ued in python and all used
ssuser13ffe4
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
สมใจ จันสุกสี
 

Recently uploaded (20)

Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptxBeyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
 
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching AptitudeUGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
B. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdfB. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdf
 
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) CurriculumPhilippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
 
ZK on Polkadot zero knowledge proofs - sub0.pptx
ZK on Polkadot zero knowledge proofs - sub0.pptxZK on Polkadot zero knowledge proofs - sub0.pptx
ZK on Polkadot zero knowledge proofs - sub0.pptx
 
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
 
math operations ued in python and all used
math operations ued in python and all usedmath operations ued in python and all used
math operations ued in python and all used
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
 

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