SlideShare une entreprise Scribd logo
1  sur  13
Télécharger pour lire hors ligne
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();

Contenu connexe

Similaire à 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
 

Similaire à 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
 

Dernier

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
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 

Dernier (20)

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
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
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
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
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
 
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Ữ Â...
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.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
 
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...
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
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...
 
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
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
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
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.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();