C programming control & Loop Structures & measure syntaxes

Click here to load reader

Transcript of C programming control & Loop Structures & measure syntaxes

Introduction to Decision Making OR Control 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 statement or decision-making statement:1. if statement1. switch statement1. Conditional Operator statement1. goto statement if Statement: -It is most powerful and widely used statement used for decision making. The syntax of the if statement is:if(test condition) {Block Statement-1}Statement-2......................................................First your computer will check the test 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. Truetest conditionBlock Statement-1FalseStatement-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. if...else Statement: -It is extension of the if statement. Its format is:if(test condition) {Block Statement-1}else {Block Statement-2}Statement-3......................................................First your computer will check the test 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.TrueFalseStatement-3Block Statement-1Block Statement-2test condition

The example of the if...else statement is:if(marks < 40) {printf(You are fail\n);}else {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...else Statement: -When we are using if...else statement with in one if...else statement then it will make the nested if...else statement. The format of nested if...else statement is:if(test condition-1) {if(test condition-2) {Block Statement-1}else {Block Statement-2}}else {Block Statement-3TrueBlock Statement-1test condition-1TrueFalsetest condition-2Block Statement-2Block Statement-3Statement-4

}False

Statement-4......................................................

Here first your computer will check the test condition-1. If the condition is true then computer check another test condition-2. If test condition-2 is true then it will execute Block Statement-1 otherwise execute the Block Statement-2. If test-condition1 is false then it will execute Block Statement-3.After completing the nested if...else statement execution computer will execute Statemetn-4.The example of the nested if...else is:if(no1>=no2) {if(no1>=no3) {printf(Number1 is Maximum\n);}else {printf(Number3 is Maximum\n);}}else {if(no2>=no3) {printf(Number2 is Maximum\n);}else {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.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...else if Statement: -It is used to make multipath decisions. Multipath decision involves chain of if with else if. The format of this statement is:TrueTrueFalseTrueBlock Statement-1Block Statement-2FalseBlock Statement-3Falsetest condition-2test condition-3Block Statement-4Statement-5test condition-1

if(test condition-1) {Block Statement-1}else if(test condition-2) {Block Statement-2}else if(test condition-3) {Block Statement-3}else {Block Statement-4}Statement-5......................................................Here first your computer will check the test condition-1. If the condition is true then computer will execute Block Statement-1 otherwise it will check the test condition-2. If the condition is true then computer will execute Block Statement-2 otherwise it will check the test condition-3. If condition is true then it will execute Block Statement-3 otherwise it will execute Block Statement-4.The example of the this statement is:if(percentage >=70) {printf(Distinction\n);}else if(percentage >= 60) {printf(First Class\n);}else if(percentage >= 50) {printf(Second Class\n);}else if(percentage >=40) {printf(Pass Class\n);}else {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 is multiple condition in the if statement it is become difficult to read and understand program. In this condition we can use switch statement, which allows checking the value of variable against the multiple case value. The format of the this statement is: switch(variablename) {casev1:Block Statement-1break;casev2:Block Statement-2break;casev3:Block Statement-3break;default:Block Statement-4break;}Statement-5Here 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.variable for match= =V1

default

= =V3

= =V2

BlockStatement-1BlockStatement-2BlockStatement-3BlockStatement-4Statement-5

Consider the following example of the switch statement to accept the month in number and print into word.switch(month) {case1:printf(January);break;case2:printf(February);break;case5:printf(May);break;case10:printf(October);break;default:printf(You have entered wrong value);break;}

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...else you can use nested switch statement. You can use only 15 level of nested switch. Also you can take only 257 cases into switch statement. Conditional Operator OR ?: 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 : Statement2If 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 : Value2Here 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 = (sales > 50000) ? 2000 : 0;In above example if your sales 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 (sales > 50000) {commission=2000;}else {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 I have 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 jump 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 arise. goto LABEL;LABEL :1. Backward jump: Here the jump is made to the previous statements. When label is before the goto statement then this kind of situation is arise. 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