Upload
rushdi-shams
View
53
Download
6
Tags:
Embed Size (px)
Citation preview
Lecture 4Lecture 4Version 1.0Version 1.0
AlgorithmAlgorithm
PseudocodePseudocode
The if StructureThe if Structure
The if/else StructureThe if/else Structure
The if/else if/else The if/else if/else StructureStructure
Increment/Decrement Increment/Decrement OperatorsOperators
2Rushdi Shams, Dept of CSE, KUET, Bangladesh
AlgorithmAlgorithm
The solution of any computing The solution of any computing problem involves a series of action in problem involves a series of action in a specific order. a specific order.
This procedure of solving problems This procedure of solving problems is called algorithm. is called algorithm.
3Rushdi Shams, Dept of CSE, KUET, Bangladesh
AlgorithmAlgorithm
What are the procedures you follow What are the procedures you follow before you come to the class?before you come to the class?
4Rushdi Shams, Dept of CSE, KUET, Bangladesh
PseudocodePseudocode
Pseudocode is an outline of a Pseudocode is an outline of a programprogram, written in a form that can , written in a form that can easily be converted into real easily be converted into real programming programming statementsstatements
Pseudocode cannot be Pseudocode cannot be compiledcompiled nor nor executedexecuted, and there are no real , and there are no real formatting or syntax rules formatting or syntax rules
It is simply one step - an important It is simply one step - an important one - in producing the final one - in producing the final codecode
5Rushdi Shams, Dept of CSE, KUET, Bangladesh
PseudocodePseudocode
The benefit of pseudocode is that it The benefit of pseudocode is that it enables the enables the programmerprogrammer to to concentrate on the concentrate on the algorithmsalgorithms without without worrying about all the syntactic details worrying about all the syntactic details of a particular of a particular programming languageprogramming language
You can write pseudocode without You can write pseudocode without even knowing what programming even knowing what programming language you will use for the final language you will use for the final implementation implementation
6Rushdi Shams, Dept of CSE, KUET, Bangladesh
The if Selection The if Selection Structure Structure
A selection structure is used to choose A selection structure is used to choose among alternative courses of action among alternative courses of action
if if student’s grade is more than 40student’s grade is more than 40
print print “passed”“passed”
next pseudocode statementnext pseudocode statement
In this pseudocode, if grade of a student is In this pseudocode, if grade of a student is more than 40 then the more than 40 then the print commandprint command will will be executed. Otherwise, if the student’s be executed. Otherwise, if the student’s grade is not more than 40, the compiler grade is not more than 40, the compiler will move to will move to next pseudocode statementnext pseudocode statement
7Rushdi Shams, Dept of CSE, KUET, Bangladesh
The if Selection The if Selection Structure Structure
So, as a general form, we can see the if So, as a general form, we can see the if selection structure as-selection structure as-
if (condition){if (condition){
body of ifbody of if
}}
The The conditioncondition needs to be true to get needs to be true to get into theinto the body of if body of if. Otherwise, the . Otherwise, the compiler will go to the next segment of compiler will go to the next segment of codes codes
8Rushdi Shams, Dept of CSE, KUET, Bangladesh
The if Selection The if Selection Structure Structure
The pseudocode can be written in C The pseudocode can be written in C as-as-
if (grade>40)if (grade>40)
printf (“Passed \n”);printf (“Passed \n”);
9Rushdi Shams, Dept of CSE, KUET, Bangladesh
The if/else Selection The if/else Selection StructureStructure
The if/else structure allows the programmer The if/else structure allows the programmer to specify that different actions are to be to specify that different actions are to be performed when the condition is true and performed when the condition is true and when the condition is falsewhen the condition is false
if if student’s grade is more than 40student’s grade is more than 40print print “passed”“passed”
elseelseprint print “failed”“failed”
If the If the conditioncondition of if is true, then compiler of if is true, then compiler will print passed and if the will print passed and if the conditioncondition of if is of if is false, the compiler will print failed. false, the compiler will print failed.
10Rushdi Shams, Dept of CSE, KUET, Bangladesh
The if/else Selection The if/else Selection StructureStructure
So, as a general form, we can see So, as a general form, we can see the if/else selection structure as-the if/else selection structure as-
if (condition){if (condition){
body of ifbody of if
}}
else{else{
body of elsebody of else
}}
11Rushdi Shams, Dept of CSE, KUET, Bangladesh
The if/else Selection The if/else Selection StructureStructure
The pseudocode can be written in C The pseudocode can be written in C as-as-
if (grade>40)if (grade>40)
printf (“Passed \n”);printf (“Passed \n”);
elseelse
printf (“Failed \n”);printf (“Failed \n”);
12Rushdi Shams, Dept of CSE, KUET, Bangladesh
The if/else Selection The if/else Selection StructureStructure
An if structure may not have any An if structure may not have any else statement followed by it but an else statement followed by it but an else structure must have a if else structure must have a if structure preceded.structure preceded.
13Rushdi Shams, Dept of CSE, KUET, Bangladesh
The if/else if/else Selection The if/else if/else Selection StructureStructure
Now, consider a big scenario where Now, consider a big scenario where you may require a nested if else you may require a nested if else structure.structure.
14Rushdi Shams, Dept of CSE, KUET, Bangladesh
The if/else if/else Selection The if/else if/else Selection StructureStructure
if if student’s grade is more than 90student’s grade is more than 90print print “Grade: A”“Grade: A”
elseelseif if student’s grade is more than 80student’s grade is more than 80print print “Grade: B”“Grade: B”
elseelseif if student’s grade is more than 70student’s grade is more than 70print print “Grade: C”“Grade: C”
elseelseif if student’s grade is more than 60student’s grade is more than 60print print “Grade: D”“Grade: D”
elseelseif if student’s grade is more than 50student’s grade is more than 50print print “Grade: E”“Grade: E”
elseelseprint print “Grade: F”“Grade: F”
15Rushdi Shams, Dept of CSE, KUET, Bangladesh
The if/else if/else Selection The 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();
}}
16Rushdi Shams, Dept of CSE, KUET, Bangladesh
The if/else if/else Selection The if/else if/else Selection StructureStructure
Now, guess what happens if we Now, guess what happens if we replace the above code as follows-replace the above code as follows-
17Rushdi Shams, Dept of CSE, KUET, Bangladesh
The if/else if/else Selection The 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();
}}
18Rushdi Shams, Dept of CSE, KUET, Bangladesh
The if/else if/else Selection The if/else if/else Selection StructureStructure
This program will output This program will output Grade: C Grade: C Grade: D Grade: EGrade: D Grade: E- this is not - this is not desirable desirable
19Rushdi Shams, Dept of CSE, KUET, Bangladesh
Increment and Decrement Increment and Decrement OperatorsOperators
C provides the unary increment C provides the unary increment operator ++ and unary decrement operator ++ and unary decrement operator –operator –
If a variable a is incremented by 1, If a variable a is incremented by 1, the increment operator ++ can be the increment operator ++ can be used instead of expression a=a+1used instead of expression a=a+1
But the meaning differs based on But the meaning differs based on the place of the operator to the the place of the operator to the variable variable
20Rushdi Shams, Dept of CSE, KUET, Bangladesh
Increment and Decrement Increment and Decrement OperatorsOperators
21Rushdi Shams, Dept of CSE, KUET, Bangladesh
Increment and Decrement Increment and Decrement OperatorsOperators
If unary increment/ decrement operators If unary increment/ decrement operators are placed in front of the variable, they are placed in front of the variable, they are called preincrement/ predecrement are called preincrement/ predecrement operators otherwise they are called operators otherwise they are called postincrement/ postdecrement operators.postincrement/ postdecrement operators.
22Rushdi Shams, Dept of CSE, KUET, Bangladesh
Increment and Decrement Increment and Decrement OperatorsOperators