Unit - 1 CP (Lecture 9)
Unit - 1 CP (Lecture 9)
1
Topics to be Covered
• if statement
• if-else statement
• else-if statement
• Nested if else statement
• switch case statement
2
Control Structure
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.
#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.
return 0;
}
29/10/2024 10
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
29/10/2024 17
switch Statement
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
21
THANK YOU
For queries
Email:[email protected]