0% found this document useful (0 votes)
26 views27 pages

LESSON 7-Decision Statement

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 27

DECISION STATEMENTS

LESSON 9
DECISION STATEMENTS
Decision making is critical to computer
programming.
There will be many situations when you
will be given two or more options and you
will have to select an option based on the
given conditions.
For example, we want to print a remark
about a student based on his secured
marks.(Refer to Page 39 of book).
DECISION STATEMENTS
Now, the question is how to write a
programming code to handle such
situations.
Almost all the programming languages
provide conditional statements that
work based on the following flow
diagram:
Refer to book ( Page 39)
DECISION STATEMENTS
Let's write a C program
with the help of if
conditional statements to
convert the given situation
into a programming code:

DECISION STATEMENTS
This lecture will discuss the
following:
various forms of if statements
and
an introduction to switch
statements available in C
programming language.
DECISION STATEMENTS
Different programming
languages provide different
types of decision-making
statements, but the basic
concept remains the same.
if...else statement
if...else statement
An if statement can be
followed by an optional
else statement, which
executes when the Boolean
expression is false.
SYNTAX OF IF…..ELSE
STATEMENT
The syntax of an
if...else statement in C
programming language
is:
SYNTAX OF IF….ELSE
STATEMENT
if(boolean_expression)
{
/* Statement(s) will execute if the Boolean
expression is true */
}
Else
{
/* Statement(s) will execute if the Boolean
expression is false */
}
if...else statement
if...else statement
An if...else statement is useful when
we have to take a decision out of two
options.
For example, if a student secures more
marks than 95, then the student is
brilliant, otherwise no such situation
can be coded, as follows:

if...else if...else statement
if...else if...else statement
An if statement can be followed
by an optional else if...else
statement, which is very useful to
test various conditions.
While using if, else if, else
statements, there are a few points
to keep in mind:
if...else if...else statement
An if can have zero or one
else's and it must come
after an else if.
An if can have zero to
many else…if's and they
must come before the else.
if...else if...else statement
Once an else…if succeeds, none
of the remaining else…if's or
else's will be tested.
The syntax of an if...else if...else
statement in C programming
language is:
if...else if...else statement
Now with the help of
if...else if...else statement,
Recode the very first
program
The switch statement
The switch statement
A switch statement is an
alternative of if statements
which allows a variable to
be tested for equality against
a list of values.
THE SWITCH STATEMENT
Each value is called a case,
and the variable being
switched on is checked for
each switch case.
It has the following syntax:
The switch statement
The expression used in a
switch statement must give
an integer value, which
will be compared for
equality with different cases
given.
The switch statement
Wherever an expression
value matches with a case
value, the body of that case
will be executed and finally,
the switch will be terminated
using a break statement.
The switch statement
If no break statements are provided, then the
computer continues executing other statements
available below to the matched case.
 If none of the cases matches, then the default case
body is executed.
The above syntax can be represented in the form of a
flow diagram as shown below:
Refer to page 45
The switch statement
Now, let's consider another
example where we want to
write the equivalent English
word for a given number.
Then, it can be coded as
follows:
Laboratory Exercise

Check EVEN or ODD


program using
switch statement
If statement

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy