SlideShare ist ein Scribd-Unternehmen logo
1 von 13
Downloaden Sie, um offline zu lesen
Control
statement
(if…. else)
MD. SHOHEL ARMAN
DEPT. OF SOFTWARE ENGINEERING
Algorithm
•The solution of any computing problem involves a series of action in a specific order.
•This procedure of solving problems is called algorithm.
Pseudocode
•Pseudocode is an outline of a program, written in a form that can easily be converted into real programming statements
•Pseudocode cannot be compiled nor executed, and there are no real formatting or syntax rules
•It is simply one step - an important one - for producing the final code
•The benefit of pseudocode is that it enables the programmer to concentrate on the algorithms without worrying about all
the syntactic details of a particular programming language
•You can write pseudocode without even knowing what programming language you will use for the final implementation
if statement
Many times we have to take actions based on certain conditions. C provides if or if else statement
to allow us to execute statements based on conditions.
The if statement may be implemented in different forms depending on the complexity of the
conditions to be tested. The forms are:
1. Simple if statement
2. If……… else statements
3. Nested if ……else statements
4. else if ladder
Forms of if…. else Statements
❑Simple if statement:
if ( test expression)
{
Statement block;
}
❑if…else statement:
if ( test expression)
{
Statement block;
}
else
{
Statement block;
}
❑nested if…else statement:
if ( test expression)
{
if ( test expression)
Statement block;
else
Statement block;
}
else
{
statement block;
}
❑if…else ladder:
if ( test expression)
statement-1;
else if ( test expression)
Statement-2;
else if(test expression)
Statement-3;
else
statement-4;
Before going farther we need to review the Relational Operators and Logical Operators :
Relational Operators Logical Operators
C has following logical operators:
• AND expressed by &&
• OR expressed by ||
• NOT expressed by !
Pseudocode
//grade greater then 40 print pass else print failed:
………..
if (grade>40)
{
printf (“Passed n”);
}
Else
{
printf (“Failed n”);
}
……..
Simple if Statement
A simple if structure is used to choose among alternative courses of action
if student’s grade is more than 40
print “passed”
next pseudocode statement
In this pseudocode, if grade of a student is more than 40 then the print command will be
executed. Otherwise, if the student’s grade is not more than 40, the compiler will move to next
pseudocode statement
Simple if Statement
So, as a general form, we can see the if selection or simple if structure as-
if (condition){
body of if
}
The condition needs to be true to get into the body of if. Otherwise, the compiler will go to the next segment of codes
The pseudocode can be written in C as-
if (grade>40){
printf (“Passed n”);
}
If…else Statement
he if…else structure allows the programmer to specify that different actions are to be performed when
the condition is true and when the condition is false
if student’s grade is more than 40
print “passed”
else
print “failed”
If the condition of if is true, then compiler will print passed and if the condition of if is false, the
compiler will print failed.
If…else Statement
So, as a general form, we can see the if/else selection structure as-
if (condition){
body of if
}
else{
body of else
}
If…else Statement
The pseudocode can be written in C as-
if (grade>40)
{
printf (“Passed n”);
}
Else
{
printf (“Failed n”);
}
❑An if structure may not have any else statement followed by it but an else structure must have a if structure preceded.
Algorithm:
if student’s grade is more than 90
print “Grade: A”
else
if student’s grade is more than 80
print “Grade: B”
else
if student’s grade is more than 70
print “Grade: C”
else
if student’s grade is more than 60
print “Grade: D”
else
if student’s grade is more than 50
print “Grade: E”
else
print “Grade: F”
Using if…else statement
C-code:
#include<stdio.h>
void main(){
int grade;
printf("Enter your grade: ");
scanf("%d",&grade);
if (grade>90)
printf("Grade: A");
else if (grade>80)
printf("Grade: B");
else if (grade>70)
printf("Grade: C");
else if (grade>60)
printf("Grade: D");
else if (grade>50)
printf("Grade: E");
else
printf("Grade: F");
getch();

Weitere ähnliche Inhalte

Ähnlich wie conditional_statement.pdf

9781111530532 ppt ch04
9781111530532 ppt ch049781111530532 ppt ch04
9781111530532 ppt ch04
Terry Yoast
 
9781111530532 ppt ch04
9781111530532 ppt ch049781111530532 ppt ch04
9781111530532 ppt ch04
Terry Yoast
 
Ap Power Point Chpt3
Ap Power Point Chpt3Ap Power Point Chpt3
Ap Power Point Chpt3
dplunkett
 
Decision statements in vb.net
Decision statements in vb.netDecision statements in vb.net
Decision statements in vb.net
ilakkiya
 
Metode Perancangan Program #2 Selection and Repetition Control Structure-ITSB...
Metode Perancangan Program #2 Selection and Repetition Control Structure-ITSB...Metode Perancangan Program #2 Selection and Repetition Control Structure-ITSB...
Metode Perancangan Program #2 Selection and Repetition Control Structure-ITSB...
ronistgdr
 

Ähnlich wie conditional_statement.pdf (20)

APP_Unit 1_updated.pptx
APP_Unit 1_updated.pptxAPP_Unit 1_updated.pptx
APP_Unit 1_updated.pptx
 
Programming Fundamentals Decisions
Programming Fundamentals  Decisions Programming Fundamentals  Decisions
Programming Fundamentals Decisions
 
CP Handout#4
CP Handout#4CP Handout#4
CP Handout#4
 
Java Decision Control
Java Decision ControlJava Decision Control
Java Decision Control
 
9781111530532 ppt ch04
9781111530532 ppt ch049781111530532 ppt ch04
9781111530532 ppt ch04
 
9781111530532 ppt ch04
9781111530532 ppt ch049781111530532 ppt ch04
9781111530532 ppt ch04
 
Visula C# Programming Lecture 3
Visula C# Programming Lecture 3Visula C# Programming Lecture 3
Visula C# Programming Lecture 3
 
Constructs (Programming Methodology)
Constructs (Programming Methodology)Constructs (Programming Methodology)
Constructs (Programming Methodology)
 
Ap Power Point Chpt3
Ap Power Point Chpt3Ap Power Point Chpt3
Ap Power Point Chpt3
 
programming c language.
programming c language. programming c language.
programming c language.
 
Decision statements in vb.net
Decision statements in vb.netDecision statements in vb.net
Decision statements in vb.net
 
Chapter 1 nested control structures
Chapter 1 nested control structuresChapter 1 nested control structures
Chapter 1 nested control structures
 
03a control structures
03a   control structures03a   control structures
03a control structures
 
Metode Perancangan Program #2 Selection and Repetition Control Structure-ITSB...
Metode Perancangan Program #2 Selection and Repetition Control Structure-ITSB...Metode Perancangan Program #2 Selection and Repetition Control Structure-ITSB...
Metode Perancangan Program #2 Selection and Repetition Control Structure-ITSB...
 
The Three Basic Selection Structures in C++ Programming Concepts
The Three Basic Selection Structures in C++ Programming ConceptsThe Three Basic Selection Structures in C++ Programming Concepts
The Three Basic Selection Structures in C++ Programming Concepts
 
Chap04
Chap04Chap04
Chap04
 
Control Structures, If..else, switch..case.pptx
Control Structures, If..else, switch..case.pptxControl Structures, If..else, switch..case.pptx
Control Structures, If..else, switch..case.pptx
 
jhtp9_ch04.ppt
jhtp9_ch04.pptjhtp9_ch04.ppt
jhtp9_ch04.ppt
 
Control Structures.pptx
Control Structures.pptxControl Structures.pptx
Control Structures.pptx
 
Ch04
Ch04Ch04
Ch04
 

Kürzlich hochgeladen

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
QucHHunhnh
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
ssuserdda66b
 
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
KarakKing
 
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
heathfieldcps1
 
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
QucHHunhnh
 

Kürzlich hochgeladen (20)

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
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
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
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
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
 
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.
 
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...
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
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
 
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
 
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 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
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
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
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 

conditional_statement.pdf

  • 1. Control statement (if…. else) MD. SHOHEL ARMAN DEPT. OF SOFTWARE ENGINEERING
  • 2. Algorithm •The solution of any computing problem involves a series of action in a specific order. •This procedure of solving problems is called algorithm.
  • 3. Pseudocode •Pseudocode is an outline of a program, written in a form that can easily be converted into real programming statements •Pseudocode cannot be compiled nor executed, and there are no real formatting or syntax rules •It is simply one step - an important one - for producing the final code •The benefit of pseudocode is that it enables the programmer to concentrate on the algorithms without worrying about all the syntactic details of a particular programming language •You can write pseudocode without even knowing what programming language you will use for the final implementation
  • 4. if statement Many times we have to take actions based on certain conditions. C provides if or if else statement to allow us to execute statements based on conditions. The if statement may be implemented in different forms depending on the complexity of the conditions to be tested. The forms are: 1. Simple if statement 2. If……… else statements 3. Nested if ……else statements 4. else if ladder
  • 5. Forms of if…. else Statements ❑Simple if statement: if ( test expression) { Statement block; } ❑if…else statement: if ( test expression) { Statement block; } else { Statement block; } ❑nested if…else statement: if ( test expression) { if ( test expression) Statement block; else Statement block; } else { statement block; } ❑if…else ladder: if ( test expression) statement-1; else if ( test expression) Statement-2; else if(test expression) Statement-3; else statement-4;
  • 6. Before going farther we need to review the Relational Operators and Logical Operators : Relational Operators Logical Operators C has following logical operators: • AND expressed by && • OR expressed by || • NOT expressed by !
  • 7. Pseudocode //grade greater then 40 print pass else print failed: ……….. if (grade>40) { printf (“Passed n”); } Else { printf (“Failed n”); } ……..
  • 8. Simple if Statement A simple if structure is used to choose among alternative courses of action if student’s grade is more than 40 print “passed” next pseudocode statement In this pseudocode, if grade of a student is more than 40 then the print command will be executed. Otherwise, if the student’s grade is not more than 40, the compiler will move to next pseudocode statement
  • 9. Simple if Statement So, as a general form, we can see the if selection or simple if structure as- if (condition){ body of if } The condition needs to be true to get into the body of if. Otherwise, the compiler will go to the next segment of codes The pseudocode can be written in C as- if (grade>40){ printf (“Passed n”); }
  • 10. If…else Statement he if…else structure allows the programmer to specify that different actions are to be performed when the condition is true and when the condition is false if student’s grade is more than 40 print “passed” else print “failed” If the condition of if is true, then compiler will print passed and if the condition of if is false, the compiler will print failed.
  • 11. If…else Statement So, as a general form, we can see the if/else selection structure as- if (condition){ body of if } else{ body of else }
  • 12. If…else Statement The pseudocode can be written in C as- if (grade>40) { printf (“Passed n”); } Else { printf (“Failed n”); } ❑An if structure may not have any else statement followed by it but an else structure must have a if structure preceded.
  • 13. Algorithm: if student’s grade is more than 90 print “Grade: A” else if student’s grade is more than 80 print “Grade: B” else if student’s grade is more than 70 print “Grade: C” else if student’s grade is more than 60 print “Grade: D” else if student’s grade is more than 50 print “Grade: E” else print “Grade: F” Using if…else statement C-code: #include<stdio.h> void main(){ int grade; printf("Enter your grade: "); scanf("%d",&grade); if (grade>90) printf("Grade: A"); else if (grade>80) printf("Grade: B"); else if (grade>70) printf("Grade: C"); else if (grade>60) printf("Grade: D"); else if (grade>50) printf("Grade: E"); else printf("Grade: F"); getch();