If-else if-else statement
Saumya Som
BWU/BCA/17/207
BCA-D (2nd Semester)
Teacher- ANOY CHOWDHURY
CONTENTS
1 C
Header File and Main Function
Conditional Statements
If Condition
If-else Condition
2
3
4
5
6 If-else if Condition
1
C
C was originally developed in the 1970s, by
Dennis Ritchie at Bell Telephone
Laboratories, Inc.
C is a High level , general –purpose
structured programming language.
Instructions of C consists of terms that are
very closely same to algebraic expressions,
consisting of certain English keywords such
as if, else, for ,do and while
2
Program
structure
#include<stdio.h>
int main()
{
- statements
}
3
Header
filesThe files that are specified in the include
section is called as header file . These are
precompiled files that has some functions
defined in them . We can call those functions
in our program by supplying parameters
Header file is given an extension .h
C Source file is given an extension .c
4
Main
function
This is the entry point of a program .When a file
is executed, the start point is the main function .
From main function the flow goes as per the
programmers choice . There may or may not be
other functions written by user in a program
Main function is compulsory for any c program
1
2
3
4
5
Conditional Statements Are Used To Execute A
Set Of Statements On Some Conditions. It
Provides A Unit Of Block In Which We Can Either
One Statement Or More Than One Statement . If
The Given Condition Is True Then The Set Of
Statements Are Executed Otherwise Body Is
Skipped.
C supports two types of selection
statements :-
The if statement
The switch statement
Conditional
Statements
6
If
Condition
It is conditional statement, which is used to
execute a set of statement on some
conditions. The condition must be of Boolean
type expression.
An expression, which returns only two value
either TRUE or FALSE, is known as Boolean
type expression.
Syntax:-
if (expression)
statement(s);
7
Example
#include <stdio.h>
void main()
{
int x, y;
char a = ‘y’;
x = y = 0;
if (a == ‘y’)
{
x += 5;
printf(“The numbers are %d and t%d”, x, y);
}
}
8
If-else
ConditionIt Is Known As Double Blocked Conditional
Statements .It Means, It Has TRUE Parts As
Well As FALSE Part.
If The Given Condition Is True Then The True
Part Is Executed Otherwise False Part Is
Executed.
Syntax:-
if (expression)
statement(s);
[else statement(s);]
9
Example
#include <stdio.h>
void main()
{
int num , res ;
printf(“Enter a number :”);
scanf(“%d”,&num);
res = num % 2;
if (res == 0)
printf(“Then number is Even”);
else
printf(“The number is Odd”);
}
10
If-else if
Condition
The if...else statement executes two different
codes depending upon whether the test
expression is true or false. Sometimes, a choice
has to be made from more than 2 possibilities.
Syntax:-
if ( this logical expression is true )
statement ;
else if ( this logical expression is true
)
statement ;
else if ( this logical
expression is true )
statement ;
else
11
Example
#include <stdio.h>
void main()
{
int m=40,n=20;
if (m>n)
{
printf("m is greater than
n");
}
else if(m<n)
{
printf("m is less than n");
}
else
{
printf("m is equal to n");
}
}
Referen
ces
Programiz
https://www.programiz.com/c-programming/c-if-else-statement
YouTube tutorial
https://www.youtube.com/watch?v=JxjUyJkjGHI
YouTube tutorial
https://www.youtube.com/watch?v=0lKWFArcTGU
12
If Else Statement

If Else Statement

  • 1.
    If-else if-else statement SaumyaSom BWU/BCA/17/207 BCA-D (2nd Semester) Teacher- ANOY CHOWDHURY
  • 2.
    CONTENTS 1 C Header Fileand Main Function Conditional Statements If Condition If-else Condition 2 3 4 5 6 If-else if Condition
  • 3.
    1 C C was originallydeveloped in the 1970s, by Dennis Ritchie at Bell Telephone Laboratories, Inc. C is a High level , general –purpose structured programming language. Instructions of C consists of terms that are very closely same to algebraic expressions, consisting of certain English keywords such as if, else, for ,do and while
  • 4.
  • 5.
    3 Header filesThe files thatare specified in the include section is called as header file . These are precompiled files that has some functions defined in them . We can call those functions in our program by supplying parameters Header file is given an extension .h C Source file is given an extension .c
  • 6.
    4 Main function This is theentry point of a program .When a file is executed, the start point is the main function . From main function the flow goes as per the programmers choice . There may or may not be other functions written by user in a program Main function is compulsory for any c program 1 2 3 4
  • 7.
    5 Conditional Statements AreUsed To Execute A Set Of Statements On Some Conditions. It Provides A Unit Of Block In Which We Can Either One Statement Or More Than One Statement . If The Given Condition Is True Then The Set Of Statements Are Executed Otherwise Body Is Skipped. C supports two types of selection statements :- The if statement The switch statement Conditional Statements
  • 8.
    6 If Condition It is conditionalstatement, which is used to execute a set of statement on some conditions. The condition must be of Boolean type expression. An expression, which returns only two value either TRUE or FALSE, is known as Boolean type expression. Syntax:- if (expression) statement(s);
  • 9.
    7 Example #include <stdio.h> void main() { intx, y; char a = ‘y’; x = y = 0; if (a == ‘y’) { x += 5; printf(“The numbers are %d and t%d”, x, y); } }
  • 10.
    8 If-else ConditionIt Is KnownAs Double Blocked Conditional Statements .It Means, It Has TRUE Parts As Well As FALSE Part. If The Given Condition Is True Then The True Part Is Executed Otherwise False Part Is Executed. Syntax:- if (expression) statement(s); [else statement(s);]
  • 11.
    9 Example #include <stdio.h> void main() { intnum , res ; printf(“Enter a number :”); scanf(“%d”,&num); res = num % 2; if (res == 0) printf(“Then number is Even”); else printf(“The number is Odd”); }
  • 12.
    10 If-else if Condition The if...elsestatement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities. Syntax:- if ( this logical expression is true ) statement ; else if ( this logical expression is true ) statement ; else if ( this logical expression is true ) statement ; else
  • 13.
    11 Example #include <stdio.h> void main() { intm=40,n=20; if (m>n) { printf("m is greater than n"); } else if(m<n) { printf("m is less than n"); } else { printf("m is equal to n"); } }
  • 14.