SlideShare a Scribd company logo
PROGRAMMING
IF STATEMENTS &
RELATIONAL
OPERATORS
By: John Paul Espino
De La Salle University – Dasmarinas
Facebook.com/Johnpaul.dss
Computer Programming - if Statements  & Relational Operators
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 (20)

PPT
The Three Basic Selection Structures in C++ Programming Concepts
Tech
 
PPT
Decision making and branching
Hossain Md Shakhawat
 
PPTX
Decision Making Statement in C ppt
MANJUTRIPATHI7
 
PPTX
Logical and Conditional Operator In C language
Abdul Rehman
 
PPTX
Selection Statements in C Programming
Kamal Acharya
 
PDF
C programming decision making
SENA
 
PPSX
C lecture 3 control statements slideshare
Gagan Deep
 
PPTX
Control and conditional statements
rajshreemuthiah
 
PPTX
Decision making statements in C programming
Rabin BK
 
PPTX
C Programming: Control Structure
Sokngim Sa
 
PPT
Control structure C++
Anil Kumar
 
PPT
Visula C# Programming Lecture 3
Abou Bakr Ashraf
 
PPTX
Control structure of c
Komal Kotak
 
PDF
Conditional operators
BU
 
PPT
Control Structures
Ghaffar Khan
 
PPTX
Unit 5. Control Statement
Ashim Lamichhane
 
PPT
Mesics lecture 6 control statement = if -else if__else
eShikshak
 
The Three Basic Selection Structures in C++ Programming Concepts
Tech
 
Decision making and branching
Hossain Md Shakhawat
 
Decision Making Statement in C ppt
MANJUTRIPATHI7
 
Logical and Conditional Operator In C language
Abdul Rehman
 
Selection Statements in C Programming
Kamal Acharya
 
C programming decision making
SENA
 
C lecture 3 control statements slideshare
Gagan Deep
 
Control and conditional statements
rajshreemuthiah
 
Decision making statements in C programming
Rabin BK
 
C Programming: Control Structure
Sokngim Sa
 
Control structure C++
Anil Kumar
 
Visula C# Programming Lecture 3
Abou Bakr Ashraf
 
Control structure of c
Komal Kotak
 
Conditional operators
BU
 
Control Structures
Ghaffar Khan
 
Unit 5. Control Statement
Ashim Lamichhane
 
Mesics lecture 6 control statement = if -else if__else
eShikshak
 

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

PPTX
Ch5 Selection Statements
SzeChingChen
 
PPTX
Programming note C#
Ahmad Syahmi Irfan
 
PDF
ICP - Lecture 7 and 8
Hassaan Rahman
 
PDF
Selection & Making Decisions in c
yndaravind
 
PDF
[ITP - Lecture 08] Decision Control Structures (If Statement)
Muhammad Hammad Waseem
 
PPTX
COM1407: Program Control Structures – Decision Making & Branching
Hemantha Kulathilake
 
PPT
Control statments in c
CGC Technical campus,Mohali
 
PPTX
18CSS101J PROGRAMMING FOR PROBLEM SOLVING
GOWSIKRAJAP
 
PDF
Control statements anil
Anil Dutt
 
PPT
Fcp chapter 2 upto_if_else (2)
Jagdish Kamble
 
PPTX
Programming in C
Nishant Munjal
 
PDF
Unit 2=Decision Control & Looping Statements.pdf
Dr. Ambedkar Institute of Technology, Bangalore 56
 
PDF
Ch05.pdf
ShivamChaturvedi67
 
PPT
Control structure
Samsil Arefin
 
PPTX
unit2 C-ProgrammingChapter 2 Control statements.pptx
JavvajiVenkat
 
PPTX
6 Control Structures-1.pptxAAAAAAAAAAAAAAAAAAAAA
EG20910848921ISAACDU
 
PPTX
Control Structures in C
sana shaikh
 
PPTX
Decision Making and Branching
Munazza-Mah-Jabeen
 
PDF
control statement
Kathmandu University
 
PDF
Programming for Problem Solving
Kathirvel Ayyaswamy
 
Ch5 Selection Statements
SzeChingChen
 
Programming note C#
Ahmad Syahmi Irfan
 
ICP - Lecture 7 and 8
Hassaan Rahman
 
Selection & Making Decisions in c
yndaravind
 
[ITP - Lecture 08] Decision Control Structures (If Statement)
Muhammad Hammad Waseem
 
COM1407: Program Control Structures – Decision Making & Branching
Hemantha Kulathilake
 
Control statments in c
CGC Technical campus,Mohali
 
18CSS101J PROGRAMMING FOR PROBLEM SOLVING
GOWSIKRAJAP
 
Control statements anil
Anil Dutt
 
Fcp chapter 2 upto_if_else (2)
Jagdish Kamble
 
Programming in C
Nishant Munjal
 
Unit 2=Decision Control & Looping Statements.pdf
Dr. Ambedkar Institute of Technology, Bangalore 56
 
Control structure
Samsil Arefin
 
unit2 C-ProgrammingChapter 2 Control statements.pptx
JavvajiVenkat
 
6 Control Structures-1.pptxAAAAAAAAAAAAAAAAAAAAA
EG20910848921ISAACDU
 
Control Structures in C
sana shaikh
 
Decision Making and Branching
Munazza-Mah-Jabeen
 
control statement
Kathmandu University
 
Programming for Problem Solving
Kathirvel Ayyaswamy
 
Ad

More from John Paul Espino (20)

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

Recently uploaded (20)

PPTX
Tally software_Introduction_Presentation
AditiBansal54083
 
PPTX
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
PDF
Alarm in Android-Scheduling Timed Tasks Using AlarmManager in Android.pdf
Nabin Dhakal
 
PDF
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
PPTX
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
PPTX
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
PPTX
Migrating Millions of Users with Debezium, Apache Kafka, and an Acyclic Synch...
MD Sayem Ahmed
 
PPTX
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
PDF
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
PDF
Online Queue Management System for Public Service Offices in Nepal [Focused i...
Rishab Acharya
 
PDF
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
PDF
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
PDF
Mobile CMMS Solutions Empowering the Frontline Workforce
CryotosCMMSSoftware
 
PPTX
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
PPTX
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
PDF
GetOnCRM Speeds Up Agentforce 3 Deployment for Enterprise AI Wins.pdf
GetOnCRM Solutions
 
PPTX
Coefficient of Variance in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
PPTX
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
Tally software_Introduction_Presentation
AditiBansal54083
 
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
Alarm in Android-Scheduling Timed Tasks Using AlarmManager in Android.pdf
Nabin Dhakal
 
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
Migrating Millions of Users with Debezium, Apache Kafka, and an Acyclic Synch...
MD Sayem Ahmed
 
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
Online Queue Management System for Public Service Offices in Nepal [Focused i...
Rishab Acharya
 
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
Mobile CMMS Solutions Empowering the Frontline Workforce
CryotosCMMSSoftware
 
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
GetOnCRM Speeds Up Agentforce 3 Deployment for Enterprise AI Wins.pdf
GetOnCRM Solutions
 
Coefficient of Variance in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 

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
  • 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(); }