1 P a ge C Programming, Decision Making C Programming Decision Making Introduction to Decision Making OR Control Statement OR Branching Statement: - We know that the execution of program is done instruction by instruction. After executing one instruction your computer will execute another instruction. This flow of the instruction execution is known as sequential execution. But some time you need to change the flow of the instruction based on the certain condition. To change the flow based on the condition we are using the Decision making statement or control statement. C supports following control statements or branching statements or decision-making statements: 1. if statement 2. if... statement 3. Nested if... statement 4. Ladder if statement 5. switch statement 6. Conditional Operator statement 7. goto statement if Statement: - It is most powerful and widely used statement used for decision making. The syntax of the if statement is: if( condition) Statement-2;... First your computer will check the condition. If the condition is true then computer put non-zero value and execute the Block Statement-1, after executing block statement it will execute Statement-2. If condition is false then computer put zero value and will not execute Block Statement-1 but directly execute Statement-2. condition Block Statement-1 Statement-2 The example of the if statement is: if(marks < 40) printf( You are fail\n ); printf( Total Marks = %d\n,marks); Here first the if statement is executed. Your computer will check whether marks is less than 40 or not and if it is that then it will print message You are fail and then print the total marks otherwise it directly print the total marks.
2 P a ge C Programming, Decision Making if... Statement: - It is extension of the if statement. It s format is: if( condition) Block Statement-2; Statement-3; First your computer will check the condition. If the condition is true then computer execute the Block Statement-1. If condition is false then computer will execute Block Statement-2. After executing either Block Statement-1 or Block Statement-2 it will execute the Statement-3. Block Statement-1 condition Block Statement-2 Statement-3 The example of the if... statement is: if(marks < 40) printf( You are fail\n ); printf( You are pass\n ); printf( Total Marks = %d\n,marks); Here first the if statement is executed. Your computer will check whether marks is less than 40 or not and if it is that then it will print message You are fail otherwise it will print the message You are pass. After print pass or fail it will print total marks. Nested if... Statement: - When we are using if... statement within one if... statement then it will make the nested if... statement. The format of nested if... statement is: if( condition-1) if( condition-2) Block Statement-2;
3 P a ge C Programming, Decision Making Block Statement-3; Statement-4;... Here first your computer will check the condition-1. If the condition is true then computer check another condition-2. If condition-2 is true then it will execute Block Statement-1 otherwise execute the Block Statement-2. If -condition1 is false then it will execute Block Statement-3. After completing the nested if... statement execution computer will execute Statemetn-4. condition-1 Block Statement-3 condition-2 Block Statement-2 Block Statement-1 Statement-4 The example of the nested if... is: if(no1>=no2) if(no1>=no3) printf( Number1 is Maximum\n ); printf( Number3 is Maximum\n ); if(no2>=no3) printf( Number2 is Maximum\n ); printf( Number3 is Maximum\n ); Here first the if statement is to check whether no1>=no2. If condition is true then it will check whether no1>=no3. If second condition is true then it will print message Number1 is Maximum otherwise print the message Number3 is Maximum.
4 P a ge C Programming, Decision Making If condition no1>=no2 is false then computer will check the condition no2>=no3 and it true then it will print message Number2 is Maximum otherwise print the message Number3 is Maximum. if... if Statement (ladder if Statement) : - It is used to make multipath decisions. Multipath decision involves chain of if with if. The format of this statement is: if( condition-1) if( condition-2) Block Statement-2; if( condition-3) Block Statement-3; Block Statement-4; Statement-5;... Here first your computer will check the condition-1. If the condition is true then computer will execute Block Statement-1 otherwise it will check the condition-2. If the condition is true then computer will execute Block Statement-2 otherwise it will check the condition-3. If condition is true then it will execute Block Statement-3 otherwise it will execute Block Statement-4. condition-1 condition-2 Block Statement-1 condition-3 Block Statement-2 Block Statement-3 Block Statement-4 Statement-5
5 P a ge C Programming, Decision Making The example of the this statement is: if(percentage >=70) printf( Distinction\n ); if(percentage >= 60) printf( First Class\n ); if(percentage >= 50) printf( Second Class\n ); if(percentage >=40) printf( Pass Class\n ); printf( Fail\n ); In above example first computer check whether percentage>=70 or not. If it is true then it will print message Distinction otherwise check another condition percentage>=60. If it is true then print message First Class and in the same at last it will print the message Fail if above all condition is fail. switch Statement: - When there are multiple conditions in the if statement, it becomes difficult to read and understand program. In this case we can use switch statement, which allows checking the value of variable against the multiple case value. The format of this statement is: switch(variablename) case v1 : case v2 : Block Statement-2; case v3 : Block Statement-3;... default : Block Statement-4; Statement-5; Here if the value of variable= = v1 then Block Statement-1 will be executed. If variable= = v2 then Block Statement-2 will be executed. In similar way if at last no match found for the variable then default statement is executed. Here we have to put the break statement to complete the execution of switch statement after matching one case.
6 P a ge C Programming, Decision Making variable for match = =V1 = =V2 = =V3 default Block Statement-1 Block Statement-2 Block Statement-3 Block Statement-4 Statement-5 Consider the following example of the switch statement to accept the month in number and print into word. switch (month) case 1 : printf( January ); case 2 : printf( February ); case 5 : printf( May ); case 10 : printf( October ); default : printf( You have entered wrong value ); In above program if you enter month equal to 1 then print January, month equal to 2 then February, month equal to 5 then May, month equal to 10 then October. If no any match is found for month then print, You have entered wrong value. Similar to nested if... you can use nested switch statement. You can use only 15 levels of nested switch. Also you can take only 257 cases into switch statement. Conditional Operator (?: Operator) : - C provides the facility of the operator to check the condition by using the conditional operator having three operands. The format of the condition: (Condition)? Statement1: Statement2; If condition is true then statement1 will be executed otherwise statement2 will be executed. Here you can also represent the condition for assignment operation: Variable = (Condition)? Value1: Value2;
7 P a ge C Programming, Decision Making Here if condition is true then variable has assigned the value1 otherwise it has assigned the value2. Consider the following example to understand the conditional operator: commission = (sale > 50000)? 2000: 0; In above example if your sale is greater than 50000 then your commission value is 2000 otherwise it is zero. Above condition you can represent using if statement as under: if (sale > 50000) commission=2000; commission =0; The advantage of the conditional operator is that you can represent the condition in shorter form. But some times it is difficult to understand when you are using multiple conditional operators. goto Statement: - goto is one kind of jump statement in the C language. It transfers your control from one statement to another statement randomly. In goto statement there is one label is used to indicate at which position you want to make the jump. Label is end with the colon (:). Whenever you have to make the jump you have to write statement goto Label. There are two kind of jumps performed by goto: 1. Forward jump: Here the jump is made to the next following statements. If label is after the goto statement then this kind of situation is arised. goto LABEL; LABEL : 2. Backward jump: Here the jump is made to the previous statements. When label is before the goto statement then this kind of situation is arised. The syntax of backward jump is, LABEL : goto LABEL; Consider the following example to understand goto statement, main( ) int a, b, c; CAL : printf( Enter a & b : ); scanf( %d%d,&a,&b); c=a + b; printf( c = %d\n,c); if(c<=0) goto CAL;
8 P a ge C Programming, Decision Making Here the program is used to calculate the c value up to the value is not going to be larger then zero. Generally goto statement is used to read the data again in your program. It is also used in the loop to break the loop, while(condition).. goto OUT;. OUT : B We have to avoid the use of goto statement because of 1. Some compiler generates less efficient code if you are using goto statement. 2. Your program becomes unreadable. 3. Program logic becomes complicated and program becomes fewer under-stables.