Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

C Programming: Repetition and Control Statements, Slides of Computer Fundamentals

A part of the ise105 computing fundamentals course on docsity.com. It covers the essentials of repetition in c programming, including counter-controlled repetition and the use of for and do-while loops. The document also introduces the switch statement, break and continue statements, and logical operators.

Typology: Slides

2011/2012

Uploaded on 07/31/2012

karthik
karthik 🇮🇳

4.6

(16)

95 documents

1 / 16

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
.
ISE105: Computing Fundamentals
Lecture 9
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download C Programming: Repetition and Control Statements and more Slides Computer Fundamentals in PDF only on Docsity!

.

ISE105: Computing Fundamentals

Lecture 9

.

Chapter 4 – C Program Control

Outline 4.1 Introduction 4.2 The Essentials of Repetition 4.3 Counter-Controlled Repetition 4.4 The for Repetition Statement 4.5 The for Statement: Notes and Observations 4.6 Examples Using the for Statement 4.7 The switch Multiple-Selection Statement 4.8 The do…while Repetition Statement 4.9 The break and continue Statements 4.10 Logical Operators 4.11 Confusing Equality (==) and Assignment (=) Operators 4.12 Structured Programming Summary

.

4.1 Introduction

  • This chapter introduces
    • Additional repetition control structures
      • for
      • Do…while
    • switch multiple selection statement
    • break statement
      • Used for exiting immediately and rapidly from certain control

structures

  • continue statement
    • Used for skipping the remainder of the body of a repetition

structure and proceeding with the next iteration of the loop

.

4.2 The Essentials of Repetition

  • Loop
    • Group of instructions computer executes repeatedly while some condition remains true
  • Counter-controlled repetition
    • Definite repetition: know how many times loop will execute
    • Control variable used to count repetitions
  • Sentinel-controlled repetition
    • Indefinite repetition
    • Used when number of repetitions not known
    • Sentinel value indicates "end of data"

.

4.3 Essentials of Counter-Controlled Repetition

  • Example:

int counter = 1; // initialization while ( counter <= 10 ) { // repetition condition printf( "%d\n", counter ); ++counter; // increment }

  • The statement int counter = 1;
  • Names counter
  • Defines it to be an integer
  • Reserves space for it in memory
  • Sets it to an initial value of 1

OutlineOutline

.

fig04_01.c

Program Output 1 2 3 4 5 6 7 8 9

10

OutlineOutline

.

fig04_02.c

.

4.4 The for Repetition Statement

.

4.4 The for Repetition Statement

  • For loops can usually be rewritten as while loops:

initialization;

while ( loopContinuationTest ) {

statement;

increment;

  • Initialization and increment
    • Can be comma-separated lists
    • Example:

for (int i = 0, j = 0; j + i <= 10; j++, i++)

printf( "%d\n", j + i );

.

4.5 The for Statement : Notes and Observations

  • Arithmetic expressions
    • Initialization, loop-continuation, and increment can contain arithmetic expressions. If x equals 2 and y equals 10 for ( j = x; j <= 4 * x * y; j += y / x ) is equivalent to for ( j = 2; j <= 80; j += 5 )
  • Notes about the for statement:
    • "Increment" may be negative (decrement)
    • If the loop continuation condition is initially false
      • The body of the for statement is not performed
      • Control proceeds with the next statement after the for statement
    • Control variable
      • Often printed or used inside for body, but not necessary

OutlineOutline

.

fig04_05.c

Program Output Sum is 2550