0% found this document useful (0 votes)
8 views16 pages

C Programming Decision Making Statements

Uploaded by

prokiller9585
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)
8 views16 pages

C Programming Decision Making Statements

Uploaded by

prokiller9585
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

UNIT 2

LESSON 1-Decision Making Statements


Decision Making Statements-Introduction
Decision making structures require that the programmer specify one or more conditions to be evaluated or
tested by the program, along with a statement or statements to be executed if the condition is determined
to be true, and optionally, other statements to be executed if the condition is determined to be false.

The general from of a typical decision making


structure
C programming language assumes any non-zero and non-null values as true and if it is
either zero or null then it is assumed as false value.
C programming language provides following types of decision making statements.

Statement Description
An if statement consists of a Boolean expression followed by one or more
if statement
statements.

An if statement can be followed by an optional else statement, which executes


if...else statement
when the boolean expression is false.

nested if statements You can use one if or else if statement inside another if or else if statement(s).

switch statement A switch statement allows a variable to be tested for equality against a list of values.
if Statement
if statement is a one-way branching in which the statements will only execute if the given expression is [Link] if statement
consists of a Boolean expression followed by one or more statements.
Syntax:
if(boolean_expression)
{
/* statement(s) will execute if the boolean expression is true */
}

If the boolean expression evaluates to true then the block of code inside the if statement will be executed. If boolean
expression evaluates to false then the first set of code after the end of the if statement (after the closing curly brace) will be
executed.
Flow Diagram:

#include <stdio.h>
void main ()
{
int a=11,b=50;
if( a > b )
{
printf("%d is bigger\n",a);
}
printf(%d is bigger\n”,b);
}
Output:
11 is bigger
if-else Statement
The if-else statement is used if we want to execute some code if the condition is true and another code if the condition is
[Link] if statement can be followed by an optional else statement, which executes when the boolean expression is
false.
Syntax:
if(boolean_expression)
{
/* statement(s) will execute if the boolean expression is true */
}
else
{
/* statement(s) will execute if the boolean expression is false */
}
If the boolean expression evaluates to true then the if block of code will be executed otherwise else block of code will
be executed.
Flow Diagram:
#include <stdio.h>
void main ()
{
int a = 100,b=20;
if( a > b )
{
printf("%d is greater than %d\n",a,b );
}
else
{
printf("%d is lesser than %d\n",a,b );
}
printf("Outside if-else\n");
}
Output:
100 is greater than 20
Outside if-else
Note:
The conditional operator ? : discussed in operators can be used to replace if...else statements. and it has the following
general form:
Exp1 ? Exp2 : Exp3;
where Exp1, Exp2, and Exp3 are expressions. The value of a ? expression is determined like this: Exp1 is evaluated. If it
is true, then Exp2 is evaluated and becomes the value of the entire ? expression. If Exp1 is false, then Exp3 is evaluated
and its value becomes the value of the expression.

Nested if statement
A nested if statement is an if statement placed inside another if statement. Nested if statements are often
used when you must test a combination of conditions before deciding on the proper action. ie.)You can use
one if or else if statement inside another if or else if statement(s).
Syntax:
if ( test condition 1)
{

//If the test condition 1 is TRUE then these it will check for test condition 2
if ( test condition 2)
{
//If the test condition 2 is TRUE then these statements will be executed
Test condition 2 True statements;
}

else
{
//If the c test condition 2 is FALSE then these statements will be executed
Test condition 2 False statements;
}

else
{
//If the test condition 1 is FALSE then these statements will be executed
Test condition 1 False statements;
}
An else statement is matched to the closest previous if statement that does not already have its own else statement.
Proper indenting makes it easier to see the matching. Just remember that in an if or else statement, if the expression is true,
the program executes the following curly bracketed code block or single statement.

Flow Diagram:
#include <stdio.h>
void main()
{
int n1, n2, n3;
This code produces following result:
printf("Enter three numbers: ");
Enter three numbers:
scanf("%d %d %d", &n1, &n2, &n3);
40
50
if (n1 >= n2)
30
{
50 is the largest number.
if (n1 >= n3)
printf("%d is the largest number.", n1);
else
printf("%d is the largest number.", n3);
}
else
{
if (n2 >= n3)
printf("%d is the largest number.", n2);
else
printf("%d is the largest number.", n3);
}

}
else-if ladder
if-else ladder is an extension of if…else statement. By using one if-else construct, it is possible to choose between two
choices. There may be a situation wherein it is required to select one among several choices. This can be accomplished by
another if-else in the else part of the construct. This type of if construct is called as else if ladder statement.

Syntax:
if(condition 1)
statement 1;
else if(condition 2)
statement 2;
"
"
"
else if(condition n)
statement n;
else default statement;

If some condition is true then execute some task; otherwise if some other condition is true, then execute some different
task; if none conditions are true then execute some default task.
Flow Diagram:
#include<stdio.h>
void main
{
int marks;
printf(“Enter marks\n”); Output:
scanf(“%d”,&marks) Enter marks
if ( marks < 35 ) 45
printf("Fair); Pass class
else if( marks < 50 )
printf("'Pass class");
else if( marks < 60 )
printf("Second class");
else if( marks < 70 )
printf("First class")
else
printf ("Distinction");
getch();
}
switch Statement
A switch statement allows a variable to be tested against several values for equality. Each of these values is called a case.
Once a matching case is found, its particular block of code is executed. It is an alternative to the more common if-
else statement.

Syntax:
switch(expression)
{
case constant-expression :
statement(s);
break;
case constant-expression :
statement(s);
break;

/* any number of case statements can be included*/


default:
statement(s);
}

You might also like