0% found this document useful (0 votes)
66 views19 pages

Control Structure-Repetition: Using Decrement or Increment

The document discusses different types of loops in programming including while loops, for loops, and nested loops. It explains that a while loop repeats a block of code as long as a condition is true, providing examples of how the while loop works and how it can be used for input validation. Flowcharts are also included to visually depict the logic and flow of while loops.

Uploaded by

Sky Fire
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views19 pages

Control Structure-Repetition: Using Decrement or Increment

The document discusses different types of loops in programming including while loops, for loops, and nested loops. It explains that a while loop repeats a block of code as long as a condition is true, providing examples of how the while loop works and how it can be used for input validation. Flowcharts are also included to visually depict the logic and flow of while loops.

Uploaded by

Sky Fire
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Control Structure- Repetition

Using Decrement or Increment


While Loop
FOR Loop
Nested Loop

Prepared by Dr Goh Wan Inn


KKSB, FKAAB, UTHM
The Increment and
Decrement Operators
• +=is the increment operator.

It adds one to a variable.

a+=1; is the same as a = a + 1;


The Increment and
Decrement Operators (cont..)
• -= is the decrement operator.

It subtracts one from a variable.

a-=1; is the same as a = a - 1;


Introduction to Loops:
The while Loop
Introduction to Loops:
The while Loop
• Loop: a control structure that causes a
statement or statements to repeat
• General format of the while loop:
while condition:
statement
while Loop – How It Works
while condition:
statement

• condition is evaluated
– if true, then statement is executed, and
expression is evaluated again
– if false, then the the loop is finished and
program statements following statement
execute
The Logic of a while Loop
How the while loop work
Test this condition
number = 1

while number <=5:


print ("Hello") if condition true, then perform
this statement. Repeat until
number+=1 condition is false.

print (“Complete”) Run this statement once


completing the loop.
Flowchart of the Loop
while is a Pretest Loop
• condition is evaluated before the loop executes. The following loop will
never execute:

number = 6
while number <=5:
print ("Hello")
number+=1

print (“Complete”)
Watch Out for Infinite Loops

• The loop must contain code to make


expression become false
• Otherwise, the loop will have no way of
stopping
• Such a loop is called an infinite loop, because it
will repeat an infinite number of times
An Infinite Loop

number = 1

while number <=5:


print ("Hello")

print (“Complete”)
Using the while Loop for
Input Validation
• Input validation is the process of inspecting
data that is given to the program as input and
determining whether it is valid.

• The while loop can be used to create input


routines that reject invalid data, and repeat
until valid data is entered.
Using the while Loop for
Input Validation (cont..)

• Here's the general approach, in pseudocode:

Read an item of input.


While the input is invalid
Display an error message.
Read the input again.
End While
Flowchart using Input Validation
Program Example 1
Program Example 2

# Using while loop for input validation No. 2


print ("")
player = int (input("How many players are available: "))
print ("")
while player<=0 :
print ("Invalid Entry")
player = int (input("Please enter positive integer: "))
print ("")
print ("Valid Entry")

You might also like