C-04

187 views

Published on

Published in: Education, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
187
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

C-04

  1. 1. Lecture 4Lecture 4 Version 1.0Version 1.0 AlgorithmAlgorithm PseudocodePseudocode The if StructureThe if Structure The if/else StructureThe if/else Structure The if/else if/else StructureThe if/else if/else Structure Increment/Decrement OperatorsIncrement/Decrement Operators
  2. 2. 2Rushdi Shams, Dept of CSE, KUET, Bangladesh AlgorithmAlgorithm  The solution of any computing problemThe solution of any computing problem involves a series of action in a specific order.involves a series of action in a specific order.  This procedure of solving problems is calledThis procedure of solving problems is called algorithm.algorithm.
  3. 3. 3Rushdi Shams, Dept of CSE, KUET, Bangladesh AlgorithmAlgorithm  What are the procedures you follow before youWhat are the procedures you follow before you come to the class?come to the class?
  4. 4. 4Rushdi Shams, Dept of CSE, KUET, Bangladesh PseudocodePseudocode  Pseudocode is an outline of aPseudocode is an outline of a programprogram, written, written in a form that can easily be converted into realin a form that can easily be converted into real programmingprogramming statementsstatements  Pseudocode cannot bePseudocode cannot be compiledcompiled nornor executedexecuted,, and there are no real formatting or syntax rulesand there are no real formatting or syntax rules  It is simply one step - an important one - inIt is simply one step - an important one - in producing the finalproducing the final codecode
  5. 5. 5Rushdi Shams, Dept of CSE, KUET, Bangladesh PseudocodePseudocode  The benefit of pseudocode is that it enables theThe benefit of pseudocode is that it enables the programmerprogrammer to concentrate on theto concentrate on the algorithmsalgorithms without worrying about all the syntactic detailswithout worrying about all the syntactic details of a particularof a particular programming languageprogramming language  You can write pseudocode without evenYou can write pseudocode without even knowing what programming language you willknowing what programming language you will use for the final implementationuse for the final implementation
  6. 6. 6Rushdi Shams, Dept of CSE, KUET, Bangladesh The if Selection StructureThe if Selection Structure  A selection structure is used to choose amongA selection structure is used to choose among alternative courses of actionalternative courses of action ifif student’s grade is more than 40student’s grade is more than 40 printprint “passed”“passed” next pseudocode statementnext pseudocode statement  In this pseudocode, if grade of a student is more thanIn this pseudocode, if grade of a student is more than 40 then the40 then the print commandprint command will be executed. Otherwise, ifwill be executed. Otherwise, if the student’s grade is not more than 40, the compilerthe student’s grade is not more than 40, the compiler will move towill move to next pseudocode statementnext pseudocode statement
  7. 7. 7Rushdi Shams, Dept of CSE, KUET, Bangladesh The if Selection StructureThe if Selection Structure  So, as a general form, we can see the if selectionSo, as a general form, we can see the if selection structure as-structure as- if (condition){if (condition){ body of ifbody of if }}  TheThe conditioncondition needs to be true to get into theneeds to be true to get into the bodybody of ifof if. Otherwise, the compiler will go to the next. Otherwise, the compiler will go to the next segment of codessegment of codes
  8. 8. 8Rushdi Shams, Dept of CSE, KUET, Bangladesh The if Selection StructureThe if Selection Structure  The pseudocode can be written in C as-The pseudocode can be written in C as- if (grade>40)if (grade>40) printf (“Passed n”);printf (“Passed n”);
  9. 9. 9Rushdi Shams, Dept of CSE, KUET, Bangladesh The if/else Selection StructureThe if/else Selection Structure  The if/else structure allows the programmer to specifyThe if/else structure allows the programmer to specify that different actions are to be performed when thethat different actions are to be performed when the condition is true and when the condition is falsecondition is true and when the condition is false ifif student’s grade is more than 40student’s grade is more than 40 printprint “passed”“passed” elseelse printprint “failed”“failed”  If theIf the conditioncondition of if is true, then compiler will printof if is true, then compiler will print passed and if thepassed and if the conditioncondition of if is false, the compiler willof if is false, the compiler will print failed.print failed.
  10. 10. 10Rushdi Shams, Dept of CSE, KUET, Bangladesh The if/else Selection StructureThe if/else Selection Structure  So, as a general form, we can see the if/elseSo, as a general form, we can see the if/else selection structure as-selection structure as- if (condition){if (condition){ body of ifbody of if }} else{else{ body of elsebody of else }}
  11. 11. 11Rushdi Shams, Dept of CSE, KUET, Bangladesh The if/else Selection StructureThe if/else Selection Structure  The pseudocode can be written in C as-The pseudocode can be written in C as- if (grade>40)if (grade>40) printf (“Passed n”);printf (“Passed n”); elseelse printf (“Failed n”);printf (“Failed n”);
  12. 12. 12Rushdi Shams, Dept of CSE, KUET, Bangladesh The if/else Selection StructureThe if/else Selection Structure  An if structure may not have any else statementAn if structure may not have any else statement followed by it but an else structure must have afollowed by it but an else structure must have a if structure preceded.if structure preceded.
  13. 13. 13Rushdi Shams, Dept of CSE, KUET, Bangladesh The if/else if/else SelectionThe if/else if/else Selection StructureStructure  Now, consider a big scenario where you mayNow, consider a big scenario where you may require a nested if else structure.require a nested if else structure.
  14. 14. 14Rushdi Shams, Dept of CSE, KUET, Bangladesh The if/else if/else SelectionThe if/else if/else Selection StructureStructure ifif student’s grade is more than 90student’s grade is more than 90 printprint “Grade: A”“Grade: A” elseelse ifif student’s grade is more than 80student’s grade is more than 80 printprint “Grade: B”“Grade: B” elseelse ifif student’s grade is more than 70student’s grade is more than 70 printprint “Grade: C”“Grade: C” elseelse ifif student’s grade is more than 60student’s grade is more than 60 printprint “Grade: D”“Grade: D” elseelse ifif student’s grade is more than 50student’s grade is more than 50 printprint “Grade: E”“Grade: E” elseelse printprint “Grade: F”“Grade: F”
  15. 15. 15Rushdi Shams, Dept of CSE, KUET, Bangladesh The if/else if/else SelectionThe if/else if/else Selection StructureStructure #include<stdio.h>#include<stdio.h> #include<conio.h>#include<conio.h> void main(){void main(){ clrscr();clrscr(); int grade;int grade; printf("Enter your grade: ");printf("Enter your grade: "); scanf("%d",&grade);scanf("%d",&grade); if (grade>90)if (grade>90) printf("Grade: A");printf("Grade: A"); else if (grade>80)else if (grade>80) printf("Grade: B");printf("Grade: B"); else if (grade>70)else if (grade>70) printf("Grade: C");printf("Grade: C"); else if (grade>60)else if (grade>60) printf("Grade: D");printf("Grade: D"); else if (grade>50)else if (grade>50) printf("Grade: E");printf("Grade: E"); elseelse printf("Grade: F");printf("Grade: F"); getch();getch(); }}
  16. 16. 16Rushdi Shams, Dept of CSE, KUET, Bangladesh The if/else if/else SelectionThe if/else if/else Selection StructureStructure  Now, guess what happens if we replace theNow, guess what happens if we replace the above code as follows-above code as follows-
  17. 17. 17Rushdi Shams, Dept of CSE, KUET, Bangladesh The if/else if/else SelectionThe if/else if/else Selection StructureStructure #include<stdio.h>#include<stdio.h> #include<conio.h>#include<conio.h> void main(){void main(){ clrscr();clrscr(); int grade;int grade; printf("Enter your grade: ");printf("Enter your grade: "); scanf("%d",&grade);scanf("%d",&grade); if (grade>90)if (grade>90) printf("Grade: A");printf("Grade: A"); if (grade>80)if (grade>80) printf("Grade: B");printf("Grade: B"); if (grade>70)if (grade>70) printf("Grade: C");printf("Grade: C"); if (grade>60)if (grade>60) printf("Grade: D");printf("Grade: D"); if (grade>50)if (grade>50) printf("Grade: E");printf("Grade: E"); elseelse printf("Grade: F");printf("Grade: F"); getch();getch(); }}
  18. 18. 18Rushdi Shams, Dept of CSE, KUET, Bangladesh The if/else if/else SelectionThe if/else if/else Selection StructureStructure  This program will outputThis program will output Grade: C Grade: DGrade: C Grade: D Grade: EGrade: E- this is not desirable- this is not desirable
  19. 19. 19Rushdi Shams, Dept of CSE, KUET, Bangladesh Increment and Decrement OperatorsIncrement and Decrement Operators  C provides the unary increment operator ++C provides the unary increment operator ++ and unary decrement operator –and unary decrement operator –  If a variable a is incremented by 1, the incrementIf a variable a is incremented by 1, the increment operator ++ can be used instead of expressionoperator ++ can be used instead of expression a=a+1a=a+1  But the meaning differs based on the place ofBut the meaning differs based on the place of the operator to the variablethe operator to the variable
  20. 20. 20Rushdi Shams, Dept of CSE, KUET, Bangladesh Increment and Decrement OperatorsIncrement and Decrement Operators
  21. 21. 21Rushdi Shams, Dept of CSE, KUET, Bangladesh Increment and Decrement OperatorsIncrement and Decrement Operators  If unary increment/ decrement operators are placed inIf unary increment/ decrement operators are placed in front of the variable, they are called preincrement/front of the variable, they are called preincrement/ predecrement operators otherwise they are calledpredecrement operators otherwise they are called postincrement/ postdecrement operators.postincrement/ postdecrement operators.
  22. 22. 22Rushdi Shams, Dept of CSE, KUET, Bangladesh Increment and Decrement OperatorsIncrement and Decrement Operators

×