0% found this document useful (0 votes)
33 views22 pages

Unit - 1 CP (Lecture 9)

Uploaded by

Neeraj Kumar
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)
33 views22 pages

Unit - 1 CP (Lecture 9)

Uploaded by

Neeraj Kumar
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/ 22

University Institute Of Computing

Bachelor of Computer Applications


Subject Name: Computer Programming
Code:20CAT111

1
Topics to be Covered
• if statement
• if-else statement
• else-if statement
• Nested if else statement
• switch case statement

2
Control Structure

Fig. 9.1 - Decision control statements [1]


29/10/2024 3
Decision Control Structure
• Decision control statements alter the normal sequential execution of the
statements of the program depending upon the test condition to be carried out at
a particular point in program.
• Decision control statements supported by c are:-
• if statement
• if-else statement
• else-if statement
• Nested if else statement
• switch case statement

29/10/2024 4
if Statement

• It is used to control the flow of execution of the statements and also to test logically
whether the condition is true or false.
• It executes a statement or block of statements only if the condition is true.
Syntax: if (condition) if (condition)
{ OR {
statement (s); statement 1;
} statement 2;
Next statement; }
statement 3;
In above syntax :
if condition is true only then the statements within the block are executed otherwise
next statement in sequence is executed.
29/10/2024 5
if Statement
• If the condition is true then the statement following the “if” is
executed if it is false then the statement is skipped.

29/10/2024 Fig. 9.2- Flow chart If Statement [2] 6


Example if Statement

#include <stdio.h>
int main()
{
int x = 20;
int y = 22;
if (x<y)
{
printf("Variable x is less than y");
}
return 0;

29/10/2024 7
if-else Statement
• If specific statements are to be executed in both cases (either condition is true or
false) then if – else statement is used.
• In if – else statement a block of statement is executed if the condition is true but a
different block of statements is executed when the condition is false.
• Syntax:
if (condition)
{
statement 1;
statement 2;
}
else
{
statement 3;
29/10/2024
} 8
if-else statement
• In this case if condition is true, a set of statements present in statement block-1 will be executed.
Otherwise, a set of statements present in statement block-2 will be executed.

Fig. 9.3 – if-else statement flow chart[3]


29/10/2024 9
Example if-else Statement
#include <stdio.h>
int main() {
int number;
printf("Enter an integer: ");
scanf("%d", &number);

// True if the remainder is 0


if (number%2 == 0) {
printf("%d is an even integer.",number);
}
else {
printf("%d is an odd integer.",number);
}

return 0;
}
29/10/2024 10
else-if ladder

• In a program involving multiple conditions, the nested if else statements makes


the program very difficult to write and understand if nested more deeply.
• For this ,we use if-else-if ladder.
• Syntax: if (condition1)
statement1;
else if(condition2)
statement2;
else if(condition3)
statement 3;
else
default statement;
else-if ladder

false
condition 1
false
condition 2
true
false
Statement 1 true condition 3

Statement 2 true

Statement 3

Default statement

Next statement
Fig 9.4: else-if ladder flow-chart
29/10/2024 12
Example else-if ladder
#include <stdio.h>
int main() {
int number1, number2;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
if(number1 == number2) {
printf("Result: %d = %d",number1,number2);
}
else if (number1 > number2) {
printf("Result: %d > %d", number1, number2);
}
else {
printf("Result: %d < %d",number1, number2);
}
return 0;
}
Nested if-else Statement

• When an if else statement is present inside the body of another “if” or “else” then
this is called nested if else.
• Syntax of Nested if else statement:
if(condition) {
if(condition2) {
//Statements inside the body of nested "if“ }
else {
//Statements inside the body of nested "else“ }
}
else {
//Statements inside the body of "else"
}
Nested if-else Statement

Fig.9.5 Nested if-else Statement[4]


Example Nested if-else Statement
#include <stdio.h>
int main() {
int number1, number2;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
if (number1 >= number2) {
if (number1 == number2) {
printf("Result: %d = %d",number1,number2);
}
else {
printf("Result: %d > %d", number1, number2);} }
else {
printf("Result: %d < %d",number1, number2);
}
return 0;
}
switch Statement
• Switch is a multi-way decision making statement which selects one of the several
alternatives based on the value of single variable or expression.
• It is mainly used to replace multiple if-else-if statement.
• Syntax: switch (expression)
{
case constant1 : statement (s); [break;]
case constant2 : statement (s); [break;]
…………………………………….
default: statement (s)
}

29/10/2024 17
switch Statement

Fig.9.6 – switch case statement flow chart[5]


Example switch Statement
#include <stdio.h>
int main()
{
int num=2;
switch(num+2)
{
case 1:
printf("Case1: Value is: %d", num);
case 2:
printf("Case1: Value is: %d", num);
case 3:
printf("Case1: Value is: %d", num);
default:
printf("Default: Value is: %d", num);
}
return 0;
}

29/10/2024 19
Reference Image Link
[1] https://newsandstory.com/tempImage/26122613503020184710.gif
[2] https://beginnersbook.com/wp-content/uploads/2017/09/if_statement _flow_
diagram_C.jpg
[3] https://
beginnersbook.com/wp-content/uploads/2017/09/If_else_flowdiagram_ C.jpg
[4] https://
secureservercdn.net/160.153.138.219/b79.d22.myftpupload.com/wp-content/uploads/20
17/08/nested-if-else-flowchart.png
[5] https://www.guru99.com/images/1/020819_0506_SwitchCaseS1.png

20
Reference Books
• T1 E. Balaguruswamy, Programming in ANSI, McGraw-Hill Education,
• https://bookmart.online/shop/programming-in-ansi-c-8-e-balagurusamy-second-hand-book/?ut
m_source=Google%20Shopping&utm_campaign=test&utm_medium=cpc&utm_term=498

• R1 Byron S. Gottfried, Programming with C, McGraw-Hill (2nd Edition)


• https://www.amazon.com/Schaums-Outline-Programming-Byron-Gottfried/dp/0070240353/ref
=sr_1_1?dchild=1&keywords=%E2%80%A2+Programming+with+C%2C+Gottfried%2C+a
nd+McGraw-Hill.&qid=1593001561&s=books&sr=1-1

• R2 Brian W. Kernighan, Dennis M. Ritchie, The ANSI C Programming Language, Prentice-


Hall 2nd Edition.
• http://www2.cs.uregina.ca/~hilder/cs833/Other%20Reference%20Materials/The%20C
%20Programming%20Language.pdf

21
THANK YOU
For queries
Email:[email protected]

You might also like