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

Introduction To Array In C Language

Introduction To Array: *Array Is A Type Of Data Structure Which Store Collection of Data Of  Same Type.Which Can Be Integer Float Or Character. *All arrays consist of contiguous memory locations. *The lowest address corresponds to the first element and the highest  address to the last element. Declaration Of Array:- It Consist up of 3 part :- 1.Data Type  2.Array Name 3.Size Of Array General Syntax:-                                                      data_type Array_name[size_of_array]; For Example:-  int marks[20]; in example datatype of array is int and name is marks and size is 20. * It Is Called Single Dimensional Array * the Array Size Must be Integer And it   must be greater than zero. Initialization Of Array: *We initialize an array in C either one by one or using a single statement           ...

Types Of Array In C language

Two Dimensional Array :- * It Is The Combination Of Two One Dimensional Array * It Is Used To Implement Matrix Operation . *A two-dimensional array can be considered as a table which  will have 'a' number of rows and'b'number of columns. Declaration Of Two Dimensional Array:-   data_type Array_name [size of row][size of column]; For Example:- int matrix[3][4]; in above example we will store a matrix of 3*3. This Will Cleared From Following Figure:- *It can be represented a[i][j].where i and j represents  row and column respectively. Initialization Of Two Dimensional Array:- let us consider an array "float a[3][3]"then it will be initialized float a[3][3]={                {1.2,2.3,4.3} //initializers for row indexed by 0                {2.1,2.2,2.3} //initializers for row indexed by 1                {3.1,3.4,3.6} //initializers for row indexed by 2 ...

Pointer In C Language

          Introduction To Pointer: About Pointer:- *POINTER is a variable whose value is the address of another variable *Every variable is a memory location and every memory location  has its address defined which can be accessed using ampersand(&)  operator,which denotes an address in memory *Dynamic memory allocation,cannot be performed without using pointers. Let's start learning them in simple and easy steps. pointer variable declaration:- data_type *variable_name; *The asterisk ( * ) used to declare a pointer is the same asterisk used for   multiplication. some of the valid pointer declarations:- int *in;             //pointer to an intege r  double *db;  // pointer to a double  float *fl;         //  pointer to a float  char *chr    //pointer to a character   *The only difference between pointers of different data types is the data type of the va...