0% found this document useful (0 votes)
122 views37 pages

Decision Making Statements

Decision making statement

Uploaded by

karthicksrujan16
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
122 views37 pages

Decision Making Statements

Decision making statement

Uploaded by

karthicksrujan16
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 37

23ADT001 - C Programming

Module I
Topic – Decision Statements

23ADT001 - C PROGRAMMING
• Conditional statements –
• If
• if else
• else if ladder
• Nested if
• Nested if else and
• switch statements

23ADT001 - C PROGRAMMING
Program Control Structure
• A Program is usually not limited to a linear sequence of instructions

• During its process, it may diverge, repeat code or take decisions

• Program statements that affect the order in which statements are executed or that
affect whether statements are executed are called control structures

• 3 control structures are:


• Sequential control structures
• Selection (decision /branched) control structures
• Iteration /looping control structures

23ADT001 - C PROGRAMMING
Control structures
• Sequential control structures(where the information flows in straight
line)
• Selection (decision /branched) control structures(where decisions are
made according to some predefined condition)
• Iteration /looping control structures (where the logic (sequence of
steps ) is repeated in a loop until the desired output is obtained)

23ADT001 - C PROGRAMMING
Sequence control structure
Flow chart Pseudocode

Process 1 Process 1

Process 2 Process 2

Process n Process n

23ADT001 - C PROGRAMMING
Example
START

Read a,b

c=a+b

Print c

STOP

23ADT001 - C PROGRAMMING
Decision Making statements
• Decision making statements in a programming language helps the
programmer to transfer the control from one part to other part of
the program.
• If statement
• If..else statement
• If..else..if ladder
• Switch case

23ADT001 - C PROGRAMMING
If statement
• if statement is the most simple decision-making statement.
• It is used to decide whether a certain statement or block of
statements will be executed or not.
• if a certain condition is true then a block of statement is executed
otherwise not.

23ADT001 - C PROGRAMMING
Simple if
Example:

Syntax :
if(expression)
{ statement
};

23ADT001 - C PROGRAMMING
Pseudocode and Flow chart
Pseudocode Flow chart
IF (condition is true) THEN
process 1
YES If
. Condition
. True?
END IF
Process 1 NO
.
.

23ADT001 - C PROGRAMMING
Example
Start

Read a

yes
If a>0
no
Print a is Positive

Stop
23ADT001 - C PROGRAMMING
if-else statement
• The if statement alone tells us that if a condition is true it will
execute a block of statements and if the condition is false it won’t.
• We can use the else statement with if statement to execute a block
of code when the condition is false.

23ADT001 - C PROGRAMMING
if-else statement
• Syntax:

if (condition)
{
Statement block-1
}
else
{
Statement block-1
}
23ADT001 - C PROGRAMMING
if-else

Example:
Syntax :
if(expression)
{
statement1;
}
else
{
Statement2;
}

23ADT001 - C PROGRAMMING
Pseudocode and Flowchart

IF (condition is true) THEN


process 1
YES If NO
. Condition
. True?
ELSE
process 2 Process 1 Process 2
.
.
END IF
.
.
23ADT001 - C PROGRAMMING
Example
Start

Read a,b

yes
If a>b
no
Print a is Greater
Print b is Greater

Stop
23ADT001 - C PROGRAMMING
Example
#include <stdio.h>
int main()
{
int i = 20;
if (i < 15)
{
printf("i is smaller than 15");
}
else
{
printf("i is greater than 15");
}
return 0;
}

23ADT001 - C PROGRAMMING
Flowchart
Start

Read a

true false
If i<15

i is smaller than 15 i is larger than 15

Stop
23ADT001 - C PROGRAMMING
Nested if
• A nested if in C is an if statement that is the target of another if
statement.
• Nested if statements mean an if statement inside another if
statement.
• Yes, both C and C++ allow us to nested if statements within if
statements, i.e, we can place an if statement inside another if
statement.

23ADT001 - C PROGRAMMING
Nested if Example:
Syntax :
if(expression1)
{
if(expression2)
{
if(expression3)
{
statement
}}}

23ADT001 - C PROGRAMMING
Nested if else / If..else..if ladder
• Here, a user can decide among multiple options.
• The C if statements are executed from the top down.
• As soon as one of the conditions controlling the if is true, the
statement associated with that if is executed, and the rest of the C
else-if ladder is bypassed.
• If none of the conditions are true, then the final else statement will
be executed.

23ADT001 - C PROGRAMMING
Nested if else / If..else..if ladder
#include<stdio.h>
#include<conio.h> Example:
void main()
{
Syntax :
if(expression 1) {
int a;
statement-block1; printf("Enter a Number: ");
} scanf("%d",&a);
else if(expression 2) {
statement-block2;
if(a > 0)
} { printf("Given Number is Positive"); }
else if(expression 3) { else if(a == 0)
statement-block3;
}
{ printf("Given Number is Zero");
else }
statement-block 4; else if(a < 0)
{ printf("Given Number is Negative"); }
getch();
}
output:
Enter a Number: -5
Given Number is Negative
23ADT001 - C PROGRAMMING
Flowchart

23ADT001 - C PROGRAMMING
Switch-The switch statement is a multiway branching statement. It provides
an easy way to dispatch execution to different parts of code based on the value
of the expression.
Syntax : Example:
switch (n)
​{
case constant1:
// code to be executed if n is equal to
constant1;
break;

case constant2:
// code to be executed if n is equal to
constant2;
break;
.
.
.
default:
// code to be executed if n doesn't match any
constant
}

23ADT001 - C PROGRAMMING
Flowchart

23ADT001 - C PROGRAMMING
.
Pseudocode Flow chart

. .

CASE Type
Case Type-1:
Process 1
Case Type-2: ye
Type 1 s Process 1
Process 2
no
.
ye
. Type 2 s Process 2
Case Type-n:
no
Process n
yes
. Type 3 Process 3
.
END CASE n
o

23ADT001 - C PROGRAMMING
23ADT001 - C PROGRAMMING
Break statement in Switch Case

Void main()
Output:
{
Case 1
int i=1; Case 2

switch(i) {
case 1: printf("Case1 ");
case 2: printf("Case2 "); break
case 3: printf("Case3 ");
case 4: printf("Case4 ");
default: printf("Default "); }
}
23ADT001 - C PROGRAMMING
Break statement in Switch Case

Void main()
Output:
{
Case2
int i=2; Case3
Case4
switch(i) { Default
case 1: printf("Case1 ");
case 2: printf("Case2 ");
case 3: printf("Case3 ");
case 4: printf("Case4 ");
default: printf("Default "); }
}
23ADT001 - C PROGRAMMING
#include <stdio.h>
Supports character
int main () { and int
/* local variable definition */ Does not support float
char grade = 'B'; values
switch(grade) {
case 'A' : printf("Excellent!\n" ); break;
case 'B' :
case 'C' : printf("Well done\n" ); break;
case 'D' : printf("You passed\n" ); break;
case 'F' : printf("Better try again\n" ); break;
default : printf("Invalid grade\n" );
}
printf("Your grade is %c\n", grade ); return 0; }
• When the above code is compiled and executed, it produces the
following result −
• Well done

23ADT001 - C PROGRAMMING
The default block can be placed anywhere.
The position of default doesn’t matter, it is still executed if no match found.

// The default block is placed above other


cases.
#include <stdio.h>
int main()
{
int x = 4;
switch (x)
{
default: printf("Choice other than 1 and
2");
break;
case 1: printf("Choice is 1");
break;
case 2: printf("Choice is 2");
break;
}
return 0;
}

23ADT001 - C PROGRAMMING
continue statement
• The continue statement is used inside loops.
• When a continue statement is encountered inside a loop, control
jumps to the beginning of the loop for next iteration, skipping the
execution of statements inside the body of loop for the current
iteration.
Syntax:
continue;

23ADT001 - C PROGRAMMING
Example
#include <stdio.h>
int main()
{
for (int j=0; j<=8; j++)
{
if (j==4)
{
continue;
}
printf("%d ", j);
}
return 0;
}

23ADT001 - C PROGRAMMING
Continue statement

23ADT001 - C PROGRAMMING
• Decision Making
statements

23ADT001 - C PROGRAMMING
References
• Ashok N.Kamthane, Amit.N.Kamthane, “Programming in C”, 3rd
Edition, Pearson Education, 2015
• Ajay Mittal, “Programming in C-A Practical Approach”, 3rd Edition,
Pearson Education, 2010.
• Yashavant P.Kanetkar, “Let Us C”, 16th Edition, BPB Publications,
2018.
• PradipDey, ManasGhosh, “Computer Fundamentals and
Programming in C”, 2nd Edition, Oxford University Press, 2013.

23ADT001 - C PROGRAMMING
23ADT001 - C PROGRAMMING

You might also like