0% found this document useful (0 votes)
3 views6 pages

Xii Computer Conditions

The document provides an overview of decision-making statements in the C programming language, including types such as if statements, if-else statements, conditional operators, nested if statements, if-else ladders, and switch statements. Each type is explained with its syntax and examples, highlighting how they are used to control the flow of a program based on conditions. Additionally, it compares if-else ladders and switch statements, noting their differences in usage and performance.

Uploaded by

Free Fire Gamers
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

Xii Computer Conditions

The document provides an overview of decision-making statements in the C programming language, including types such as if statements, if-else statements, conditional operators, nested if statements, if-else ladders, and switch statements. Each type is explained with its syntax and examples, highlighting how they are used to control the flow of a program based on conditions. Additionally, it compares if-else ladders and switch statements, noting their differences in usage and performance.

Uploaded by

Free Fire Gamers
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Decision Making in C language

Decision Making in C Language.


The decision making statements in C language are used to perform operations on the basis of
condition and when program encounters the situation to choose a particular statement among
many statements.
Types of Decision Making statements in C
Control
Statements Description.

If statements If statement is used to execute the code if condition is true

If else If else statement is used to execute a code when condition is true otherwise it
statements execute another code in else section

Conditional Conditional Operator in C can be used to implement if-then-else type of logic


Operator ? :

Nested if We can use if statement inside another if statement, if we want to execute a


statements code when multiple conditions become true

If else if If else ladder is used, If we want to check for multiple conditions one after
ladder another

Switch Switch statement allows us to perform equality test of value of a variable


statement against a list of possible values.

If statement in C Programming.
The if statement in C programming language is used to execute a block of code only if condition
is true. The if statement checks whether conditional expression is true or false. If the conditional
expression is true, then the block of code inside the if statement is executed but if conditional
expression is false, then the block of code inside the if statement is ignored and the next statement
after if block is executed.
Syntax of If Statement
if(conditional expression)
{
/* code to be execute if the conditional expression is true */
statements;
}
Note: Opening and Closing Braces are optional, if the block of code of if statement contains
only one statement and no statement terminator ; at the end of if(condition)

1|Page DECISION MAKING IN C LANGUAGE


Example.
if( percentage >=33)
{
printf(“ Congratulations You are Pass”);
}

If else statement in C Programming.


The if else statement in C programming language is used to execute a set of statements if condition
is true and execute another set of statements when condition is false. Only either if block or else
block of code gets executed (not both) depending on the outcome of condition.
Syntax of if else statement
if(conditional expression)
{
/* code to be execute if the conditional expression is true */
statements;
}
else
{
/* code to be execute if the conditional expression is false */
statements;
}
Example
if( percentage >=33)
printf(“ Congratulations You are Pass”);
else
printf(“ Sorry You are Fail”);

2|Page DECISION MAKING IN C LANGUAGE


Conditional Operator in C.
Conditional Operator in C is a powerful Operator which can be used to implement if-then-else
type of logic. This operator is also known as ternary operator as it takes three expressions in
following form.
Conditional Expression ? statement if condition is TRUE : statement if condition is FALSE;
Example.
(percentage>=33) ? printf(“Pass”) : printf(" Fail");
Nested If Statement in C Programming
If statement within another if statement is called nested if statement. The nested if statement in
C programming language is used when multiple conditions need to be tested. The inner statement
will execute only when outer if statement is true otherwise control won't even reach inner if
statement.
Syntax of Nested If Statement
if(conditional expression one) // outer if
{
/* Code to be executed when conditional expression one is true */
statement1;
if(conditional expression two) // inner if
{
/* Code to be executed when conditional expression two and
conditional expression two both are true */
statement2;
}
}
Example
if(number>0)
{
printf(“ Number is greater than zero”);
if (number<100)
printf(“ Number is greater than zero and number is less than 100”);
}

3|Page DECISION MAKING IN C LANGUAGE


If else Ladder Statement in C Programming
The if else ladder statement in C programming language is used to test set of conditions in
sequence. An if condition is tested only when all previous if conditions in if-else ladder are false. If
any of the conditional expression evaluates to true, then it will execute the corresponding code
block and exits whole if-else ladder.
if (conditional expression one)
{
// statement(s)
}
else if(conditional expression two)
{
// statement(s)
}
else if (conditional expression three)
{
// statement(s)
}
.
.
else
{
// statement(s)
}

Switch statement in C Language.


A switch statement allows a variable or value of an expression to be tested for equality against a
list of possible case values and when match is found, the block of code associated with that case
is executed. Its alternative of if else ladder but it is only used for equality and with integer data
type.
Syntax of Switch Statement
switch(integer expression)
{
case constant1 :
/* Code to be executed when value of expression equals constant1 */
statement;
break;
case constant2 :
/* Code to be executed when value of expression equals constant2 */
statement;
break;
case constant3 :
/* Code to be executed when value of expression equals constant3 */
4|Page DECISION MAKING IN C LANGUAGE
statement;
break;
default : /* Optional */
statement;
}
The break statement is optional. The break statement at the end of each case cause switch
statement to exit. If break statement is not used, all statements below that case statement are also
executed until it found a break statement. The default code block gets executed when none of
the case matches with expression. default case is optional and doesn't require a break statement.

Q. what is difference between if else ladder and switch statement in C?


If-else ladder Switch Statement
Used with all data types eg. int, float, Used only with int and char data types
double etc
Less than <, greater than > and other Used only for equality
relational operator s can also be used.
if-else ladder works slower than its Switch works faster than an equivalent if-else ladder.
equivalent switch statement.
Variables can be used in if-else conditional Cases can never have variable expressions (for
expressions. E.g. if(a< c+2) example it is wrong to say case a +3 : )

Q. Write a program that uses switch statement


5|Page DECISION MAKING IN C LANGUAGE
include<stdio.h>
int main(void)
{
int num;
printf("\n Enter number from 1 to 5:");
scanf("%d",&num);
switch(num)
{
case 1:printf("\n ONE");
break;
case 2:printf("\n TWO");
break;
case 3:printf("\n THREE");
break;
case 4:printf("\n FOUR");
break;
case 5:printf("\n FIVE");
break;
default:
printf("\n Number is less than 1 or number is greater than 5");
}
return(0);
}
Q. Write a program that finds out the greatest number among three inputted numbers.
#include<stdio.h>
int main (void)
{
int a, b, c;
printf ("\nEnter first number:");
scanf ("%d",&a);
printf ("\nEnter second number:");
scanf ("%d",&b);
printf ("\nEnter third number:");
scanf ("%d",&c);
if (a>b&&a>c)
printf (“\nGreatest numberis:%d”,a);
else if (b>a &&b>c)
printf (“\nGreatest numberis:%d”,b);
else
printf (“\nGreatest numberis:%d”,c);
return(0);
}

6|Page DECISION MAKING IN C LANGUAGE

You might also like