<jupyter_start><jupyter_text>LOOP                                           There may be a situation when you need to execute a block of code several number of times. In general, statements are executed sequentially. The first statement in a function is executed first, 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:                                                                            Loop Control Statements                                                            Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed.R supports the following control statements:                                                        While Loop                                             The While Loop executes the same code again and again until a stop condition is met.                                                                                Syntax:                                                            The basic syntax for creating a while loop in R is:                     while (conditions) {    statements}                                         Flow Diagram:                                                                               Here key point of the While Loop is that the loop might not ever run. When the condition is tested and the result is FALSE, the loop body will be skipped and the first statement after the while loop will be executed.                                                             Example:<jupyter_code># While Loop Example 01:

cat("Start", "\n")

i <- 1

while (i < 6) {
    cat(i, "\n")
    i = i + 1
}

cat("Finish", "\n")
# While Loop Example 02:

num <- 0

while(num != 6) {
    num <- sample(1:6, 1)
    cat("Number of Dice:", num, "\n")
}

cat("You Win!", "\n")<jupyter_output>Number of Dice: 1 
Number of Dice: 5 
Number of Dice: 1 
Number of Dice: 5 
Number of Dice: 4 
Number of Dice: 3 
Number of Dice: 4 
Number of Dice: 3 
Number of Dice: 6 
You Win!<jupyter_text>For Loop                                             A For Loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.                                                                                Syntax:                                                            The basic syntax for creating a for loop statement in R is:                     for (value in vector) {    statements}                                         Flow Diagram:                                                                               R’s for loops are particularly flexible in that they are not limited to integers, or even numbers in the input. We can pass character vectors, logical vectors, lists or expressions.                                                             Example:<jupyter_code># For Loop Example 01:

countries <- c('India', 'UK', 'Japan', 'USA', 'Australia', 'China')

for (str in countries) {
  cat("Countries are:", str, "\n")
}

cat("----This is Coming from Outside the For Loop----", "\n")
# For Loop Example 02:

v <- LETTERS

for ( i in v) {
    cat(i, "\n")
}
# For Loop Example 03:

x <- c(2, 5, 3, 9, 8, 11, 6)

count <- 0

for (val in x) {
    if(val %% 2 == 0) {
        cat(val, "\n")
        count = count + 1        
    }  
}

cat("#", count)<jupyter_output>2 
8 
6 
# 3<jupyter_text>Break Statement                                             The break statement in R programming language has the following two usages:1. When the break statement is encountered inside a loop, the loop is immediately terminated and program control resumes at the next statement following the loop.2. It can be used to terminate a case in the switch statement (covered in the next chapter).                                                                                Syntax:                                                            The basic syntax for creating a break statement in R is:                     if (test_expression) {    break}                                         Flow Diagram:                                                                               Example:<jupyter_code># Break Statement Example 01:

x <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

for (num in x) {
    if (num == 7) {
        cat("Coming out from for loop Where num =", num, "\n")
        break
    }
    cat("Num are : ", num, "\n")
}<jupyter_output>Num are :  1 
Num are :  2 
Num are :  3 
Num are :  4 
Num are :  5 
Num are :  6 
Coming out from for loop Where num = 7<jupyter_text>Next Statement                                             The next statement in R programming language is useful when we want to skip the current iteration of a loop without terminating it. On encountering next, the R parser skips further evaluation and starts next iteration of the loop.                                                                                Syntax:                                                            The basic syntax for creating a next statement in R is:                     if (test_expression) {    next}                                         Flow Diagram:                                                                               Example:<jupyter_code># Next Statement Example 01:

x <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

for (num in x) {
    if (num == 7) {
        cat("Coming out from for loop Where num =", num, "\n")
        next
    }
    cat("Num are : ", num, "\n")
}
# Next Statement Example 02:

number <- 1:16

count <- 0

for (val in number) {
    if (val %% 2 != 0) {
        cat("ODD Number = ", val, "(Skipped by Next Statement)", "\n")
        next
    }
    cat("EVEN Number =", val, "\n")
    count <- count + 1
}

cat("Number of EVEN:", count, "\n")<jupyter_output>ODD Number =  1 (Skipped by Next Statement) 
EVEN Number = 2 
ODD Number =  3 (Skipped by Next Statement) 
EVEN Number = 4 
ODD Number =  5 (Skipped by Next Statement) 
EVEN Number = 6 
ODD Number =  7 (Skipped by Next Statement) 
EVEN Number = 8 
ODD Number =  9 (Skipped by Next Statement) 
EVEN Number = 10 
ODD Number =  11 (Skipped by Next Statement) 
EVEN Number = 12 
ODD Number =  13 (Skipped by Next Statement) 
EVEN Number = 14 
ODD Number =  15 (Skipped by Next Statement) 
EVEN Number = 16 
Number of EVEN: 8<jupyter_text>Repeat Loop                                             The Repeat Loop executes the same code again and again until a stop condition is met.                    There is no condition check in repeat loop to exit the loop.We must ourselves put a condition explicitly inside the body of the loop and use the Break Statement to exit the loop. Failing to do so will result into an infinite loop.                                                                                Syntax:                                                            The basic syntax for creating a repeat loop in R is:                     repeat {    statement    if(condition) {        break    }}                                         Flow Diagram:                                                                               Example:<jupyter_code># Repeat Loop Example 01:

repeat {
    num <- sample(1:6, 1)
    
    cat("Number of Dice:", num, "\n")
    
    if(num == 6) {
        cat("You Win!", "\n")
        break
    } else {
        cat("You Loss!", "\n")        
    }
}
# Repeat Loop Example 02:

total <- 0

number <- as.integer(readline(prompt="Please Enter Any Integer Value Below 10: "))

repeat  {
  total = total + number
  number = number + 1
  if (number > 10)  {
    break
  }
}

cat("The Total Sum of Numbers From the Repeat Loop is:", total)<jupyter_output>Please Enter Any Integer Value Below 10: 1
The Total Sum of Numbers From the Repeat Loop is: 55
{'fraction_non_alphanumeric': 0.0686060606060606, 'fraction_numerical': 0.016, 'mean_word_length': 1.3682548794489093, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 76, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '458808', 'license': 'MIT', 'n_tokens_mistral': 2026, 'n_tokens_neox': 1854, 'n_words': 935, 'path': 'Week 03/.ipynb_checkpoints/R_08_Data_Structures-checkpoint.ipynb', 'repo_name': 'shirazipooya/RCourse', 'size': '8250'}