Successfully reported this slideshow.
Your SlideShare is downloading. ×

conditional_statement.pdf

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Loading in …3
×

Check these out next

1 of 13 Ad

More Related Content

Recently uploaded (20)

Advertisement

conditional_statement.pdf

  1. 1. Control statement (if…. else) MD. SHOHEL ARMAN DEPT. OF SOFTWARE ENGINEERING
  2. 2. Algorithm •The solution of any computing problem involves a series of action in a specific order. •This procedure of solving problems is called algorithm.
  3. 3. Pseudocode •Pseudocode is an outline of a program, written in a form that can easily be converted into real programming statements •Pseudocode cannot be compiled nor executed, and there are no real formatting or syntax rules •It is simply one step - an important one - for producing the final code •The benefit of pseudocode is that it enables the programmer to concentrate on the algorithms without worrying about all the syntactic details of a particular programming language •You can write pseudocode without even knowing what programming language you will use for the final implementation
  4. 4. if statement Many times we have to take actions based on certain conditions. C provides if or if else statement to allow us to execute statements based on conditions. The if statement may be implemented in different forms depending on the complexity of the conditions to be tested. The forms are: 1. Simple if statement 2. If……… else statements 3. Nested if ……else statements 4. else if ladder
  5. 5. Forms of if…. else Statements ❑Simple if statement: if ( test expression) { Statement block; } ❑if…else statement: if ( test expression) { Statement block; } else { Statement block; } ❑nested if…else statement: if ( test expression) { if ( test expression) Statement block; else Statement block; } else { statement block; } ❑if…else ladder: if ( test expression) statement-1; else if ( test expression) Statement-2; else if(test expression) Statement-3; else statement-4;
  6. 6. Before going farther we need to review the Relational Operators and Logical Operators : Relational Operators Logical Operators C has following logical operators: • AND expressed by && • OR expressed by || • NOT expressed by !
  7. 7. Pseudocode //grade greater then 40 print pass else print failed: ……….. if (grade>40) { printf (“Passed n”); } Else { printf (“Failed n”); } ……..
  8. 8. Simple if Statement A simple if structure is used to choose among alternative courses of action if student’s grade is more than 40 print “passed” next pseudocode statement In this pseudocode, if grade of a student is more than 40 then the print command will be executed. Otherwise, if the student’s grade is not more than 40, the compiler will move to next pseudocode statement
  9. 9. Simple if Statement So, as a general form, we can see the if selection or simple if structure as- if (condition){ body of if } The condition needs to be true to get into the body of if. Otherwise, the compiler will go to the next segment of codes The pseudocode can be written in C as- if (grade>40){ printf (“Passed n”); }
  10. 10. If…else Statement he if…else structure allows the programmer to specify that different actions are to be performed when the condition is true and when the condition is false if student’s grade is more than 40 print “passed” else print “failed” If the condition of if is true, then compiler will print passed and if the condition of if is false, the compiler will print failed.
  11. 11. If…else Statement So, as a general form, we can see the if/else selection structure as- if (condition){ body of if } else{ body of else }
  12. 12. If…else Statement The pseudocode can be written in C as- if (grade>40) { printf (“Passed n”); } Else { printf (“Failed n”); } ❑An if structure may not have any else statement followed by it but an else structure must have a if structure preceded.
  13. 13. Algorithm: if student’s grade is more than 90 print “Grade: A” else if student’s grade is more than 80 print “Grade: B” else if student’s grade is more than 70 print “Grade: C” else if student’s grade is more than 60 print “Grade: D” else if student’s grade is more than 50 print “Grade: E” else print “Grade: F” Using if…else statement C-code: #include<stdio.h> void main(){ int grade; printf("Enter your grade: "); scanf("%d",&grade); if (grade>90) printf("Grade: A"); else if (grade>80) printf("Grade: B"); else if (grade>70) printf("Grade: C"); else if (grade>60) printf("Grade: D"); else if (grade>50) printf("Grade: E"); else printf("Grade: F"); getch();

×