SlideShare uma empresa Scribd logo
1 de 22
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
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.
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?
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
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
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
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
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”);
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.
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
}}
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”);
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.
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.
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”
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();
}}
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-
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();
}}
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
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
20Rushdi Shams, Dept of CSE, KUET, Bangladesh
Increment and Decrement OperatorsIncrement and Decrement Operators
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.
22Rushdi Shams, Dept of CSE, KUET, Bangladesh
Increment and Decrement OperatorsIncrement and Decrement Operators

Mais conteúdo relacionado

Semelhante a Lec 04. If-Else Statement / Increment and Decrement Operators

Lec 03. Arithmetic Operator / Relational Operator
Lec 03. Arithmetic Operator / Relational OperatorLec 03. Arithmetic Operator / Relational Operator
Lec 03. Arithmetic Operator / Relational OperatorRushdi Shams
 
Lec 05. While Loop
Lec 05. While LoopLec 05. While Loop
Lec 05. While LoopRushdi Shams
 
[ITP - Lecture 08] Decision Control Structures (If Statement)
[ITP - Lecture 08] Decision Control Structures (If Statement)[ITP - Lecture 08] Decision Control Structures (If Statement)
[ITP - Lecture 08] Decision Control Structures (If Statement)Muhammad Hammad Waseem
 
Programming Fundamentals Decisions
Programming Fundamentals  Decisions Programming Fundamentals  Decisions
Programming Fundamentals Decisions imtiazalijoono
 
Conditional Statement
Conditional Statement Conditional Statement
Conditional Statement OXUS 20
 
Chapter 4 flow control structures and arrays
Chapter 4 flow control structures and arraysChapter 4 flow control structures and arrays
Chapter 4 flow control structures and arrayssshhzap
 
Fcp chapter 2 upto_if_else (2)
Fcp chapter 2 upto_if_else (2)Fcp chapter 2 upto_if_else (2)
Fcp chapter 2 upto_if_else (2)Jagdish Kamble
 
7 programming-using-java decision-making220102011
7 programming-using-java decision-making2201020117 programming-using-java decision-making220102011
7 programming-using-java decision-making220102011Mahmoud Alfarra
 
programming c language.
programming c language. programming c language.
programming c language. Abdul Rehman
 
Lec 09. Introduction to Functions / Call by Values
Lec 09. Introduction to Functions / Call by ValuesLec 09. Introduction to Functions / Call by Values
Lec 09. Introduction to Functions / Call by ValuesRushdi Shams
 
C Control Statements.docx
C Control Statements.docxC Control Statements.docx
C Control Statements.docxJavvajiVenkat
 
Programming Basics if then else, switch, operators
Programming Basics if then else, switch, operatorsProgramming Basics if then else, switch, operators
Programming Basics if then else, switch, operatorsTrivuz ত্রিভুজ
 
#OOP_D_ITS - 3rd - Migration From C To C++
#OOP_D_ITS - 3rd - Migration From C To C++#OOP_D_ITS - 3rd - Migration From C To C++
#OOP_D_ITS - 3rd - Migration From C To C++Hadziq Fabroyir
 

Semelhante a Lec 04. If-Else Statement / Increment and Decrement Operators (20)

Lec 03. Arithmetic Operator / Relational Operator
Lec 03. Arithmetic Operator / Relational OperatorLec 03. Arithmetic Operator / Relational Operator
Lec 03. Arithmetic Operator / Relational Operator
 
Lec 05. While Loop
Lec 05. While LoopLec 05. While Loop
Lec 05. While Loop
 
jhtp9_ch04.ppt
jhtp9_ch04.pptjhtp9_ch04.ppt
jhtp9_ch04.ppt
 
[ITP - Lecture 08] Decision Control Structures (If Statement)
[ITP - Lecture 08] Decision Control Structures (If Statement)[ITP - Lecture 08] Decision Control Structures (If Statement)
[ITP - Lecture 08] Decision Control Structures (If Statement)
 
Programming Fundamentals Decisions
Programming Fundamentals  Decisions Programming Fundamentals  Decisions
Programming Fundamentals Decisions
 
control statement
control statement control statement
control statement
 
Conditional Statement
Conditional Statement Conditional Statement
Conditional Statement
 
Chapter 4 flow control structures and arrays
Chapter 4 flow control structures and arraysChapter 4 flow control structures and arrays
Chapter 4 flow control structures and arrays
 
03slide.ppt
03slide.ppt03slide.ppt
03slide.ppt
 
175035 cse lab-03
175035 cse lab-03175035 cse lab-03
175035 cse lab-03
 
Ch3.1
Ch3.1Ch3.1
Ch3.1
 
ICP - Lecture 7 and 8
ICP - Lecture 7 and 8ICP - Lecture 7 and 8
ICP - Lecture 7 and 8
 
Fcp chapter 2 upto_if_else (2)
Fcp chapter 2 upto_if_else (2)Fcp chapter 2 upto_if_else (2)
Fcp chapter 2 upto_if_else (2)
 
7 programming-using-java decision-making220102011
7 programming-using-java decision-making2201020117 programming-using-java decision-making220102011
7 programming-using-java decision-making220102011
 
programming c language.
programming c language. programming c language.
programming c language.
 
Lec 09. Introduction to Functions / Call by Values
Lec 09. Introduction to Functions / Call by ValuesLec 09. Introduction to Functions / Call by Values
Lec 09. Introduction to Functions / Call by Values
 
C Control Statements.docx
C Control Statements.docxC Control Statements.docx
C Control Statements.docx
 
Programming Basics if then else, switch, operators
Programming Basics if then else, switch, operatorsProgramming Basics if then else, switch, operators
Programming Basics if then else, switch, operators
 
#OOP_D_ITS - 3rd - Migration From C To C++
#OOP_D_ITS - 3rd - Migration From C To C++#OOP_D_ITS - 3rd - Migration From C To C++
#OOP_D_ITS - 3rd - Migration From C To C++
 
Control Statement programming
Control Statement programmingControl Statement programming
Control Statement programming
 

Mais de Rushdi Shams

Research Methodology and Tips on Better Research
Research Methodology and Tips on Better ResearchResearch Methodology and Tips on Better Research
Research Methodology and Tips on Better ResearchRushdi Shams
 
Common evaluation measures in NLP and IR
Common evaluation measures in NLP and IRCommon evaluation measures in NLP and IR
Common evaluation measures in NLP and IRRushdi Shams
 
Machine learning with nlp 101
Machine learning with nlp 101Machine learning with nlp 101
Machine learning with nlp 101Rushdi Shams
 
Semi-supervised classification for natural language processing
Semi-supervised classification for natural language processingSemi-supervised classification for natural language processing
Semi-supervised classification for natural language processingRushdi Shams
 
Natural Language Processing: Parsing
Natural Language Processing: ParsingNatural Language Processing: Parsing
Natural Language Processing: ParsingRushdi Shams
 
Types of machine translation
Types of machine translationTypes of machine translation
Types of machine translationRushdi Shams
 
L1 l2 l3 introduction to machine translation
L1 l2 l3  introduction to machine translationL1 l2 l3  introduction to machine translation
L1 l2 l3 introduction to machine translationRushdi Shams
 
Syntax and semantics
Syntax and semanticsSyntax and semantics
Syntax and semanticsRushdi Shams
 
Propositional logic
Propositional logicPropositional logic
Propositional logicRushdi Shams
 
Probabilistic logic
Probabilistic logicProbabilistic logic
Probabilistic logicRushdi Shams
 
Knowledge structure
Knowledge structureKnowledge structure
Knowledge structureRushdi Shams
 
Knowledge representation
Knowledge representationKnowledge representation
Knowledge representationRushdi Shams
 
L5 understanding hacking
L5  understanding hackingL5  understanding hacking
L5 understanding hackingRushdi Shams
 
L2 Intrusion Detection System (IDS)
L2  Intrusion Detection System (IDS)L2  Intrusion Detection System (IDS)
L2 Intrusion Detection System (IDS)Rushdi Shams
 

Mais de Rushdi Shams (20)

Research Methodology and Tips on Better Research
Research Methodology and Tips on Better ResearchResearch Methodology and Tips on Better Research
Research Methodology and Tips on Better Research
 
Common evaluation measures in NLP and IR
Common evaluation measures in NLP and IRCommon evaluation measures in NLP and IR
Common evaluation measures in NLP and IR
 
Machine learning with nlp 101
Machine learning with nlp 101Machine learning with nlp 101
Machine learning with nlp 101
 
Semi-supervised classification for natural language processing
Semi-supervised classification for natural language processingSemi-supervised classification for natural language processing
Semi-supervised classification for natural language processing
 
Natural Language Processing: Parsing
Natural Language Processing: ParsingNatural Language Processing: Parsing
Natural Language Processing: Parsing
 
Types of machine translation
Types of machine translationTypes of machine translation
Types of machine translation
 
L1 l2 l3 introduction to machine translation
L1 l2 l3  introduction to machine translationL1 l2 l3  introduction to machine translation
L1 l2 l3 introduction to machine translation
 
Syntax and semantics
Syntax and semanticsSyntax and semantics
Syntax and semantics
 
Propositional logic
Propositional logicPropositional logic
Propositional logic
 
Probabilistic logic
Probabilistic logicProbabilistic logic
Probabilistic logic
 
L15 fuzzy logic
L15  fuzzy logicL15  fuzzy logic
L15 fuzzy logic
 
Knowledge structure
Knowledge structureKnowledge structure
Knowledge structure
 
Knowledge representation
Knowledge representationKnowledge representation
Knowledge representation
 
First order logic
First order logicFirst order logic
First order logic
 
Belief function
Belief functionBelief function
Belief function
 
L5 understanding hacking
L5  understanding hackingL5  understanding hacking
L5 understanding hacking
 
L4 vpn
L4  vpnL4  vpn
L4 vpn
 
L3 defense
L3  defenseL3  defense
L3 defense
 
L2 Intrusion Detection System (IDS)
L2  Intrusion Detection System (IDS)L2  Intrusion Detection System (IDS)
L2 Intrusion Detection System (IDS)
 
L1 phishing
L1  phishingL1  phishing
L1 phishing
 

Último

Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 

Último (20)

Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 

Lec 04. If-Else Statement / Increment and Decrement Operators

  • 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 20Rushdi Shams, Dept of CSE, KUET, Bangladesh Increment and Decrement OperatorsIncrement and Decrement Operators
  • 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. 22Rushdi Shams, Dept of CSE, KUET, Bangladesh Increment and Decrement OperatorsIncrement and Decrement Operators