0% found this document useful (0 votes)
11 views

Lecture 6 - Program Control Structures - Decisions

The document discusses different decision structures in C programming including if, if-else, if-else-if ladder, switch statements. It provides syntax, flowcharts and examples of each structure. Key topics covered include using conditions to determine program flow, comparing multiple conditions, and executing different code blocks based on variable values.

Uploaded by

princetrojan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Lecture 6 - Program Control Structures - Decisions

The document discusses different decision structures in C programming including if, if-else, if-else-if ladder, switch statements. It provides syntax, flowcharts and examples of each structure. Key topics covered include using conditions to determine program flow, comparing multiple conditions, and executing different code blocks based on variable values.

Uploaded by

princetrojan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

DIT 004 – Introduction to Programming

Methodologies

Lecture 6 – Decision Structures


C if else Statement
2

 The if-else statement in C is used to perform the operations based on some


specific condition. The operations specified in if block are executed if and
only if the given condition is true.
 There are the following variants of if statement in C language.
 If statement
 If-else statement
 If else-if ladder
 Nested if
If Statement
3

 The if statement is used to check some given condition and perform some
operations depending upon the correctness of that condition.
 It is mostly used in the scenario where we need to perform the different
operations for the different conditions.
 The syntax of the if statement is given below.
if(expression)
{
//code to be executed
}
Flowchart of if statement in C
4

#include<stdio.h>
int main()
{
int number=0;
printf("Enter a number:");
scanf("%d",&number);
if(number%2==0)
{
printf("%d is even number",number);
}
return 0;
}
Program to find the largest number of the
three.
5

#include <stdio.h> if(b>a && b > c)


int main() {
{ printf("%d is largest",b);
int a, b, c; }
printf("Enter three numbers?"); if(c>a && c>b)
scanf("%d %d %d",&a,&b,&c); {
if(a>b && a>c) printf("%d is largest",c);
{ }
printf("%d is largest",a); if(a == b && a == c)
} {
printf("All are equal");
}
}
If-else Statement
6

 The if-else statement is used to perform two operations for a single


condition.
 The if-else statement is an extension to the if statement using which, we can
perform two different operations, i.e., one is for the correctness of that
condition, and the other is for the incorrectness of the condition.
 Syntax
if(expression)
{
//code to be executed if condition is true
}
Else
{
//code to be executed if condition is false
}
Flowchart of the if-else statement in C
7

#include<stdio.h>
int main(){
int number=0;
printf("enter a number:");
scanf("%d",&number);
if(number%2==0){
printf("%d is even number",number);

}
else{
printf("%d is odd number",number);
}
return 0;
}
Program to check whether a person is
eligible to vote or not.
8

#include <stdio.h>
int main()
{
int age;
printf("Enter your age?");
scanf("%d",&age);
if(age>=18)
{
printf("You are eligible to vote...");
}
else
{
printf("Sorry ... you can't vote");
}
}
If else-if ladder Statement
9

 The if-else-if ladder statement is an extension to the if-else statement. It is


used in the scenario where there are multiple cases to be performed for
different conditions.
 In if-else-if ladder statement, if a condition is true then the statements
defined in the if block will be executed, otherwise if some other condition is
true then the statements defined in the else-if block will be executed, at the
last if none of the condition is true then the statements defined in the else
block will be executed.
 There are multiple else-if blocks possible.
10

Syntax
if(condition1)
{
//code to be executed if condition1 is true
}else if(condition2)
{
//code to be executed if condition2 is true
}
else if(condition3)
{
//code to be executed if condition3 is true
}
...
Else
{
//code to be executed if all the conditions are false
}
Flowchart of else-if ladder statement in C
11
12

#include<stdio.h> else if(number==100)


int main(){ {

int number=0; printf("number is equal to 100");


}
printf("enter a number:");
Else
scanf("%d",&number);
{
if(number==10)
printf("number is not equal to 10, 50 or 100");
{
printf("number is equals to 10"); }
} return 0;
else if(number==50) }

{
printf("number is equal to 50");
}
Program to calculate the grade of the
student according to the specified marks.
13

#include <stdio.h> else if (marks > 40 && marks <= 60)


int main() {
{ printf("You scored grade B ...");
int marks; }
printf("Enter your marks?"); else if (marks > 30 && marks <= 40)
scanf("%d",&marks); {
if(marks > 85 && marks <= 100) printf("You scored grade C ...");
{ }
printf("Congrats ! you scored grade A ..."); else
} {
else if (marks > 60 && marks <= 85) printf("Sorry you are fail ...");
{ }
printf("You scored grade B + ..."); }
}
C Switch Statement
14

 The switch statement in C is an alternate to if-else-if ladder statement which


allows us to execute multiple operations for the different possible values of
a single variable called switch variable.
 We can define various statements in the multiple cases for the different
values of a single variable.
Syntax
switch(expression)
{
case value1:
//code to be executed;
break; //optional
case value2:
//code to be executed;
break; //optional
......
default:
code to be executed if all cases are not matched;
}
Rules for switch statement in C language
15

1) The switch expression must be of an integer or character type.


2) The case value must be an integer or character constant.
3) The case value can be used only inside the switch statement.
4) The break statement in switch case is not must. It is optional.

 If there is no break statement found in the case, all the cases will be
executed present after the matched case.
 It is known as fall through the state of C switch statement.
16
17

#include<stdio.h> case 50:


int main() printf("number is equal to 50");
{ break;
int number=0; case 100:
printf("enter a number:"); printf("number is equal to 100");
scanf("%d",&number); break;
switch(number) default:
{ printf("number is not equal to 10, 50 or 100");
case 10: }

printf("number is equals to 10"); return 0;

break; }
Difference between if-else & switch

18

You might also like