You are on page 1of 16

CSE-105: Structured Programming

Language

Shahriar Rahman Khan


Lecturer, Department of CSE, MIST
Email: shahriarkhan@cse.mist.ac.bd

1
Objectives of the Course
❏ To understand the basics of Computers.
❏ To understand the basics of computer programming in C.
❏ To think and analyze about problem
❏ To solve elementary programming problems.

10/17/2022 Lec Shahriar Rahman Khan 22


CSE-105: Structured Programming
Language

Lec 5: IF ELSE in C

Shahriar Rahman Khan


Lecturer, Department of CSE, MIST
Email: shahriarkhan@cse.mist.ac.bd

10/17/2022 6
Single If in C

if (expression)
{
statement;
…………
statement;
}

Syntax
Single if

12/10/2021 Lec Shahriar Rahman Khan 7


Single If in C

12/10/2021 Lec Shahriar Rahman Khan 8


If ELSE in C
if (expression)
{
statement;
…………
statement;
}
else
{
statement;
…………
statement;
}
If and else Syntax

12/10/2021 Lec Shahriar Rahman Khan 9


If ELSE in C

Code Output

12/10/2021 Lec Shahriar Rahman Khan 10


Sequence of ‘if’s
if (expression)
{
statements;
}
if(expression)
{
statements;
}
......................
if(expression)
{
statements;
}

12/10/2021 Lec Shahriar Rahman Khan 11


Sequence of ‘if’s

12/10/2021 Lec Shahriar Rahman Khan 12


IF ELSE Ladder
if (expression)
{
statements;
}
else if(expression)
{
statements;
}
......................
else
{
statements;
}

12/10/2021 Lec Shahriar Rahman Khan 13


IF ELSE Ladder

12/10/2021 Lec Shahriar Rahman Khan 14


Control Statements

Type When to Use


A single If Just need to check one condition

Sequence of only “If”s Need to check multiple condition one after another
(usually on same vars, might be true for multiple cases)
If and else Only two conditions, only one of them might be true.
Second condition doesn’t even need to be checked.

If- else-if- else Ladder Only one condition out of multiple may be true. Check
them one after another. The last or default condition is
not checked and written with an “else”

12/10/2021 Lec Shahriar Rahman Khan 15


Switch Statement
switch (expression)
​{
case constant 1: // statements

break;
case constant 2: // statements

break;
...
default: // default statements
}

12/10/2021 Lec Shahriar Rahman Khan 16


Switch Statement

12/10/2021 Lec Shahriar Rahman Khan 17


Our Motivation

10/17/2022 Lec Shahriar Rahman Khan 24


GOOD NEWS!
THE CLASS IS
OVER…
THANK YOU!

10/17/2022 25

You might also like