Unit 5 Control Structures - I, Study notes of C programming

A unit from a programming course that introduces loops and decision structures in C programming language. It explains the while() and do-while() loops, the comma operator, the transfer of control from within loops, the (ternary) if()-then-else operator, and the switch-case-default statement. examples of programs that use these structures. The unit also includes objectives that students should be able to achieve after studying it.

Typology: Study notes

2021/2022

Available from 03/15/2023

SteveFisher
SteveFisher 🇺🇸

10 documents

1 / 28

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c

Partial preview of the text

Download Unit 5 Control Structures - I and more Study notes C programming in PDF only on Docsity!

UNIT 5 CONTROL STRUCTURES - I Structure Page No. 5.1 Introduction 101 Objectives 5.2 The while( and do - while() Loops 101 5.3. The Comma Operator 108 5.4. The Transfer of Control from Within Loops 110 5.5 The (Ternary) if()- then - else Operator 13 5.6 The switch - case - default Statement 118 3.7 Summary 123 5.8 Sohitions/Answers 124 $5.1. INTRODUCTION The rationale of structured programming is to create goto-less programs. While compound object statements help to an extent in achieving this objective, it is the loop structures of C—the for (;;} loop, the while() loop and the do - white() loop—that are indispensable tools for accomplishing this goal. Loops are required wherever a set of statements must be executed a number of times. In this Unit, we will introduce to loops. In Sec. 5.2, we will discuss the while() and do-while() loops. We will also introduce you the the switch-case-default statement in Sec. 5.6. In Sec. 5.4, we will discuss how to transfer control from within loops. In Sec. 5.3, we will introduce you to the comma operator. Objectives After studying this unit, you should be able to * write programs using the while () and do - while () loops; * explain the use of comma operator, * use the ternary of if - then - else operator; and * use the switch - case - default decision structure for muitiple-way branchings. 5.2 THE while () AND do - while () LOOPS Let us begin our discussion of loops by looking at a common task you may have performed many times. Suppose you want look up a word in the dictionary.For example, you ate looking up the word ringent. It’s unlikely that you will be able to open the dictionary at the right page and locate the word in its line the first time; so you open it towards the middie, where jet’s assume you find the word nexus. Since n comes before r, the latter half of the book must now be searched. Thus the process is repeated: you open the book towards the middle of the second half, at approximately the three-quarters point, where again assume you find serub. s comes after r, so ringent must occur, if at all it does, between the pages which contain nexus and scrub; you therefore bisect this interval, and suppose you now find the dictionary open at the page containing puddle. p comes before r, so ringent must be between puddle and scrub. You therefore divide this interval, and thus continue to bisect the ensuing sub-intervals, until you find the word, or discover that it’s not defined in the dictionary. Note that, beginning with the entire dictionary as the original search interval, you have executed the interval creation and search instruction repetitively; in computer jargon, you've