SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Ähnlich wie 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
 

Ähnlich wie 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
 

Mehr von 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
 

Mehr von 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
 

Kürzlich hochgeladen

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
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
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
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
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 

Kürzlich hochgeladen (20)

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
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
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.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
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 

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