0% found this document useful (0 votes)
35 views

Pre/Post Increment/Decrement Operators

The document discusses pre-increment and post-increment operators in C. Pre-increment performs the increment operation before any assignment, while post-increment performs increment after assignment. This is demonstrated through examples showing how loop and count values are assigned. The document then discusses if/else statements and logical operators that allow conditional execution of code based on expression evaluations. Nested if/else statements, else-if ladders, relational operators, and logical operators like AND, OR, and NOT are described with examples.

Uploaded by

Sree Murthy
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Pre/Post Increment/Decrement Operators

The document discusses pre-increment and post-increment operators in C. Pre-increment performs the increment operation before any assignment, while post-increment performs increment after assignment. This is demonstrated through examples showing how loop and count values are assigned. The document then discusses if/else statements and logical operators that allow conditional execution of code based on expression evaluations. Nested if/else statements, else-if ladders, relational operators, and logical operators like AND, OR, and NOT are described with examples.

Uploaded by

Sree Murthy
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

PRE/POST INCREMENT/DECREMENT OPERATORS PRE means do the operation first followed by any assignment operation.

POST means do the operation after any assignment operation. Consider the following statements ++count; /* PRE Increment, means add one to count */ count++; /* POST Increment, means add one to count */ In the above example, because the value of count is not assigned to any variable, the effects of the PRE/POST operation are not clearly visible. Lets examine what happens when we use the operator along with an assignment operation. Consider the following program, #include <stdio.h> main() { int count = 0, loop; loop = ++count; /* same as count = count + 1; loop = count; */ printf("loop = %d, count = %d\n", loop, count); loop = count++; /* same as loop = count; count = count + 1; */ printf("loop = %d, count = %d\n", loop, count); } If the operator precedes (is on the left hand side) of the variable, the operation is performed first, so the statement loop = ++count; really means increment count first, then assign the new value of count to loop. Decision Making Statements Many times in the program such conditions occur when we want to take the exact decisions in order to make an error free program. This kind of situations can be handled using decision control instructions of C. It includes if - else statements and the conditional operators. The decision control structure in C can be implemented using: a) The if statement b) The if - else statement c) Nested if else d) The else if ladder The if Statement [Ref. 1a, 2a] The general form or syntax of if statement looks like this: if (condition) { //execute these statements; } Here, the keyword if tells the compiler that what follows, is a decision control instruction. The condition following the keyword if is always enclosed within a pair of parentheses. If the condition is true, then the statements in the parenthesis are executed. It the condition is not true then the statement is not executed instead the program skips this part. The condition in C is evaluated using Cs relational operators as well as logical operators. These operators help us to build expression, which are either true or false. For example: X == Y X is equal to Y X != Y X is not equal to Y

X < Y X is less than Y X > Y X is greater than Y X <= Y X is less than or equal to Y X >= Y X is greater than or equal to Y Demonstration of if statement: /* Example 2.1 */ #include<stdio.h> main( ) { int num; printf(Enter a number less than 10 :); scanf(%d, &num); if(num < = 10) printf(The number is less than 10); } Sample output of this program will be: Enter a number less than 10 : 7 The number is less than 10 Executing this another time: Enter a number less than 10 : 15 After second statement there will no output because the condition given in the if statement is false. The if statements are only executed when the condition is true.

See the above flowchart. If the condition is true then statement-1 is executed else and if false it wont be executed. Remember, statements2 and 3 are executed always. The condition can be the combination of relational and logical operators also. Such as: if (i > 1 || j!=5) { printf("i = %d , i ); } Multiple Statements within if If it is desired that more than one statement is to be executed if the condition following if is satisfied, then such statements must be placed within pair of braces.

e.g The following program demonstrate that if year of service greater than 3 then a bonus of Rs. 2500 is given to employee. The program illustrates the multiple statements used within if. /* Example 2.2 calculation of bonus */ #include<stdio.h> { int bonus, cy, yoj, yos; printf(Enter current year and year of joining : ); scanf(%d%d,&cy,&yoj); yos = cy - yoj; if(yos > 3) { bonus = 2500; printf(Bonus = Rs. %d, bonus); } } Lets see the output of this program: Enter current year and year of joining : 2006 2001 Bonus = Rs. 2500 In this program, value of cy and yoj are taken from user i.e. 2006 and 2001. Value of yos will become 5. The condition within if will check whether the value of yos is greater than 3 or not. Here, condition will become true and the statements followed by if are executed. The if-else statement The if statement by itself will execute a single statement or a group of statements when the condition following if is true, it does nothing when the condition is false. If the condition is false then a group of statements can be executed using else statement. Its syntax is as given below: if(condition) { //statements1 } else { //statements2 } Here, if the condition is true then statements1 are executed else statements2 are executed. It is demonstrated in following program. /* Example 2.3 Calculation of gross salary */ #include<stdio.h> main( ) { float bs, gs, da, hra; printf(Enter basic salary); scanf(%f, & bs); if(bs <1500) { hra = bs * 10/100; da = bs * 90/100;

} else { hra = 500; da = bs * 98/100; } gs = bs+hra+da; printf(gross salary = Rs. %f, gs); } When, we used if else statements, either if or else statement block will execute depending upon the condition given in the if statement. The else doesnt require condition. Execution of else block also depends on if(condition). When it is false program control transfers to else block.

Note: 1. Statements inside the ifs curly braces are called as if block and statements for elses curly braces are called as else block. 2. There is no necessity to have an else for if but, each else must match an if statement else there will be a compiler error. The error will be Misplaced else. 3. If there is only one statement inside if as well as else block, we can eliminate the curly braces. Nested if else If we write an entire if - else construct within the body of the if statement or the body of an else statement. This is called nesting of ifelse. Syntax: if(condition) { //statements1 if (condition) { //statements2 } else {

//statements3 } } else //statements4 Here, the inner condition executed only when the outer condition of if is true. This hierarchy of nesting of if can be extended in deep with any number of if-else statements.

#include<stdio.h> main( ) { int y; printf(Enter year : ); scanf(%d, &y); if(y > 2000) { printf(\n Number is greater than 2000); if(y % 4 == 0) { printf(\n This is leap year); } else printf(\n This is not leap year); } else { printf(\n Number is less than 2000); if(y % 4 ==0) { printf(\n This is leap year); } else printf(\n This is not leap year); }

Output: Enter year : 1988 Number is less than 2000 This is leap year When the user enters 1988, first if condition checks whether it is greater than 2000 or not? Here this condition will become false and control will transfer to else block. Again if(y % 4==0) inside else block will check whether it is totally divisible by 4 or not? Using Logical operators As we have already seen the logical operators. The basic use of these is related to if else statement. These conditional operators are: a) && (AND operator) b) || (OR operator) c) ! (NOT operator) When, we want to specify more than one condition in the single if statement we can use logical operators among these conditions. Though, there are two & (and) in the operator it is pronounced as AND and || as OR. && Operator is used when there is necessity that both the conditions to be true. || Operator is used when there is necessity that any one conditions to be true. e.g. if (a > 15 && b < 10) { printf(Hello); } Here, if value of a is greater than 15 as well as value of b is less than 10 then printf(Hello); statement will be executed. Else it will perform the statements after the if block. Similarly, see the example of OR operator. if (a > 15 || b < 10) { printf(Hello); } Here, if value of a is greater than 15 or value of b is less than 10 then printf(Hello); statement will be executed. Else it will perform the statements after the if block. Now, lets focus on the NOT operator (!). This operator reverses the value of expression it operates on; it makes true expression false and false expression true. e.g. ! (a > 10) This means that whether a is not greater than 10. In other words, it can be written as (a <= 10). The else-if ladder There is another way of putting multiple ifs together when multipath decisions are involved. A multipath decisions is a chain of ifs in which the statement is associated with each else in an if. It takes the following general form: if(condition1) //statements1; else if(condition2) //statements2;

else if(condition3) //statements3; else if(condition n) //statements n; else //statements-default; //statements-x; This construct is known as else-if ladder. The conditions are evaluated from top to the bottom of ladder. As soon as true condition is found, statements associated with it are executed and then program control is transferred to the statements-x, skipping the rest of the ladder. When all the conditions become false, then final else containing default statements will be executed. This is shown in the following flowchart

#include<stdio.h> main( ) { int a, b, c, d, e, per; printf(Enter marks of four subjects : ); scanf(%d %d %d %d, &a, &b, &c, &d); if(a < 40 || b < 40 || c < 40 || d < 40) printf(\n You have failed in one subject); per = (a + b + c + d) / 4; if(per > = 60) printf(\n First class); else { if (per <=59 && per >=55) printf(\n Higher second class);

else if(per <=54 && per >=40) printf(Second class); else printf(\n Fail); } } In this example we have used else if ladder as well as logical operators. if(a < 40 || b < 40 || c < 40 || d < 40) This statement will check whether marks of any one subjects is less than 40 or not? If it is, it will execute the statement followed by if i.e. printf(\n You have failed in one subject); Now, see the following statement, if (per <=59 && per >=55) Here, if the value of per is less than or equal to 59 as well as greater than or equal to 55 then the statement followed by this will be executed i.e. printf(\n Higher second class); Else, the control will move to the else block. The switch Statement This is another form of the multi-way decision statement. It is well structured, but can only be used in certain cases. The switch statement tests value of a given variable (or expression) against the list of case values and when the match is found, a block of statements associated with that statements are executed. The general form of switch statement is as follows: switch(variable) { case value-1: //statements1; break; case value-2: //statements2; break; case value-3: //statements3; break; --------default: //default-statements; break; } statements-x; In the above given syntax, value-1, value-2, value-3. are the set the constants. When the value of the variable given in the switch brackets is matched with any one of these values, the particular set of statements are executed and then program control transfers out of the switch block. For example, if value of variable is matched with value-2 then statements2 are executed and the break statement transfers the program control out of the

switch. If the match is not found, the default block is executed. Remember, statements-x are executed in any case. Generally, switch case are executed for menu selection problems. int number = 2; switch(number) { case 0 : printf("None\n"); break; case 1 : printf("One\n"); break; case 2 : printf("Two\n"); break; case 5 : printf("Several\n"); break; default : printf("Many\n"); break; } Here, the output of this program will be:- Two Flowchart: there are two ways in which a switch flowchart can be drawn.

Rules of switch statement The switch variable must be an integer or character type. Case labels must be constants of constant expressions. Case labels must be unique. No two labels can have the same value.

Case labels must end with a colon. The break statement transfers the program control out of the switch block. The break statement is optional. When the break is not written in any case then the statements following the next case are also executed until the break is not found. The default case is optional. If present, it will be executed when the match with any case is not found. There can be at most one default label. The default may be placed any where but generally written at the end. When placed at end it is not compulsory to write the break for it. We can nest the switch statements. Difference between if statement and switch statement

The loop control structures Many times it is necessary to execute several statements repetitively for certain number of times. In such cases, we can use looping statements of C programming language. The statements are executed until a condition given is satisfied. Depending upon the position of the condition in the loop, the loop control structures are classified as the entry-controlled loop and exit-controlled loop. They are also called as pre-test and post-test loops respectively. The while loop The while is an entry-controlled loop statement having following general form: while(condition) { //loop statements or body of loop } Here, the condition is evaluated first, if it is true then loop statements or body of the loop is executed. After this the program control will transfer to condition again to check whether it is true or false. If true, again loop body is executed. This process will be continued until the condition becomes false.

When is becomes false, the statements after the loop are executed. This is also called as pre-test loop.

For example: If we want to print the number from 1 to 30 using a while loop, it can be written as: int num = 1; /* initialize counter */ while(num<=30) /* check the condition */ { /* start of loop */ printf(\n%d,num); /* print the number */ num++; /* increment the counter */ } /* end of loop */ See one more practical example of while loop. That is, if we want to find the addition of all the numbers from 10 to 20. It can be written as: int x = 10, sum = 0; /* initialize variables */ while(x <=20) /* check the condition */ { /* start of the loop */ sum = sum + x; /* add the values */ x++; /* increment counter */ } /* end of the loop */ printf(\nAddition is : %d,sum); /* print addition */ The do-while loop This is exit-controlled loop statement in which the condition of the loop statement is written at the end of the loop. It takes the following general form: do { //loop statements or loop body } while(condition); Here, when the program control is reached at do statement the loop body or the statements inside the loop are executed first. Then at the end the condition is the while is checked. If it is true, then program control again transfers to execute the loop statements. This process continues until the condition becomes false. In short, we can say that the loop statements are executed at least once without checking condition for its trueness.

This loop is also referred as post-test loop structure. Consider the same examples used above: If we want to print the number from 1 to 30 using a do-while loop, it can be written as: int num = 1; /* initialize counter */ do /* do-statement */ { /* start of loop */ printf(\n%d,num); /* print the number */ num++; /* increment the counter */ } /* end of loop */ while(num<=30); /* condition */ See one more practical example of do-while loop. That is, if we want to find the addition of all the numbers from 10 to 20. It can be written as: int x = 10, sum = 0; /* initialize variables */ do /* do-statement */ { /* start of the loop */ sum = sum + x; /* add the values */ x++; /* increment counter */ } /* end of the loop */ while(x <=20); /* check the condition */ printf(\nAddition is : %d,sum); /* print addition */ Depending upon the problem statement given we can use either while or do-while loop statements.

The for loop The for loop is an entry controlled loop structure which provides more concise loop structure having following general form: for(initialization ; condition ; increment/decrement) {

//loop statements or loop body } The execution of for loop statement takes as follows: 1. Initialization of loop control variables is done first using assignment operators such as: i = 1 or count = 0 etc. Remember this part is executed only once. 2. The condition given afterwards is checked then. If this condition is true, the statements inside the loop are executed. Else program control will get transferred nest statement after the loop. The condition can be combination of relational as well as logical operators such as: count < 10 3. When the body of the loop is executed, program control will get transferred back to for statement for executing the third statement that is, increment/decrement. In this part the loop control variables value is incremented or decremented. Then program controls the condition again to check whether it is true or not. If true, the loop body executed again. And the process is continued again until the condition evaluates to false. Consider the following code of the program: int x; for(x = 0; x < 10; x++) { printf(%d, x); } This for loop is executed for 10 times and prints the 0 to 9 digits in one line. In short the statements are executed in following sequence: 1. x = 0; 2. if(x<10) print value of x on screen 3. increment value of x by one 4. if(x<10) print value of x on screen 5. increment value of x by one When the condition becomes false, the loop gets terminated. For example if we want to print all the even numbers from 1 to 25 it can be written using for loop as: int i; for(i = 1;i < 25; i++) { if(i%2 == 0) printf(\n%d, i); } The flowchart of for loop can be shown as below:

Program to find the factorial of the number using for loop #include<stdio.h> main() { int fact, num; printf("Enter the number: "); scanf("%d", &num); for(fact=1 ; num>0 ; num--) fact = fact * num; printf("Factorial: %d", fact); } Nesting of the loops Nesting of the loop statements is allowed in C programming language. That is, we can write a for loop inside another for loop or any other loop in another loop statement. It can be written as follows: for(i = 0 ; i < 10 ; i++) { ...... ...... for(j = 0 ; j < 10 ; j++) { . . . . . . inner outer . . . . . . loop loop } ...... } The nesting may be continued up to any desired level. Here, for each iteration of outer loop inner loop completes its all iterations. In above example, the inner loop statements will be executed for 10 X 10 = 100 number of times. See one application of this concept. For example we require following output: * ** *** **** In this case, the loops must be nested as shown below: /* Example 2.8 */ #include<stdio.h> main() { int x,y; for(x = 0 ; x <= 3 ; x++) { for( y = 0 ; y <= x ; y++) printf("*"); printf("\n"); } }

You might also like