Introduction To Decision Making:-
Decision-making structures require that the programmer specifies one or moreconditions 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.
*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 the following types of decision-making
*if statement :-
*if...else statement
*nested if statements
*switch statment
*nested switch statements
Now We Will See About These Statement One by One:-
if Statement:-
An if statement consists of a Boolean expression followed by one or more
statements.
Syntax Of If Statements:-
if(Check_Condition)
{
/* statement(s) will execute if the condition is true */{
}
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh27LF5gpXWNpd8BUvJgk-bFj2tQfDLySoLOb5UtvjQsD8SXxgMQ8ReFgsIxyBa8UUugmyu72uGeLhFkWTnZtxSP9IrDgs4fW8QisGA1jR4F9eiJIBR83fT0H5mz8bOM0E4oHBFhc97Mps/s320/13.jpg)
#include <stdio.h>
int main ()
{
/* local variable definition */
int a = 19;
/* check the boolean condition using if statement */
if( a < 29 )
{
/* if condition is true then print the following */
printf("a is less than 29\n" );
}
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgT1Qu6tvZMnvFyWM4Dx-t3FdKu47IqRtbLynFsWe4ukFn6YBfx6f3kKhmDTmo2db97XD-izyIW-Lh2CMLGXaJUU2OWDc1qq0laFRwzNBBvCeJBfQhdda3__QVLtlHSWtQFGqps_hlA_eI/s320/18.jpg)
return 0;
}
When the above code is compiled and executed,
it produces the following output:-
a is less than=29
value of a is =19
if..else Statement:-
An if statement can be followed by an optional else statement, which executeswhen the Boolean expression is false.
General Syntax For This Statement:-
if(Check_Condition)
{
/* statement(s) will execute if the Check_Condition is true */
}
else
{
/* statement(s) will execute if the Check_Condition is false */
}
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhKbmJ_4C-8hQUofp8xSax2wUQsuBjVPzaQZnpnLPCzzLfdMc9biRYmY6LZX5pQvdV_f62AyLzYJiyE_9Yn9eZl82GXp66WimWNTMa8t4aqJFxLT1CHIcZoVKGDr879ifDznGl-rH48u1A/s320/12.jpg)
#include <stdio.h>
int main ()
{
/* local variable definition */
float a = 101.88;
/* check the boolean condition */
if( a < 89 )
{
/* if condition is true then print the following */
printf("a is less than 89\n" );
}
else
{ Output Screen(Kali Linux)
/* if condition is false then print the following */
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj17bNsaGYB_daLpUA_nEPQE7LOVbR6m6XINGCprmQA2ccxGgBdNSQg9NmHnjqPGT6gjV6h0oGEiRJoQMkDeLRWxXnFxNKG3cwDuN7Q9h71t-LV1uJKO3SJhNcU-tU5YxnWIkPKEGrixdA/s320/17.jpg)
}
printf("value of a is = %d\n", a);
return 0;
}
When the above code is compiled and executed,
it produces the following output:
a is not less than 89
value of a is = 89
if..else if...else 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.
Some Point To Keep Mind Whwn Using if..else if..else Condition:-
*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.
General Syntax:-
if(Check_Condition 1)
{
/* Executes when the Check_Condition 1 is true */
}
else if( Check_Condition 2)
{
/* Executes when the Check_Condition 2 is true */
}
else if( Check_Condition 3)
{
/* Executes when the Check_Condition 3 is true */
}
else
{
/* executes when the none of the above condition is true */
}
Example Program:-
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiWUu2WVxja4F4C6MbAQHKXDmp7ZkqAurpMD2Atc1z3ZwTzTp4XkDfVKTt4sNZ4dZ_2NRiOJ854S4SqA6dSiJVKwFS0v_op2an22xa0flK1Xi1y8qsCDb-oIHmL5DjpeGNvel8PVGpIk6U/s320/11.jpg)
int main ()
{
/* local variable definition */
float a = 10.3;
/* check the boolean condition */
if( a == 5 )
{
/* if condition is true then print the following */
printf("Value of a is 5\n" );
}
else if( a == 6 )
{
/* if else if condition is true */
printf("Value of a is 6\n" );
}
else if( a == 7 )
Output Screen(Kali Linux)
{
/* if else if condition is true
*/
printf("Value of a is 7\n" );
}
else
{
/* if none of the conditions is true */
printf("None of the values is matching\n" );
}
printf("Exact value of a is= %f\n", a );
return 0;
}
When the above code is compiled and executed, it produces the following output:
None of the values is matchingExact value of a is= 10.3
Nested if Statements:-
It is always legal in C programming to nest if-else statements, which means you
can use one if or else if statement inside another if or else if statement(s).
Syntax Of Nested if Statement:-
if( Check_Condition 1)
{
/* Executes when the Check_Condition 1 is true */
if( Check_Condition 2)
{
/* Executes when the Check_Condition 2 is true */
}
}
Switch Statement:-
*A switch statement allows a variable to be tested for equality against a list of
values. Each value is called a case, and the variable being switched on is
checked for each switch case.
Genral Syntax :-
switch(expression){case constant-expression
:
statement(s);
break; /* optional */
case constant-expression
:
statement(s);
break; /* optional */
/* you can have any number of case statements */
default : /* Optional */
statement(s);
}
We Have To Apply Following Rules
1)The expression used in a switch statement must have an integral or
enumerated type, or be of a class type in which the class has a single
conversation function to an integral or enumrated type
2)You can have any number of case statements within a switch. Each case is
followed by the value to be compared to and a colon.
3)The constant-expression for a case must be the same data type as the
variable in the switch, and it must be a constant or a literal.
4)When the variable being switched on is equal to a case, the statements
following that case will execute until a break statement is reached.
5)When a break statement is reached, the switch terminates, and the flow
of control jumps to the next line following the switch statement.
6)Not every case needs to contain a break
7)A switch statement can have an optional default case
Nested switch Statements:-
*It is possible to have a switch as a part of the statement sequence of an outer
switch. Even if the case constants of the inner and outer switch contain common
values, no conflicts will arise.
Genral Syntax For nested switch statements:-
switch(ch1) {
case 'A':
printf("This A is part of outer switch" );
switch(ch2) {
case 'A':
printf("This A is part of inner switch" );
break;
case 'B': /* case code */
}
break;
case 'B': /* case code */
}
Example Program:-
#include <stdio.h>
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRhB2SKgf4J8VpI9acMwWH98gS7yLi3s6G7qgOHcVV2v4eauajTcpErrbfI1xbK_Zo25kZYRymP6WU0vq3mgSUdg7httKOQj8kW4_rMahw14vefa0RU2IVJsz-PFyjRIXkl1iznaaPwxI/s320/s.c.jpg)
{
/* local variable definition */
float a = 10;
int b = 20;
switch(a) {
case 10:
printf("This is part of outer switch\n", a );
switch(b) {
case 20:
printf("This is part of inner switch\n", a );
}
}
printf("Exact value of a is = %d\n", a );
printf("Exact value of b is = %d\n", b );
return 0;
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjzkU2O2lbRYs4HHfr233Kk3L1gB97fE434bqJFWwsgKRs1MMBOqZnQngeuqW-3y2wmpuee8bgv6-DGfsV9MzzEQjUI16RRVKPTdCxh_YCLFfYQ3kTN3OP-6m6cKf7jle4qgbepeb0vf08/s320/sc.o.jpg)
When the above code is compiled and executed, it produces the following output:
This is part of outer switch
This is part of inner switch
Exact value of a is : 10
Exact value of b is : 20
-:Thanks For Visiting Us-:
Please Share On Social Media
Comments
Post a Comment