Decision Making

Decision making in a programming language is an important concept Since sometimes we want to execute part of the statements among many statements when certain condition meets.

C programming language supports 4 types of Decision making Control statements

  1. if statement
  2. if else statement
  3. nested if else statement
  4. Switch statement

Another concept is there in C Programming that is Loop which is also related to decision-making, we will have a look at in next chapter.

if Statement:

In this type, if the condition is true , then the respective block of the code will be executed.

syntax:

if(condition)

{

statements

}

if non zero number is placed inside if condition means , then the statement gets satisfied and returns”TRUE”

8993d if2bstatement bmp 213x300

void  main()
{
int a=40,b=20;
if (a > b)
{
printf("a is greater than b");
}
}

output:

a is greater than b

if else Statement:

Group of statements will be executed , if condition is satisfied . If condition is not satisfied, then else part of that conditions will be executed.

syntax:

if(condition)

{

    statements to be executed if condition is true.

}

else

{

    statements to be executed, if condition is false

}

8cff4 if else2bstatement bmp 275x300

program:

void  main()
{
int a=40,b=20;
if (a < b)
{
printf("a is less than b");
}

else

{

    printf("a is greater than or equal to b");

}

}

output:

a is greater than or equal to b.

nested if else statement:

if… else can be used in nested forms when serious of decisions are involved.

syntax:

if (condition)
statements to be executed if test expression is true;
else if(condition)
statements to be executed if test expressions 1 is true;
else if (condition)
. . .
else
statements to be executed if all test expressions are false;

program:

void  main()
{
int a=40,b=20;
if (a == b)
{
printf("a is equal to b");
}

else if( a > b)

{

    printf("a is greater than  b");

}

else

{

printf("a is less than b");

}

}

output:

a is greater than  b

switch.. case statement:

switch .. case is similar to nested if else statement, but the problem is , when we use multiple nested if else, it will increase the complexity of program.

syntax:

switch (expression)
{
case constant1:
statements  to be executed if expression equals to constant1;
break;
case constant2:
statements  to be executed if expression equals to constant3;
break;
. . .
default:
statements  to be executed if expression doesn't match to any cases;
}

expression may be a number or a character .

switch case checks the  value of expression or variable against the list of cases .When the match is found,  block of the code is executed associated with that case.

Break statement takes the control out from the case.

Break statement is optional. If we forget to add break statement in a case, then the following cases also will be executed.

program:

# include
int main()
{
char operator;
int num1 = 10,num2 = 10;
operator='+';
switch(operator)
{
case '+':
printf("num1+num2=%d",num1+num2);
break;
case '-':
printf("num1-num2=%d",num1-num2);
break;
case '*':
printf("num1*num2=%d",num1*num2);
break;
case '/':
printf("num2/num1=%d",num1/num2);
break;
default:
/* if operator is other than +, -, * or /, error message is shown */
printf(Error! operator is not correct");
break;
}
return 0;
}

output:

num1+num2=20;

goto statement:

goto statements is used to transfer the normal flow of a program to the specified label in the program.

Syntax:
{
…….
go to label;
…….
…….
Label:
Statements;
}

Leave a Reply

Your email address will not be published. Required fields are marked *