Looping with While Statements: A Comprehensive Guide for Beginners, Slides of Computer science

A comprehensive introduction to looping with while statements in programming. It covers the advantages of looping, the use of loop control variables, the difference between definite and indefinite loops, nested loops, and common loop mistakes. Well-structured and uses clear examples to illustrate the concepts. It is suitable for beginners learning about programming concepts.

Typology: Slides

2024/2025

Uploaded on 03/27/2025

simon-orgen
simon-orgen 🇵🇭

1 document

1 / 24

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
z
LOOPING WITH
while
STATEMENTS
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18

Partial preview of the text

Download Looping with While Statements: A Comprehensive Guide for Beginners and more Slides Computer science in PDF only on Docsity!

z

LOOPING WITH

while

STATEMENTS

z

In this lesson, you will learn about:  The advantages of looping  Using a loop control variable  Difference between definite and indefinite loops  Nested loops  Avoiding common loop mistakes

z

z

Understanding the Advantages of Looping

z

Using a Loop Control Variable

z

Loop control variable

Manages the number of

repetitions a loop performs

z

Using a Definite Loop with a Counter Counter is any numeric variable that counts the number of times an event has occurred.

z

Three (3) separate actions should occur using a loop control variable:

  1. The loop control variable is initialized before entering the loop. start Declarations num count = 0

Loop control

variable

Loop control variable is initialized. count < 4? Loop control variable is tested. output “Hello” count = count + 1 Loop control variable is altered. output “Goodbye” stop

  1. The loop control variable’s value is tested , and if the result is true, the loop body is entered.

3. The loop control

variable is altered

within the body of

the loop so that the

tested condition that

follows while

eventually is false.

YES

NO

z

Ways to change the value of the Loop Control Variable

 Simply assign

a new value to

the loop

control

variable

start Declarations num count = 0 count < 4? output “Hello” count = 4 output “Goodbye” stop

YES

NO

z

Ways to change the value of the Loop Control Variable  Retrieve a new value from an input device start Declarations num count = 0 count < 4? output “Hello” output “Goodbye” stop input count

YES

NO

z

Ways to change the value of the Loop Control Variable

 Reduce, or

decrement(--) ,

the loop control

variable

start Declarations num count = 8 count > 4? output “Hello” count = count - 1 output “Goodbye” stop

YES

NO

z

Using an Indefinite Loop with a Sentinel Value Sentinel Value  A preselected value that stops the execution of a program  it represents an entry or exit point

start Declarations string RESPONSE output “Do you want to continue? Y or N >>” input RESPONSE while RESPONSE = “Y” output “Hello” output “Do you want to continue? Y or N >>” input RESPONSE endwhile output “Goodbye” stop

z

Nested LOOP