C if else

  • 1,337 views
Uploaded on

If else of C Programming tutorial

If else of C Programming tutorial

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
1,337
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
41
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. C if elseIntroduction:-The if statement is used to conditionally execute a statement or ablock of statements. Conditions can be true or false, execute onething when the condition is true, something else when thecondition is false.
  • 2. C if elseSyntax:- if (expression) statement(s); [else statement(s);]
  • 3. C if elseExample :- if (a == b) printf ("%d is equal to %d", a, b); else printf ("%d is not equal to %d", a, b);
  • 4. C if elseIf-then statements :-Syntax:- if (expression) statement(s);
  • 5. C if elseIf-then statements :-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 abovecode in following ways :
  • 6. C if elseIf-then statements :-Example:- if (a == b) printf ("if a is equal to b"); printf ("%d is equal to %d", a, b); 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.
  • 7. C if elseA complete example on conditional if-else statement:-Example :- #include<stdio.h> main() { int num; printf("Input a number : "); scanf("%d",&num); if(num>0) { printf("This is a positive integern"); } else // else portion of if statement { printf(" This is not a positive integer..Try againn") ; } }
  • 8. C if elseA complete example on conditional if-else statement:-Example :- #include<stdio.h> main() { int num; printf("Input a number : "); scanf("%d",&num); if(num>0) { printf("This is a positive integern"); } else // else portion of if statement { printf(" This is not a positive integer..Try againn") ; } }
  • 9. C if elseSequential if-then statements :-Example :- if (a == b) printf ("a = b"); if (a == c) printf ("a = c"); if (b == c) printf ("b = c")
  • 10. C if elseMultiway if-else-Statement :-syntax :- if (expression_1) statement_1 else if (expression_2) statement_2 . . . else if (expression_n) statement_n else other_statement
  • 11. C if elseMultiway if-else-Statement :- #include<stdio.h> main()Example :- { 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"); } }
  • 12. C if elseNested if-then-else statements :-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); }