Relational and Logical Operators
CS 164
Dr. Tim McGuire
Sam Houston State University

Data Driven Programs

Cís Relational Operators

Use of the Relational Operators

Using Pseudocode to describe solutions to problems

Some Examples

int a =1, b = 2, c = 3, d = 1;

Summary

One use of relational operators

(sales > 1000.00) * 0.05 * (sales - 1000.00);

The if command

if (relational test)
{ a block of one or more C statements }

Examples

Previous "Tricky" example:

commission = 0.10*sales;
if (sales > 1000.00)
{ commission += 0.05*(sales-1000.00); }
commission = 0.10*sales + (sales > 1000.00) * 0.05 * (sales - 1000.00);

An example of a program using an if construct:  hotelif.cpp

The if-else construct

if (relational test)
{ a block of one or more C statements }
else
{ a block of one or more C statements }

Action of if-else

if (relation is true)
{ Block of one or more C statements
}
else
{ Another block of one or more C statements
}
// rest of program follows

Warnings & Comments

Logical Operators

Precedence of operators

Using logical operators

if ( x == 0 || x == 1)
{ do one thing }
else
{ do another thing }
if (x == 0 || 1) // why not???