Introduction to Computer Science I While Loop Janyl Jumadinova - - PowerPoint PPT Presentation

introduction to computer science i
SMART_READER_LITE
LIVE PREVIEW

Introduction to Computer Science I While Loop Janyl Jumadinova - - PowerPoint PPT Presentation

Introduction to Computer Science I While Loop Janyl Jumadinova 26-28 March, 2018 Loops A portion of a program that repeats a statement or a group of statements is called a loop. The statement or group of statements to be repeated is


slide-1
SLIDE 1

Introduction to Computer Science I

While Loop

Janyl Jumadinova 26-28 March, 2018

slide-2
SLIDE 2

Loops

◮ A portion of a program that repeats a statement or a group of

statements is called a loop.

◮ The statement or group of statements to be repeated is called

the body of the loop.

◮ There must be a means of exiting the loop. 2/11

slide-3
SLIDE 3

While Loop

◮ A while statement repeats while a controlling boolean

expression remains true.

◮ The loop body typically contains an action that ultimately

causes the controlling boolean expression to become false.

3/11

slide-4
SLIDE 4

While Loop

Syntax: while (Boolean_Expression) Body_Statement

  • r

while (Boolean_Expression) { First_Statement Second_Statement ... }

4/11

slide-5
SLIDE 5

5/11

slide-6
SLIDE 6

6/11

slide-7
SLIDE 7

7/11

slide-8
SLIDE 8

Break Statement

◮ A break statement can be used to end a loop immediately. ◮ The break statement ends only the innermost loop or if

statement that contains the break statement.

8/11

slide-9
SLIDE 9

Break Statement

◮ A break statement can be used to end a loop immediately. ◮ The break statement ends only the innermost loop or if

statement that contains the break statement.

◮ break statements make loops more difficult to understand. ◮ Use break statements sparingly (if ever). 8/11

slide-10
SLIDE 10

9/11

slide-11
SLIDE 11

Continue in Loops

A continue statement :

◮ Ends current loop iteration ◮ Begins the next one ◮ Do not use unless necessary

– Introduce unneeded complications

10/11

slide-12
SLIDE 12

Loop Bugs

Common loop bugs:

◮ Unintended infinite loops ◮ Off-by-one errors ◮ Testing equality of floating-point numbers ◮ The loop may terminate for some input values, but not for

  • thers.

11/11