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

Decision Control Structure

The document discusses various decision control structures in C language including if statement, if-else statement, nested if statements, else-if ladder, and switch statement. It provides syntax and examples of simple if statement, if-else statement, and else-if ladder. The if statement executes code if a condition is true, if-else adds else block to execute if condition is false, and else-if ladder allows checking multiple conditions in single statement.

Uploaded by

apexg786
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)
20 views

Decision Control Structure

The document discusses various decision control structures in C language including if statement, if-else statement, nested if statements, else-if ladder, and switch statement. It provides syntax and examples of simple if statement, if-else statement, and else-if ladder. The if statement executes code if a condition is true, if-else adds else block to execute if condition is false, and else-if ladder allows checking multiple conditions in single statement.

Uploaded by

apexg786
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/ 16

Computer Programming:

C language
Subject Teacher:
Shamshad Lakho
[email protected]
The Decision Control Structure
• Decision making statements are used to skip or to execute a group of
statements based on the result of some condition.
• The decision making statements are,
− simple if statement
− if…else statement
− nested if
− else … if ladder
− switch statement
− goto
• These statements are also called branching statements
IF Statement
• if statement executes a single statement or a block of statements if a
boolean expression evaluates to true.
Syntax:

If(condition)
{
Statements;
}

Here condition is a boolean expression


If condition is true, then the statement is executed
If condition is false, than the statement is bypassed
Simple if statement
Syntax:
if(condition)
{
Statements;
}

False
if(condition)

True (Bypass)

Statements;
Simple if - Example
#include<stdio.h> Output1
#include<conio.h>
int main() Enter any two numbers 10 5
{
Num1 is greater than Num2
int num1,num2;
printf("Enter any two numbers "); End of program
scanf("%d %d",&num1,&num2);
if(num1>num2)
printf("\nNum1 is greater than Num2"); Output1
printf("\nEnd of program"); Enter any two numbers 10 50
getch();
End of program
}
IF Else Statement
• An if statement can include an else clause that executes a statement or block
if the boolean expression is not true.
Syntax:

If (condition)
Statement1;
Else
Statement2;

• If condition is true, then statement1 is executed.


• Otherwise, statement2 is executed.
if - else statement
Syntax:

if(condi False
tion)
if(condition)
{ True
True block statements;
} True Block False Block
Statement Statements
else
{
False block statements;
}
if – else Example
# include <stdio.h>
Output
void main ()
{
Type a number 50
Theint num; is positive
number
printf ("Type a number:");
scanf ("%d", &num);
if (number < 0)
printf(“The number is negative”);
else
printf(“The number is positive”);
}
if – else Example
#include<stdio.h>
Output
void main()
{ Enter a number 125
Int num; The number is ODD
printf ("Enter a number:");
scanf ("%d",&num);
if (num%2==0)
printf ("The number is EVEN.\
n");
else
printf ("The number is ODD.\
n");
}
if – else Example
#include<stdio.h>
#include<conio.h>
void main (void)
{
int percentage;
printf(“Enter your percentage: “);
scanf(“%d”, &percentage);
if(percentage>=60)
printf(“You are Passed”);
else
printf(“You are failed”);
getch();
}
Else - if Ladder Statement
• An if statement can be followed by an optional else
if...else statement, which is very useful to test various
conditions using single if...else if statement.

When using if , else if , else statements there are few points to


keep in mind:

• An if can have zero or one else's and it must come after any
else if's.
• An if can have zero to many else if's and they must come
before the else.
• Once an else if succeeds, none of the remaining else if's or
else's will be tested.
Else - if Ladder Statement
Syntax
if (condition1)
statement block 1;
else if (condition2)
statement block 2;
else if (condition3)
statement block 3;
:
:
else if (condition)
statement block n;
else
Else - if Ladder Statement
True
If(condition1)
False
True
Else if(condition2)
False Statements1;
True
Else if(condition3)
False Statements2;

Default Statements;
Statements3;
Else - if Ladder Example
#include<stdio.h>
#include<conio.h>

void main(void)
{

int num1,num2;
clrscr();
printf(“Enter integer numbers to check: ”);
scanf(“%d %d”,&num1,&num2);
if(num1>num2)
printf(“num1 is greater than num2”);
else if(num1<num2)
printf(“num1 is less than num2”);
else
printf(“both numbers are equal”);
getch();

}
Else - if Ladder Example
#include <stdio.h>
voidOutput
main ()
{
float perc;
Enter Percentage: 75
printf ("Enter Percentage:");
A Grade
scanf ("%f", &perc);
if (perc <= 100 && perc >= 80)
printf ("\n A-1 Grade");
else if (perc >= 70)
printf("\n A Grade");
else if (perc >= 60)
printf ("\n B Grade");
else
printf ("Fail");
}
Else - if Ladder Example
#include<stdio.h>
#include<conio.h>
int main()
{
float num1, num2; char op;
printf("Enter two numbers");
scanf("%f %f",&num1,&num2);
printf("\nEnter operator (+,-,*,/)");
op=getche();
if(op =='+')
printf("\nResult=%f",num1+num2);
else if(op == '-')
printf("\nResult=%f",num1-num2);
else if(op == '*')
printf("\nResult=%f",num1*num2);
else if(op == '/')
printf("\nResult=%f",num1/num2);
else
printf("Invalid Operator");
getch(); }

You might also like