Decision Making and Branching, C Language Assignment Help

Assignment Help: >> Introduction to C language >> Decision Making and Branching, C Language

We know that the C program is a set of statements which are normally executed sequentially in the order in which they appear. This happens when no repetitions of certain calculations are necessary. However, in practice, we have a number of situations where we may have to change the order of execution of statements based on certain conditions, or repeat a group of statements until certain specified conditions are met. This involves a kind of decision making to see whether a particular condition has occurred or not and then direct the computer to execute certain statements accordingly.

By default the instructions in a program are executed sequentially, but it is not required frequently. Control or conditional statements enable us to change the flow of the program. Based on a condition, a statement or a sequence of statements takes alternative actions.

The if Statement

 

Most of the programming tools use the "if statement" to make decisions. One of the fundamental concepts of computer science is if a certain condition is true, the computer is directed to take one course of action, and if condition is false, it is directed to do something else. The general form of "if statement".

                if (conditional expression)

                {

                                statement1;

                                statement2;

                                . . .

                                . . .

                }

 

The expression after the keyword if is the conditional expression that is to be checked or evaluated and it is always enclosed within parentheses. This is followed by a statement or set of statements that are executed only if the conditional expression is evaluated to true. If the condition provided is not true then all the statements in if block are skipped and the program resume from the statement just following if block.

The block of if statement is delimited by curly braces. If the block of if statement involves only one statement then the use of braces can be omitted.

 

The if-else statement

The block of if statement is executed only if the conditional expression evaluates to true. It does nothing when the condition is false. One can execute a group of statements if condition is true and another group of statements when the condition is false using the if-else construct.

 

The general form of "if-else construct":

                if (conditional expression)

                {

                                group of statements1;

                }

                else

                {

                                group of statements2;

                }

 

If the result of the evaluation of the conditional expression is true then the group of statements1 will execute otherwise group of statements2 will execute, but not the both.

 

Nested if-else statements

 

It is perfectly possible to write an entire if-else construct within either the body of if statement or the body of else statement or in both of the statements to fulfill the requirement of the programmer. The process of constructing if-else within if-else statements is called nesting of if-else statements.

An if-else statement already under the if-else construct can also contain another if-else statement and so on. There is no limit on how deeply the ifs and else's can be nested.

 

Example:

                if  (condition1)

                {              statements;

                                if(condition2)

                                {

                                                statements;

                                }

                                else

                                {

                                                statements;

                                }
                }

                else

                {              statements;

                }

 

Example:            

if  (condition1)

                {

                                statements;

                }

                else

                {

                                statements;

if(condition2)

                                {

                                                statements;

                                }

                                else

                                {

                                                statements;

                                }
                }

The "if block" as well as "else block" both can have nested if-else constructs simultaneously too.

 

The if-else if ladder

 

There is another way of putting "if statements" together when multiple decisions are involved. A multi path decision is a chain of "if constructs" in which the statements associated with each 'else' is an 'if' again. It takes the following form:

if (condition1)

521_neasted_if_else.png                statement 1;

   else if (condition2)

                   statement 2;

      else if (condition3)

                      statement 3;

                .

                .

                .

                .

                else if (condition n)

                                statement n;

                                else

                                                default statement;

 

statement x;

statement y;

.

.

This construct is known as the else-if ladder. The conditions are evaluated from the top of the ladder to downwards. As soon as the true condition is found, the statement associated with it is executed and control is transferred to the statement x (skipping the rest of the ladder). When all the n conditions become false, then the final else containing the default statement is executed.

 

The switch-case statement

 

The control statement that allows us to make a decision from the number of choices is called switch-case-default construct. These three keywords go together to make up the control statement. Mostly the switch-case statement is used when an integer variable is to be successively compared against different values.

The general form of switch-case statement;

                switch (integer expression)

                {

                                case constant-1:

                                                statement-1;

                                case constant-2:

                                                statement-2;

                                .

                                .

                                .

case constant-n:

                                                statement-n;

                                default:

                                                default-statement;

                }

The integer expression following the keyword switch is any C expression that will yield an integer value. It could be any int or char variable or an expression that is evaluated to an integer value.

The keyword case is followed by an integer or a char constant. Each constant in each case must be different from all the others. The statements in each case represent any valid C statements.

First at all, the entire integer expression is evaluated. The resultant value is then matched one by one against the constant values that follow the case statements. When a match is found, the program executes the statement following that case, and all subsequent case and default statements as well. If no match is found, only the default statement following the default keyword is executed.

If we want that only case that matches the switch variable will execute and not the other, then we has to put a break statement after statements associated with each case.

                switch (integer expression)

                {

                                case constant-1:

                                                statement-1;

                                                break;

                                case constant-2:

                                                statement-2;

                                                break;

                                .

                                .

                                .

case constant-n:

                                                statement-n;

                                                break;

                                default:

                                                default-statement;

                }

There is no need of break statement after the statements following the default keyword. If a match is found in a case with such construct, the statement following that case executes and the break statement moves the control out of the switch-case construct.

A case can include more than one statement and there is no need to enclose these multiple statements associated with the case within the pair of braces.

We cannot include a float value in the switch-case construct, only int or char variables are allowed. A switch may occur in another switch it is called nested switch-case construct.

void main ( )

{

                                int ch;

                                printf ("Enter your choice");

                                scanf ("%d", &ch);

                                switch (ch)

                                {
                                                case1:

                                                                printf ("I am in first case");

                                                                break;

                                                case2:

                                                                printf ("I am in second case");

                                                                break;

                                                case3:

                                                                printf ("I am in third case");

                                                                break;

                                                default:

                                                                printf("Invalid Choice");

                                }

}

The Tips and Traps:

  • You can put the cases in any order you want.
  • You are also allowed to use char values in case and switch.
  • Sometimes there may not be any statement in some of the cases in switch, but still they might turn out to be useful.

main ( )

{

                                char ch;

                                printf ("Enter any of the alphabet a, b, or c");

                                scanf ("%c", & ch);

                                switch (ch)

                             {

                                                case 'a' :

                                                case 'A' :

                                                                printf ("A for Apple");

                                                                break;

                                                case 'b' :

                                                case 'B' :

                                                                printf ("B for Boy");

                                                                break;

                                                case 'c' :

                                                case 'C' :

                                                                printf("C for Cat");

                                                                break;

                                                default:

                                                                printf("Invalid Choice");

                                }

}

 

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd