Sei sulla pagina 1di 15

As I mention my approach is toward the mu control statement-if else /*vivek*/

Until now we study ,we give command and it takes value from keyboard and run the program but here something new .now we learn how to give particular direction to our program using some condional statements This is a conditional statement used in C to check condition or to control the flow of execution of statements. This is also called as 'decision making statement or control statement.' The execution of a whole program is done in one direction only. 1)If Syntax:if (condition) { statements; } in above syntax, the condition is checked first. If it is true, then the program control flow goes inside the braces and executes the block of statements associated with it. If it returns false, then program skips the

braces. If there are more than 1 (one) statements in if statement then use { } braces else it is not necessary to use. Lets understand by flow chart

1) enter the salary, if salary is less than50, 000 then give 20% salary as bonus.

#include<stdio.h> main( ) { float x,bonus_rate=0; printf("enter the salary:-"); scanf("%f",&x); if(x<5000) bonus_rate =20; x=x+x*bonus_rate /100;

printf("now your salary is %f",x); }

input:- 3000 Output :-3600 Input:-5000 Output:-5000

Live image of output :-

How it works - Step by step 1)initialize the variable and bonus rate 2)take the value from keyboard 3)if the value is less than 5000 then program run as it is and take bonus rate as 20and perform the calculation and print the calculated value

4)if the value is more than 5000 if condition jump one statement and for computer one statement is nothing but one semicolon ( ; ) .therefore it jump the statement bonus_rate =20; and go to next statement here x=x+x*bonus_rate /100; but if condition goes false therefore it takes the bonus rate as 0 and perform calculation and give the output. C in my words 1) Dont put ; after if statement. If statement is conditional statement if condition is true it go as it is but if condition goes wrong then it jump one statement and go as it is .but jumping of one statement is nothing but jumping of one semicolon because in computer language line is complete with ; so if we put ; after it nullify the effect of using if because it jump it jump if statement itself considering as statement then 20% bonus_rate is given to all 2)initialization of bonus_rate=0 is another key statement . What happen if someone not initializes the bonus rate ? Ans In some compiler the bonus_rate is directly initialized to 0 but in some compiler it is not . In this case bonus_rate can take any garbage value and make calculation .so I prefer better to initialize it as 0 to avoid error. Multiple Statements within if 2)write program to print the result. #include<stdio.h> Void main( ) { Int result ; printf("enter the mark:-"); scanf ( "%d ", & result) ; if (result >=40) { printf("Passed\n"); printf("Congratulations\n") } }

Live output:-

As I mention above If there are more than 1 (one) statements in if statement then use { } braces else it is not necessary to use. this program is just ex for that . C in my word:1) Here when we write more the one statement using {} braces then whole block of brace {} consider as one statement and print as it is.

2)If. Else The same above program we can also write using if else

#include<stdio.h> main( ) { float x; printf("enter the mark:-"); scanf("%f",&x); if(x>40) { printf("Passed\n"); printf("Congratulations\n"); }

else { printf("Failed\n"); printf("Good luck in the resist\n"); } } Live output-

How it works- step by step 1)First step is initialization of var. 2)enter the marks 3)if your marks is above 40 then if condition is true and it print block of braces {} and program stop there and no further execution but if someone give value less than 40 then if statement become false and the whole block of braces {}is jumped because it is consider as one statement And program is further executed and directly print whole block of brace bracket as it is. C in my word :when you are going for bigger programming making a small mistake it irritate you therefore you to avoid such mistake

You improve yourself form the beginning of learning. If you are careful in reading you can observe the way I put the braces {}.I suggest you to after writing if condition give tab and then put the {} it avoid the confusion and if we make any mistake we can correct easily. Nested if statements If you use an if statement in an if statement it is called nesting. Nesting if statements can make a program very complex, but sometimes there is no other way. So use it wisely. Take a look at a nested if statement example below: Enter the grade if grade is liss then 10 print fail if grade is between 10 and 60 print pass and if grade is more than 60 print very good #include<stdio.h> void main() { int grade; scanf("%d",&grade); if(grade<=10) { printf("you are fail"); } else { if(grade<=60) { printf("you are pass"); } else { printf("very good"); } } } Note: Again take a look at the placing of the braces { } and the placing of the indentations. and take care that how many brackets you open should be closed. How it work- step by step

1)If the grade is equal or below ten program stop there and print you are fail. 2)If the grade is above ten, the program will go into the else statement. In the else statement there is another if statement (this is called as nesting). This if statement checks the grade again. If the grade is below sixty, you must study harder. In the else statement from this if statement there is another if statement. It checks whether the grade is above or equal to sixty. If it is, you passed the test. 3)if grade is more than 60 then another else come in operation which print output as very good case(1): If grade is below 10 only first if is evaluate Live output-

Case (2):If grade is between 10 to 60 program move to else because first if condition false but inside the else another if.else looping(i.e. nested if else) Inside the else compiler move to if condition here this if is satisfy

Live output-

Case(3): If grade is more than 60 program move to else because first if condition is false but inside the else another if.else looping(i.e. nested if else) Inside the else compiler move to if condition here this if condition is also fails therefore compiler move to else this else statement is satisfy Live output-

Multiple condition operators C three logical operators, namely, &&, || and !. These are to be read as AND OR and NOT respectively. There are several things to note about these logical operators. Most obviously, two of them are composed of double symbols: || and &&. Dont use the single symbol | and &. These single symbols also have a meaning. They are bitwise operators Write the above grade code program && and || also #include<stdio.h> void main() { int grade; printf("enter the grade:- "); scanf("%d",&grade);

if(grade<=10) { printf("you are fail"); } if((grade>10)&&(grade<=60)) { printf("you are pass"); } if (grade>75) { printf("very good"); } } C in my words 1)here we are not using else 2)for operator && we have use in bracket properly Ex: If ( (grade>10)&&(grade<=60) ) Live output-

With the OR ( || ) operator you can test if one of two conditions are true. Example:| If ( a=10 || b<20 ) If a equals ten or b is smaller than twenty then do something. So if a or b is true, something happens. 3)The else if Clause Write above grade program using if else clause

#include<stdio.h> void main() { int grade; scanf("%d",&grade); if(grade<=10) {

printf("you are fail"); } else if (grade<=60) { printf("you are pass"); } else { printf("very good"); } } C in my words : Else if clause :- You can note that this program reduces the indentation of the statements. In this case every else is associated with its previous if. The last else goes to work only if all the conditions fail. Even in else if ladder the last else is optional. Note: that the else if clause is nothing different. It is just a way of rearranging the else with the if that follows it. This would be evident if you look at the following code: If else if(grade<=10) { printf("you are fail"); } else { If (grade<=60) { printf("you are pass"); } else { printf("very good"); Else if clause if(grade<=10) { printf("you are fail"); } else if (grade<=60) { printf("you are pass"); } else { printf("very good"); }

} } } From the comparison you can see that else { If is replace by else if

4)Nested if Write program to check given no is divisible by 5 or 2 or both #include <stdio.h> #include<conio.h> void main() { clrscr(); int n; scanf("%d",&n); if(n%5==0) /*vivek*/ { if(n%2==0)

{ printf("\n divisible 2"); } else { printf("\n not divisible 2"); } printf("\n divisible 5"); } else { printf("\n not divisible 5"); } getch(); } /*vivek*/

Potrebbero piacerti anche