c if else
DESCRIPTION
If else of C Programming tutorialTRANSCRIPT
![Page 1: C if else](https://reader036.vdocuments.net/reader036/viewer/2022082805/54be30464a7959552d8b4572/html5/thumbnails/1.jpg)
C if else
Introduction:-
The if statement is used to conditionally execute a statement or a block of statements. Conditions can be true or false, execute one thing when the condition is true, something else when the condition is false.
![Page 2: C if else](https://reader036.vdocuments.net/reader036/viewer/2022082805/54be30464a7959552d8b4572/html5/thumbnails/2.jpg)
C if elseSyntax:-
if (expression) statement(s); [else statement(s);]
![Page 3: C if else](https://reader036.vdocuments.net/reader036/viewer/2022082805/54be30464a7959552d8b4572/html5/thumbnails/3.jpg)
C if else
Example :-
if (a == b) printf ("%d is equal to %d", a, b); else printf ("%d is not equal to %d", a, b);
![Page 4: C if else](https://reader036.vdocuments.net/reader036/viewer/2022082805/54be30464a7959552d8b4572/html5/thumbnails/4.jpg)
C if else
Syntax:-
if (expression) statement(s);
If-then statements :-
![Page 5: C if else](https://reader036.vdocuments.net/reader036/viewer/2022082805/54be30464a7959552d8b4572/html5/thumbnails/5.jpg)
C if else
Example:-
if (a == b) printf ("%d is equal to %d", a, b); else printf ("%d is not equal to %d", a, b);
Note : There is no indentation rule in writing C programming, we can write the above code in following ways :
If-then statements :-
![Page 6: C if else](https://reader036.vdocuments.net/reader036/viewer/2022082805/54be30464a7959552d8b4572/html5/thumbnails/6.jpg)
C if else
Example:- if (a == b) printf ("if a is equal to b"); printf ("%d is equal to %d", a, b);
Note : The second way of writing code is a good practice.
if (a == b) { printf ("if a is equal to b"); printf ("%d is equal to %d", a, b); }
If-then statements :-
![Page 7: C if else](https://reader036.vdocuments.net/reader036/viewer/2022082805/54be30464a7959552d8b4572/html5/thumbnails/7.jpg)
C if else
Example :- #include<stdio.h> main() { int num; printf("Input a number : "); scanf("%d",&num); if(num>0) { printf("This is a positive integer\n"); } else // else portion of if statement { printf(" This is not a positive integer..Try again\n"); } }
A complete example on conditional if-else statement:-
![Page 8: C if else](https://reader036.vdocuments.net/reader036/viewer/2022082805/54be30464a7959552d8b4572/html5/thumbnails/8.jpg)
C if else
Example :- #include<stdio.h> main() { int num; printf("Input a number : "); scanf("%d",&num); if(num>0) { printf("This is a positive integer\n"); } else // else portion of if statement { printf(" This is not a positive integer..Try again\n"); } }
A complete example on conditional if-else statement:-
![Page 9: C if else](https://reader036.vdocuments.net/reader036/viewer/2022082805/54be30464a7959552d8b4572/html5/thumbnails/9.jpg)
C if else
Example :-
if (a == b) printf ("a = b"); if (a == c) printf ("a = c"); if (b == c) printf ("b = c")
Sequential if-then statements :-
![Page 10: C if else](https://reader036.vdocuments.net/reader036/viewer/2022082805/54be30464a7959552d8b4572/html5/thumbnails/10.jpg)
C if else
syntax :- if (expression_1) statement_1 else if (expression_2) statement_2 . . . else if (expression_n) statement_n else other_statement
Multiway if-else-Statement :-
![Page 11: C if else](https://reader036.vdocuments.net/reader036/viewer/2022082805/54be30464a7959552d8b4572/html5/thumbnails/11.jpg)
C if else
Example :-
#include<stdio.h> main() { int num; printf("Input score :"); scanf("%d",&num); if (num>=90) { printf("Grade : Excellent"); } else if(num>=80 && num<90) { printf("Grade : Very Good"); } else if(num>=60 && num<80) { printf("Grade : Good"); } else if(num>=50 && num<60) { printf("Grade : Average"); } else if(num>=40 && num<50) { printf("Grade : Poor"); } else { printf("Grade : Not Promoted"); } }
Multiway if-else-Statement :-
![Page 12: C if else](https://reader036.vdocuments.net/reader036/viewer/2022082805/54be30464a7959552d8b4572/html5/thumbnails/12.jpg)
C if else
Example :- #include<stdio.h> main() { int num1=5, num2=3, num3=-12, min; if(num1<num2) { if(num1<num3) min = num1; else min = num3; } else { if(num2<num3) min = num2; else min = num3; } printf("Among %d, %d, %d minimum number is %d",num1,num2,num3,min); }
Nested if-then-else statements :-