Supriya Kolhe

Posted on

# C : Decision Making

1. What is decision making?
2. Types (if, if else, Nested if....else, else if ladder, switch , Nested switch, ?:, goto)

1.Decision making: It is about deciding the order of execution of statements based on certain conditions or repeat a group of statements until certain specified conditions are met.

2.if : If the expression returns true, then the statement-inside will be executed, otherwise statement-inside is skipped and only the statement-outside is executed.

``````if(expression)
{
statement inside_if;
}
statement outside_if;
``````
``````/*Example: */
#include <stdio.h>

void main( )
{
int x, y;
x = 15;
y = 13;
if (x > y )
{
printf("x is greater than y");
}
}
``````

3.if..else : If the expression is true, the statement-block1 is executed, else statement-block1 is skipped and statement-block2 is executed.

``````if(expression)
{
statement block1;
}
else
{
statement block2;
}
``````
``````/*Example: */
#include <stdio.h>

void main( )
{
int x, y;
x = 15;
y = 18;
if (x > y )
{
printf("x is greater than y");
}
else
{
printf("y is greater than x");
}
}
``````
1. Nested if....else : if expression is false then statement-block3 will be executed, otherwise the execution continues and enters inside the first if to perform the check for the next if block, where if expression 1 is true the statement-block1 is executed otherwise statement-block2 is executed.
``````if( expression )
{
if( expression1 )
{
statement block1;
}
else
{
statement block2;
}
}
else
{
statement block3;
}
``````
``````/*Example: */
#include <stdio.h>

void main( )
{
int a, b, c;
printf("Enter 3 numbers...");
scanf("%d%d%d",&a, &b, &c);
if(a > b)
{
if(a > c)
{
printf("a is the greatest");
}
else
{
printf("c is the greatest");
}
}
else
{
if(b > c)
{
printf("b is the greatest");
}
else
{
printf("c is the greatest");
}
}
}

``````

5.else if ladder : The expression is tested from the top(of the ladder) downwards. As soon as a true condition is found, the statement associated with it is executed.

``````if(expression1)
{
statement block1;
}
else if(expression2)
{
statement block2;
}
else if(expression3 )
{
statement block3;
}
else
default statement;
``````
``````/*Example: */
#include <stdio.h>

void main( )
{
int a;
printf("Enter a number...");
scanf("%d", &a);
if(a%5 == 0 && a%8 == 0)
{
printf("Divisible by both 5 and 8");
}
else if(a%8 == 0)
{
printf("Divisible by 8");
}
else if(a%5 == 0)
{
printf("Divisible by 5");
}
else
{
printf("Divisible by none");
}
}
``````

NOTES TO REMEMBER ABOUT IF ELSE:
->In if statement, a single statement can be included without enclosing it into curly braces { ... }

``````int a = 9;
if(a > 3)
printf("success");
``````

->"==" must be used for comparison in the expression of if condition, if you use = the expression will always return true, because it performs assignment not comparison.
->Other than 0(zero), all other values are considered as true.

``````if(9)
printf("True");
``````

above block will print "True".

6.switch : 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.

``````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);
}
``````
``````#include <stdio.h>

int main () {

/* local variable definition */

case 'A' :
printf("Excellent!\n" );
break;
case 'B' :
case 'C' :
printf("Well done\n" );
break;
case 'D' :
printf("You passed\n" );
break;
case 'F' :
printf("Better try again\n" );
break;
default :
}

return 0;
}
``````

->The expression (after switch keyword) must yield an integer value i.e the expression should be an integer or a variable or an expression that evaluates to an integer.
->The case label values must be unique.
->The case label must end with a colon(:)
->The next line, after the case statement, can be any valid C statement.

->break statements are used to exit the switch block. It isn't necessary to use break after each block, but if you do not use it, then all the consecutive blocks of code will get executed after the matching block.
->default case is executed when none of the mentioned case matches the switch expression. The default case can be placed anywhere in the switch case. Even if we don't include the default case, switch statement works.
->Nesting of switch statements are allowed, which means you can have switch statements inside another switch block. However, nested switch statements should be avoided as it makes the program more complex and less readable.

Difference between switch and if:
->if statements can evaluate float conditions. switch statements cannot evaluate float conditions.
->if statement can evaluate relational operators. switch statement cannot evaluate relational operators i.e they are not allowed in switch statement.

7.Nested switch : 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.

``````switch(ch1) {

case 'A':
printf("outer switch" );

switch(ch2) {
case 'A':
printf("inner switch" );
break;
case 'B': /* case code */
}

break;
case 'B': /* case code */
}
``````
``````/*Example: */
#include <stdio.h>

int main () {

int a = 110;
int b = 220;

switch(a) {

case 110:
printf("outer switch\n", a );

switch(b) {
case 220:
printf("inner switch\n", a );
}
}

printf("a is : %d\n", a );
printf("b is : %d\n", b );

return 0;
}
``````

8.?:: can be used to replace if...else statements.

``````Exp1 ? Exp2 : Exp3;
``````

->Exp1 is evaluated. If it is true, then Exp2 is evaluated and becomes the value of the entire ? expression.

->If Exp1 is false, then Exp3 is evaluated and its value becomes the value of the expression.

9.goto : allows us to transfer control of the program to the specified label. label is an identifier. When the goto statement is encountered, the control of the program jumps to label: and starts executing the code.

Why to not avoid goto:
->code might get hard to follow.
->goto allows one to jump out of scope.

``````goto label;
... .. ...
... .. ...
label:
statement;
``````
``````/*Example: */
#include <stdio.h>

int main() {

const int maxInput = 100;
int i;
double number, average, sum = 0.0;

for (i = 1; i <= maxInput; ++i) {
printf("%d. Enter a number: ", i);
scanf("%lf", &number);

// go to jump if the user enters a negative number
if (number < 0.0) {
goto jump;
}
sum += number;
}

jump:
average = sum / (i - 1);
printf("Sum = %.2f\n", sum);
printf("Average = %.2f", average);

return 0;
}
``````