08.01.2023 Views

Learn to Program with C_ Learn to Program using the Popular C Programming Language ( PDFDrive )

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 4

Programs with Selection Logic

In this chapter, we will explain the following:

• What are Boolean expressions

• How C represents Boolean values

• How to write programs using if

• How to write programs using if...else

• Where semicolons are required, where they are optional, and where they

must not be put

• How a program should be tested

• Why symbolic constants are useful and how to use them in a C program

4.1 Introduction

In the last chapter, we showed how to write programs using sequence logic—programs whose

statements are executed “in sequence” from the first to the last.

In this chapter, the programs will use selection logic—they will test some condition and take

different courses of action based on whether the condition is true or false. In C, selection logic is

implemented using the if and the if...else statements.

4.2 Boolean Expressions

A Boolean expression (named after the famous English mathematician George Boole) is one that

is either true or false. The simplest kinds of Boolean expressions are those that compare one value

with another. Some examples are:

k is equal to 999

a is greater than 100

a2 + b2 is equal to c2

b2 is greater than or equal to 4ac

s is not equal to 0

65

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!