Skip to main content

Decision Making In C Language


Introduction To Decision Making:-

Decision-making structures require that the programmer specifies 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.
*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 */
}
Example Program:-
#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" );
}
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 less than=29
value of a is =19
if..else Statement:-
An if statement can be followed by an optional else statement, which executes
when 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 */
}
Example Program:-
#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 */
printf("a is not less than 89\n" );
}
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:-
#include <stdio.h>
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>
int main ()
{
/* 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;
}
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

Popular posts from this blog

Introductution To Structures In C Language

About Structure:- Arrays allow to define a type of variables that can hold several data items of the same kind. Similarly, structure is another user-defined data type available in C that allows to combine data items of different kinds. Let Us Take An Real Life Example: If We want to track the following attributes about each book: 1.Title (May Be String) 2.Author(May Be String) 3.Subject (May Be String) 4.Book ID (Integer Type) Than We Have to Define A Structure Variable That Can Take This Different Type Of Data-Types. Syntax For Defining A Structure:- struct [structure_Name] { member definition; member definition; ... member definition; } [one or more structure variables]; For Information of Student The Definition of structure Will Be: struct student_data {  char name[20];  int roll_Number;  int standered;  char father's_name[20]; } std1; //structure Vaiable   Accesing Structure Members:- The Members of structure variable accesed by Dot(.) Operator.. Let Us Take...

Use Of Pointers In C language

Use Of Pointer In C Language:- We Have already Discussed About Pointe rs In Previous Post. This is the most important and useful IN C programming . The Uses Of Pointers In C Language Is Given below:- 1.Array of pointers:- We Can Use Collection Of Pointer by using   An Array Of Pointers. 2.Pointer to pointer:- C allows you to have pointer on a pointer. 3.Passing pointers to functions:-  We Pass Pointer As  Arguments. Now We Will know About These Uses Of Pointers One By One:- 1.Array of pointers:- In This Method We Declare An array For Storing Pointers: declaration of an array of pointers to an integer:- int *ptr[MIN]; declaration of an array of pointers to an float:- flot *ptr[MIN]; declaration of an array of pointers to a CHAR:- char *ptr[]; Example Program:- THIS PROGRAM DEMONSTRATES ARRAY OF POINTER, WE SIMPLY PRINT ADDRESS AND VALUE OF POINTERS #include <stdio.h> const int MAX = 5; int main () { int *ptr; int var[] = {10, 100, 200,400,500}; int i; ptr=var; for (...