SlideShare a Scribd company logo
1 of 20
PROGRAMMING
IF STATEMENTS &
RELATIONAL
OPERATORS
By: John Paul Espino
De La Salle University – Dasmarinas
Facebook.com/Johnpaul.dss
CONDITIONAL STATEMENTS
• A conditional statement allows us to control whether a program segment is executed or not.
• Two constructs
• if statement
• if
• if-else
• if-else-if
• switch statement
THE BASIC IF STATEMENT
• Syntax
if(condition)
action
• if the condition is true then
execute the action.
• action is either a single
statement or a group of
statements within braces.
condition
action
true
false
CHOICE (IF)
 Put multiple action statements
within braces
if (it's raining){
printf(“take umbrella”);
printf(“wear raincoat”);
}
ABSOLUTE VALUE
// program to read number & print its absolute value
#include <stdio.h>
main(){
int value;
printf("Enter integer: “);
scanf(“%d”,&value);
if(value < 0)
value = -value;
printf("The absolute value is %d “,value);
getch();
}
RELATIONAL OPERATORS
Relational operators are used to compare two values to form a
condition.
Math C Plain English
= == equals [example: if(a==b) ]
[ (a=b) means put the value of b into a ]
< < less than
 <= less than or equal to
> > greater than
 >= greater than or equal to
 != not equal to
CONDITIONS
Examples:
Number_of_Students < 200
10 > 20
20 * j == 10 + i
THE BOOLEAN TYPE
• C contains a type named bool for conditions.
• A condition can have one of two values:
• true (corresponds to a non-zero value)
• false (corresponds to zero value)
• Boolean operators can be used to form more complex conditional
expressions.
• The and operator is &&
• The or operator is ||
• The not operator is !
THE BOOLEAN TYPE
• Truth table for "&&" (AND):
Operand1 Operand2 Operand1 &&
Operand2
true true true
true false false
false true false
false false false
THE BOOLEAN TYPE
• Truth table for “||" (OR):
Operand1 Operand2 Operand1 ||
Operand2
true true true
true false true
false true true
false false false
THE BOOLEAN TYPE
• Truth table for "!" (NOT):
Operand !Operand
true false
false true
A BOOLEAN TYPE
• Assignments to bool type variables
bool P = true;
bool Q = false;
bool R = true;
bool S = P && Q;
bool T = !Q || R;
bool U = !(R && !Q);
“IF” WITH A BLOCK OF STATEMENTS
if (aValue <= 10)
{
printf("Answer is %.2fn", aValue);
countB++;
}
POSITIVE OR NEGATIVE
#include <stdio.h>
main()
{
int number;
clrscr();
printf(“Enter a number”);
scanf(“%d”,&number);
if (number>0)
printf(“It is Positive”);
getch();
}
IF-ELSE STATEMENT
SYNTAX OF IF-ELSE STATEMENT
if ( condition)
statement;
else
statement;
IF – ELSE WITH A BLOCK OF STATEMENTS
if (aValue <= 10)
{
printf("Answer is %.2fn", aValue);
countB++;
} End if
else
{
printf("Error occurredn");
countC++;
} End else
TO VOTE OR NOT TO VOTE
#include<stdio.h>
main()
{
int age;
clrscr();
printf(“Enter your age”);
scanf(“%d”,&age);
if (age>=18)
printf(“You are qualified to vote”);
else
printf(“Not qualified to vote”);
getch();
}
POSITIVE OR NEGATIVE
#include <stdio.h>
main()
{
int number;
clrscr();
printf(“Enter a number”);
scanf(“%d”,&number);
if (number>0)
printf(“Positive number”);
else
printf(“Negative number”);
getch();
}

More Related Content

What's hot

Conditional operators
Conditional operatorsConditional operators
Conditional operators
BU
 
Control Structures
Control StructuresControl Structures
Control Structures
Ghaffar Khan
 

What's hot (20)

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
 
Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branching
 
Control structures in C
Control structures in CControl structures in C
Control structures in C
 
Decision Making Statement in C ppt
Decision Making Statement in C pptDecision Making Statement in C ppt
Decision Making Statement in C ppt
 
Control structures in c
Control structures in cControl structures in c
Control structures in c
 
Logical and Conditional Operator In C language
Logical and Conditional Operator In C languageLogical and Conditional Operator In C language
Logical and Conditional Operator In C language
 
Selection Statements in C Programming
Selection Statements in C ProgrammingSelection Statements in C Programming
Selection Statements in C Programming
 
C programming decision making
C programming decision makingC programming decision making
C programming decision making
 
Control structure in c
Control structure in cControl structure in c
Control structure in c
 
C lecture 3 control statements slideshare
C lecture 3 control statements slideshareC lecture 3 control statements slideshare
C lecture 3 control statements slideshare
 
Control and conditional statements
Control and conditional statementsControl and conditional statements
Control and conditional statements
 
Decision making statements in C programming
Decision making statements in C programmingDecision making statements in C programming
Decision making statements in C programming
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
 
Control structure C++
Control structure C++Control structure C++
Control structure C++
 
Visula C# Programming Lecture 3
Visula C# Programming Lecture 3Visula C# Programming Lecture 3
Visula C# Programming Lecture 3
 
Control structure of c
Control structure of cControl structure of c
Control structure of c
 
Conditional operators
Conditional operatorsConditional operators
Conditional operators
 
Control Structures
Control StructuresControl Structures
Control Structures
 
Unit 5. Control Statement
Unit 5. Control StatementUnit 5. Control Statement
Unit 5. Control Statement
 
Mesics lecture 6 control statement = if -else if__else
Mesics lecture 6   control statement = if -else if__elseMesics lecture 6   control statement = if -else if__else
Mesics lecture 6 control statement = if -else if__else
 

Similar to Computer Programming - if Statements & Relational Operators

10-Lec - Nested IF Statement.pptx
10-Lec - Nested IF Statement.pptx10-Lec - Nested IF Statement.pptx
10-Lec - Nested IF Statement.pptx
AqeelAbbas94
 
Ap Power Point Chpt3 B
Ap Power Point Chpt3 BAp Power Point Chpt3 B
Ap Power Point Chpt3 B
dplunkett
 

Similar to Computer Programming - if Statements & Relational Operators (20)

C statements (a fraction).pptx
C statements (a fraction).pptxC statements (a fraction).pptx
C statements (a fraction).pptx
 
If
IfIf
If
 
M C6java5
M C6java5M C6java5
M C6java5
 
if,loop,switch
if,loop,switchif,loop,switch
if,loop,switch
 
Java unit 3
Java unit 3Java unit 3
Java unit 3
 
10-Lec - Nested IF Statement.pptx
10-Lec - Nested IF Statement.pptx10-Lec - Nested IF Statement.pptx
10-Lec - Nested IF Statement.pptx
 
Control statments in c
Control statments in cControl statments in c
Control statments in c
 
SPL 8 | Loop Statements in C
SPL 8 | Loop Statements in CSPL 8 | Loop Statements in C
SPL 8 | Loop Statements in C
 
Ref Lec 4- Conditional Statement (1).pptx
Ref Lec 4- Conditional Statement (1).pptxRef Lec 4- Conditional Statement (1).pptx
Ref Lec 4- Conditional Statement (1).pptx
 
C language UPTU Unit3 Slides
C language UPTU Unit3 SlidesC language UPTU Unit3 Slides
C language UPTU Unit3 Slides
 
Bsit1
Bsit1Bsit1
Bsit1
 
Ap Power Point Chpt3 B
Ap Power Point Chpt3 BAp Power Point Chpt3 B
Ap Power Point Chpt3 B
 
Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8
 
Programming Fundamentals Decisions
Programming Fundamentals  Decisions Programming Fundamentals  Decisions
Programming Fundamentals Decisions
 
L05if
L05ifL05if
L05if
 
Unit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CUnit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in C
 
Java script session 4
Java script session 4Java script session 4
Java script session 4
 
2.all , ComProg1.pptx
2.all , ComProg1.pptx2.all , ComProg1.pptx
2.all , ComProg1.pptx
 
C programming Control Structure.pptx
C programming Control Structure.pptxC programming Control Structure.pptx
C programming Control Structure.pptx
 
Ch04
Ch04Ch04
Ch04
 

More from John Paul Espino

More from John Paul Espino (20)

Religion Education - Sa kabataang Pilipino - A La Juventud Filipina
Religion Education - Sa kabataang Pilipino - A La Juventud FilipinaReligion Education - Sa kabataang Pilipino - A La Juventud Filipina
Religion Education - Sa kabataang Pilipino - A La Juventud Filipina
 
Religion Education - Human Dignity - Freedom and Responsibility
Religion Education - Human Dignity - Freedom and ResponsibilityReligion Education - Human Dignity - Freedom and Responsibility
Religion Education - Human Dignity - Freedom and Responsibility
 
Public Speaking and Leadership -Process of Reading
Public Speaking and Leadership -Process of ReadingPublic Speaking and Leadership -Process of Reading
Public Speaking and Leadership -Process of Reading
 
Environmental Engineering - Case Study - The Minamata Disease Disaster
Environmental Engineering - Case Study - The Minamata Disease DisasterEnvironmental Engineering - Case Study - The Minamata Disease Disaster
Environmental Engineering - Case Study - The Minamata Disease Disaster
 
Philippine Constitution - Parliamentary Immunity
Philippine Constitution - Parliamentary Immunity Philippine Constitution - Parliamentary Immunity
Philippine Constitution - Parliamentary Immunity
 
Philippine Constitution - Article XI - Accountability of Public Officers
Philippine Constitution - Article XI - Accountability of Public OfficersPhilippine Constitution - Article XI - Accountability of Public Officers
Philippine Constitution - Article XI - Accountability of Public Officers
 
Philippine Constitution - Article X - Local Government
Philippine Constitution - Article X - Local GovernmentPhilippine Constitution - Article X - Local Government
Philippine Constitution - Article X - Local Government
 
Philippine Constitution - Article VIII - Judicial Department
Philippine Constitution - Article VIII - Judicial DepartmentPhilippine Constitution - Article VIII - Judicial Department
Philippine Constitution - Article VIII - Judicial Department
 
Philippine Constitution - Article VII - Executive Department
Philippine Constitution - Article VII - Executive DepartmentPhilippine Constitution - Article VII - Executive Department
Philippine Constitution - Article VII - Executive Department
 
Philippine Constitution - Article VI - Legislative Power
Philippine Constitution - Article VI - Legislative PowerPhilippine Constitution - Article VI - Legislative Power
Philippine Constitution - Article VI - Legislative Power
 
Philippine Constitution - ARTICLE IX - Constitutional Commissions
Philippine Constitution - ARTICLE IX - Constitutional Commissions Philippine Constitution - ARTICLE IX - Constitutional Commissions
Philippine Constitution - ARTICLE IX - Constitutional Commissions
 
Philosophy - the aesthetic attitude and the sublime
Philosophy - the aesthetic attitude and the sublime Philosophy - the aesthetic attitude and the sublime
Philosophy - the aesthetic attitude and the sublime
 
Philosophy - the aestheic attidude and the sublime
Philosophy - the aestheic attidude and the sublimePhilosophy - the aestheic attidude and the sublime
Philosophy - the aestheic attidude and the sublime
 
Information literacy - effects of social networking to students thesis presen...
Information literacy - effects of social networking to students thesis presen...Information literacy - effects of social networking to students thesis presen...
Information literacy - effects of social networking to students thesis presen...
 
Fundamentals of Accounting - Posting & Trial Balance
Fundamentals of Accounting - Posting & Trial BalanceFundamentals of Accounting - Posting & Trial Balance
Fundamentals of Accounting - Posting & Trial Balance
 
Fundamentals of accounting - manufacturing
Fundamentals of accounting - manufacturingFundamentals of accounting - manufacturing
Fundamentals of accounting - manufacturing
 
Fundamentals of accounting - cost value profit (cvp)
Fundamentals of accounting - cost value profit (cvp)Fundamentals of accounting - cost value profit (cvp)
Fundamentals of accounting - cost value profit (cvp)
 
Ethics - nicomachean ethics section 7 - 9
Ethics - nicomachean ethics section 7 - 9 Ethics - nicomachean ethics section 7 - 9
Ethics - nicomachean ethics section 7 - 9
 
Ethics - aristotle's ethics
Ethics - aristotle's ethicsEthics - aristotle's ethics
Ethics - aristotle's ethics
 
Environmental engineering - oxygen cycle
Environmental engineering - oxygen cycleEnvironmental engineering - oxygen cycle
Environmental engineering - oxygen cycle
 

Recently uploaded

CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 

Recently uploaded (20)

LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
ManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide DeckManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide Deck
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 

Computer Programming - if Statements & Relational Operators

  • 1. PROGRAMMING IF STATEMENTS & RELATIONAL OPERATORS By: John Paul Espino De La Salle University – Dasmarinas Facebook.com/Johnpaul.dss
  • 2.
  • 3. CONDITIONAL STATEMENTS • A conditional statement allows us to control whether a program segment is executed or not. • Two constructs • if statement • if • if-else • if-else-if • switch statement
  • 4. THE BASIC IF STATEMENT • Syntax if(condition) action • if the condition is true then execute the action. • action is either a single statement or a group of statements within braces. condition action true false
  • 5. CHOICE (IF)  Put multiple action statements within braces if (it's raining){ printf(“take umbrella”); printf(“wear raincoat”); }
  • 6. ABSOLUTE VALUE // program to read number & print its absolute value #include <stdio.h> main(){ int value; printf("Enter integer: “); scanf(“%d”,&value); if(value < 0) value = -value; printf("The absolute value is %d “,value); getch(); }
  • 7. RELATIONAL OPERATORS Relational operators are used to compare two values to form a condition. Math C Plain English = == equals [example: if(a==b) ] [ (a=b) means put the value of b into a ] < < less than  <= less than or equal to > > greater than  >= greater than or equal to  != not equal to
  • 9. THE BOOLEAN TYPE • C contains a type named bool for conditions. • A condition can have one of two values: • true (corresponds to a non-zero value) • false (corresponds to zero value) • Boolean operators can be used to form more complex conditional expressions. • The and operator is && • The or operator is || • The not operator is !
  • 10. THE BOOLEAN TYPE • Truth table for "&&" (AND): Operand1 Operand2 Operand1 && Operand2 true true true true false false false true false false false false
  • 11. THE BOOLEAN TYPE • Truth table for “||" (OR): Operand1 Operand2 Operand1 || Operand2 true true true true false true false true true false false false
  • 12. THE BOOLEAN TYPE • Truth table for "!" (NOT): Operand !Operand true false false true
  • 13. A BOOLEAN TYPE • Assignments to bool type variables bool P = true; bool Q = false; bool R = true; bool S = P && Q; bool T = !Q || R; bool U = !(R && !Q);
  • 14. “IF” WITH A BLOCK OF STATEMENTS if (aValue <= 10) { printf("Answer is %.2fn", aValue); countB++; }
  • 15. POSITIVE OR NEGATIVE #include <stdio.h> main() { int number; clrscr(); printf(“Enter a number”); scanf(“%d”,&number); if (number>0) printf(“It is Positive”); getch(); }
  • 17. SYNTAX OF IF-ELSE STATEMENT if ( condition) statement; else statement;
  • 18. IF – ELSE WITH A BLOCK OF STATEMENTS if (aValue <= 10) { printf("Answer is %.2fn", aValue); countB++; } End if else { printf("Error occurredn"); countC++; } End else
  • 19. TO VOTE OR NOT TO VOTE #include<stdio.h> main() { int age; clrscr(); printf(“Enter your age”); scanf(“%d”,&age); if (age>=18) printf(“You are qualified to vote”); else printf(“Not qualified to vote”); getch(); }
  • 20. POSITIVE OR NEGATIVE #include <stdio.h> main() { int number; clrscr(); printf(“Enter a number”); scanf(“%d”,&number); if (number>0) printf(“Positive number”); else printf(“Negative number”); getch(); }