Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
5 views4 pages

Module 2.8

The document explains loop statements in programming, specifically in R, which allow for executing code multiple times based on conditions. It covers three types of loops: repeat, while, and for loops, providing examples for each. The document illustrates how to use these loops to perform tasks such as counting adult customers and simulating coin flips.

Uploaded by

Squall Lionheart
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)
5 views4 pages

Module 2.8

The document explains loop statements in programming, specifically in R, which allow for executing code multiple times based on conditions. It covers three types of loops: repeat, while, and for loops, providing examples for each. The document illustrates how to use these loops to perform tasks such as counting adult customers and simulating coin flips.

Uploaded by

Squall Lionheart
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/ 4

2.

8 LOOP STATEMENTS
Tof here may be a situation when you need to execute a block
code several number of times. In general, statements are
executed sequentially. The rst statement in a function is
executed rst, followed by the second, and so on.

Programming languages provide various control


structures that allow for more complicated execution paths. A
loop statement allows us to execute a statement or group of
statements multiple times and the following is the general
form of a loop statement in most of the programming
languages.

R programming language provides the following kinds of


loop to handle looping requirements. Loops enable you to
execute a block of code multiple times, either a
predetermined number of times or until a speci c condition is
met.

Page 71 of 77
fi
fi
fi
Loop Type & Description
repeat loop
Executes a sequence of statements multiple times and
abbreviates the code that manages the loop variable.
while loop
Repeats a statement or group of statements while a given
condition is true. It tests the condition before executing the
loop body.
for loop
Like a while statement, except that it tests the condition at
the end of the loop body.

For Loop:
Imagine you have a dataset with customer ages and need
to calculate the total number of customers above 18. The for
loop allows you to iterate through a sequence of values,
executing speci c code for each element. Here's the basic
structure:

for (i in sequence) {
# Code to execute for each element in the sequence
}

The example code presented below iterates through the


customer_ages vector using the for loop. For each age, it
checks if it's greater than or equal to 18 using an if statement.
If true, the adult count is incremented. Finally, it prints the
total number of adults.

customer_ages <- c(25, 19, 32, 17, 21)


adult_count <- 0 # Initialize a variable to store the adult count

Page 72 of 77
fi
for (age in customer_ages) {
if (age >= 18) {
adult_count <- adult_count + 1 # Increment adult count when age >= 18)
}
}
print(paste("There are", adult_count, "adult customers.”))

While Loop:
The while loop keeps executing a block of code as long as a
speci c condition remains TRUE. Here's the structure:

while (condition) {
# Code to execute as long as the condition is TRUE
}

This example uses a while loop. It keeps ipping a coin


(simulated by sampling from a character vector) until the
heads ag becomes TRUE (when "Heads" is ipped). The
loop keeps track of the number of ips using a counter
variable.

flips <- 0 # Initialize a counter for the number of flips


heads <- FALSE #Initialize a flag for heads

while (!heads) {
flip_result <- sample(c("Heads", "Tails"), 1, replace = TRUE)
# Simulate a coin flip
flips <- flips + 1
heads <- flip_result == "Heads" # Update the flag for heads
}
print(paste("It took", flips, "flips to get Heads."))

Page 73 of 77
fi
fl
fl
fl
fl
Repeat Loop:
The repeat loop executes a code block at least once and
then continues to iterate as long as a condition is TRUE. It’s
crucial to include a way to break out of the loop to avoid
in nite loops. Here's the structure:

repeat {
# Code to execute at least once
} until (condition)

This example uses a repeat loop to continuously prompt


the user for their name until they enter "quit". The break
statement allows the user to exit the loop by entering the
speci c keyword.

repeat {
# Prompt the user for their name
user_input <- readline(prompt = "Enter your name (or type 'quit' to exit): ")

# Check if the user entered ‘quit'


if (user_input == "quit") {
# Exit the loop
cat("Exiting the loop. Goodbye!\n")
break
}
# If the user did not enter 'quit', display the entered name
cat("Hello, ", user_input, "!\n", sep = "")
}
Take note that this example script only exhibits the use of
the repeat loop and that it. Although it stores the inputted
data to the user_input variable, it cannot store all the values!
The variable only holds one value, and that is the last
inputted value.

Page 74 of 77
fi
fi

You might also like