Upcoming SlideShare
×

# C-04

187 views

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
187
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
5
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.