Decision Making - C Programming Professional Made easy (2015)

C Programming Professional Made easys (2015)

Chapter 7 Decision Making

The structures for decision making require you to specify at least one condition to be tested or evaluated by the program, along with statements that have to be executed if the condition is found to be true. However, statements that were found to be false can also be executed optionally. The general form of a decision making structure in most programming languages looks something like the example given below:

The C programming language assumes all non-null and non-zero values to be true. If there is either a null or zero, C assumes it to be false. The C programming language also makes use of the following decision making statements:

The If Statement

An if statement consists of a Boolean expression that is followed by at least one more statement.

Syntax

The syntax of an if statement is generally:

If (Boolean expression)

{

/* the statement(s) to be executed if the Boolean expression is true

*/

}

If the Boolean expression is found to be true, then the code inside the if statement is executed. Otherwise, the first set of code at the end of the if statement is executed.

As mentioned earlier, the C programming language assumes all non-null and non-zero values to be true while it assumes all null or zero values to be false.

Flow Diagram

Example:

#include <stdio.h>

int main ( )

{

/* local variable definition */

int a = 10;

/* use the if statement to check the Boolean condition */

if ( a < 20 )

{

/* if the condition is true, the following should be printed */

printf ( “a is less than 20\n” );

}

printf ( “the value of a is : %d\n”, a );

return 0;

}

Once the above code has been executed, it shows the following output:

a is less than 20;

the value of a is : 10

The If Else Statement

An if statement can be followed by an else state, which is optional. If the Boolean expression is false, then this statement is executed.

Syntax

The syntax of an if else statement is as follows:

if (Boolean expression)

{

/* the statement(s) to be executed if the Boolean expression is true */

}

else

{

/* the statement(s) to be executed if the Boolean expression is false */

}

If the Boolean expression is found to be true, then the if code is executed. On the other hand, if the Boolean expression is found to be false, then the else code is executed.

Again, the C programming language assumes all non-null and non-zero values to be true while it assumes all null or zero values to be false.

Flow Diagram

Example:

#include <stdio.h>

int main ( )

{

/* local variable definition */

int a = 100;

/* checking of the Boolean condition */

if ( a < 20 )

{

/* if the condition is true, the following should be printed */

printf ( “a is less than 20\n” );

}

else

{

/* if the condition is false, the following should be printed */

printf ( “a is not less than 20\n” );

}

printf ( “the value of a is : %d\n”, a );

return 0;

}

Once the above code is executed, the following output is shown:

a is not less than 20;

the value of a is : 100

The If, Else If, Else Statement

An if statement can be followed by an else if, else statement, which is optional. This statement is highly useful for testing different conditions using the single if else statement. Then again, when using the if, else if, else statements, you should keep in mind the following pointers:

1. An if can have zero or more else if’s. They have to come before the else.

2. An if can have zero or one else. It has to come after the else if.

3. If an else if is successfully executed, the remaining else’s or else if’s will no longer be tested.

Syntax

The general syntax of an if, else if, else statement is as follows:

if (first Boolean expression)

{

/* executes when the first Boolean expression is true */

}

else if (second Boolean expression)

{

/* executes when the second Boolean expression is true */

}

else if (third Boolean expression)

{

/* executes when the third Boolean expression is true */

}

else

{

/* executes when none of the above conditions is true */

}

Example:

#include <stdio.h>

int main ( )

{

/* local variable definition */

int a = 100;

/* checking of the Boolean condition */

if ( a == 10 )

{

/* if the condition is true, the following should be printed */

printf (“the value of a is 10\n” );

}

else if ( a == 20 )

{

/* if the else if condition is true, the following should be printed */

printf (“the value of a is 20\n” );

}

else if ( a == 30 )

{

/* if the else if condition is true */

printf (“the value of a is 30\n” );

}

else

{

/* if none of the above conditions is true */

printf (“None of the values match\n” );

}

printf (“The exact value of a is: %d\n”, a );

return o;

}

Once the above code is executed, the following output is shown:

None of the values match

The exact value of a is: 100

Nested If Statements

In the C programming language, you can test nest if else statements. This means that you can use one else if or if statement inside another else if or if statement(s).

Syntax

The general syntax for a nested if statement is as follows:

if (first Boolean expression)

{

/* executes when the first Boolean expression is true */

if (second Boolean expression)

{

/* executes when the second Boolean expression is true */

}

}

Example:

#include <stdio.h>

int main ( )

{

/* local variable definition */

int a = 100;

int b = 200;

/* checking of the Boolean condition */

if ( a == 100 )

{

/* if the condition is true, the following should be checked */

if ( b == 200 )

{

/* if the condition is true, the following should be printed */

printf (“The value of a is 100 and the value of b is 200\n” );

}

}

printf (“The exact value of a is : %d\n”, a );

printf (“The exact value of b is: %d\n”, b );

return 0;

}

Once the above code is executed, the following output is shown:

The value of a is 100 and the value of b is 200

The exact value of a is : 100

The exact value of b is : 200

Switch Statement

A switch statement allows a variable to undergo testing for equality against a list of values. Each one of the values is known as a case, and the variable being switched on is checked for every switch case.

Syntax

This is the general syntax for a switch statement:

switch (expression)

{

case constant-expression :

statement(s);

break;

/* optional */

case constant-expression :

statement(s);

break;

/* optional */

/* any number of case statements can be used */

default :

/* optional */

statement(s);

}

The following rules should be applied to a switch statement:

1. The case labels should be unique.

2. The case labels should end with a colon.

3. The case labels should have constants or constant expressions.

4. The case label should be on an integral type, such as integer or character.

5. The case label should not be a floating point number.

6. The switch case should have a default label.

7. The default label is optional.

8. The default may be placed anywhere in the switch.

9. The break statement should take control out of the switch.

10. A break statement may be shared by at least two cases.

11. Nesting, which is basically using a switch within a switch, can be used.

12. Relational operators should not be used in a switch statement.

13. Macro identifiers can be used as switch case labels.

14. The const variable can be used in a switch case statement.

15. An empty switch case can be used.

In order to make these rules clearer, you should consider the following examples for every rule:

Rule 1: The case labels should be unique.

Example:

int id = 3;

switch (id)

{

case 1:

printf (“C Language”);

break;

case 2:

printf (“C++ Language”);

break;

case 3:

printf (“Java Language”);

break;

default:

printf (“No student found”);

break;

}

Rule 2: The case labels should end with a colon.

Example:

case 1:

printf (“C Language”);

break;

Rule 3: The case labels should have constants or constant expressions.

Example:

case 1+1:

case 67:

case ‘A’:

Keep in mind that variables should not be used. Therefore, you cannot use the following:

case num2:

case var:

Rule 4: The case label should be on an integral type, such as integer or character.

Example:

case 20:

case 30 + 30:

case ‘B’:

case ‘b’:

Rule 5: The case label should not be a floating point number.

Example:

You cannot use a decimal value, such as:

case 2.5:

Rule 6: The switch case should have a default label.

Example:

switch (roll)

{

case 1:

printf (“C Language”);

break;

case 2:

printf (“C++ Language”);

break;

case 3:

printf (“Java Language”);

break;

default:

printf (“Default Version 1”);

break;

default:

printf (“Default Version 2”);

break;

}

Rule 7: The default label is optional.

Example:

switch (roll)

{

case 1:

printf (“C Language”);

break;

case 2:

printf (“C++ Language”);

break;

case 3:

printf (“Java Language”);

break;

}

Keep in mind that your program will still produce the same output even if you do not include default labels.

Rule 8: The default may be placed anywhere in the switch.

Example:

switch (roll)

{

case 1:

printf (“C Language”);

break;

case 2:

printf (“C++ Language”);

break;

default:

printf (“No student found”);

break;

case 3:

printf (“Java Language”);

break;

}

Rule 9: The break statement should take control out of the switch.

Rule 10: A break statement may be shared by at least two cases.

Example:

switch (beta)

{

case ‘a’:

case ‘A’:

printf (“Letter A”);

break;

case ‘b’:

case ‘B’:

printf (“Letter B”);

break;

}

Rule 11: Nesting, which is basically using a switch within a switch, can be used.

Example:

switch (beta)

{

case ‘a’:

case ‘A’:

printf (“Letter A”);

break;

case ‘b’:

case ‘B’:

switch (beta)

{

}

break;

}

Rule 12: Relational operators should not be used in a switch statement.

Example:

switch (num)

{

case >14:

printf (“Number > 14”);

break;

case =14:

printf (“Number = 14”);

break;

case <14:

printf (“Number < 14");

break;

}

Remember that you cannot use relational operators as switch labels.

Rule 13: Macro identifiers can be used as switch case labels.

Example:

#define MAX 1

switch (num)

{

case MAX:

printf (“Number = 1”);

break;

}

Rule 14: The const variable can be used in a switch case statement.

Example:

int const var = 1;

switch (num)

{

case var:

printf (“Number = 1”);

break;

}

Flow Diagram

Example:

#include <stdio.h>

int main ( )

{

/* local variable definition */

char grade = ‘B’;

switch (grade)

{

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 :

printf ( “Invalid grade\n” );

}

printf ( “Your grade is %c\n”, grade );

return 0;

}

Once the above code is executed, the following output is shown:

Well done

Your grade is B

Nested Switch Statements

If you are wondering whether or not it is possible to use a switch in an outer switch statement sequence, the answer is yes. You can include a switch in the statement sequence without encountering any conflicts even though the case constants of the outer and inner switch have common values.

Syntax

This is the general syntax for a nested switch:

switch (ch 1)

{

case ‘A’:

printf (“This A is part of the outer switch” );

switch (ch 2)

{

case ‘A’:

printf (“This A is part of the inner switch” );

break;

case ‘B’:

/* case code */

}

break;

case ‘B’:

/* case code */

}

Example:

#include <stdio.h>

int main ( )

{

/* local variable definition */

int a = 100;

int b = 200;

switch (a)

{

case 100:

printf (“This is part of the outer switch\n”, a );

switch (b)

{

case 200:

printf (“This is part of the inner switch\n”, a );

}

}

printf (“The exact value of a is : %d\n”, a );

printf (“The exact value of b is : %d\n”, b);

return 0;

}

Once the above code is executed, the following output is shown:

This is part of the outer switch

This is part of the inner switch

The exact value of a is : 100

The exact value of b is : 200

The ?: Operator

You can use the ?: operator as a replacement for an if else statement. Its general form is as follows:

Exp1 ? Exp2 : Exp3;

Exp1, Exp2, and Exp3 are types of expressions. Make sure that you take note of the placement of the colon in the statement. Keep in mind that the value of ? is determined as such:

Exp1 is first assessed. If it is proven to be true, Exp2 is assessed next and it serves as the current expression value. On the other hand, if Exp1 is proven to be false, then Exp3 is assessed next and its value serves as the current expression value.