Please disable your adblock and script blockers to view this page

Search this blog

Showing posts with label While Loop. Show all posts
Showing posts with label While Loop. Show all posts

Tuesday 14 August 2018

 Loops (R Loops) are used to repeat the execution of a block of statements based on some predefined conditions. In R Programming we have the following types of loops that can be used as per development requirements.



Repeat Loop in R Programming

Repeat loop in R executes the set of statements until the defined condition is satisfied

  1. #Print some value using Repeat Loop
  2. count <- 1
  3. repeat {
  4. print("R Programming")
  5. count <- count+1
  6. if(count > 5) {
  7. break
  8. }
  9. }

and output is

  1. [1] "R Programming"
  2. [1] "R Programming"
  3. [1] "R Programming"
  4. [1] "R Programming"
  5. [1] "R Programming"

Another Example

  1. #Print table of 10 using Repeat Loop
  2. a <- 10
  3. count <- 1
  4. repeat {
  5. print(a*count)
  6. count <- count+1
  7. if(count > 10) {
  8. break
  9. }
  10. }
and output is

  1. [1] 10
  2. [1] 20
  3. [1] 30
  4. [1] 40
  5. [1] 50
  6. [1] 60
  7. [1] 70
  8. [1] 80
  9. [1] 90
  10. [1] 100

While Loop in R Programming

While loop checks the condition before starting loop execution and executes the set of statements while the defined condition is true.

  1. #Declare and Initialise a varable
  2. a <- 1
  3. #Check condition in a while loop
  4. while(a<=5){
  5. print(a)
  6. a <- a+1
  7. }

and output is

  1. [1] 1
  2. [1] 2
  3. [1] 3
  4. [1] 4
  5. [1] 5

FOR Loop in R Programming

When you need to execute a code a specific number of times then FOR loop comes into action. Here we'll see how to iterate a vector using FOR loop.

  1. #Declare and Initialise a Vector
  2. a <- c(10,TRUE,"Ashish",2.4)
  3. #Iterate vector using FOR loop
  4. for(i in a){
  5. print(i)
  6. }

and output is

  1. [1] "10"
  2. [1] "TRUE"
  3. [1] "Ashish"
  4. [1] "2.4"

Cheers 🙂 Happy Learning

Friday 23 March 2018

PL/SQL While Loop


PL/SQL WHILE loop is used to execute statements as long as given condition is true and the condition is checked at the beginning of each iteration

The syntax of PL/SQL While loop is like this

WHILE condition

statements to execute

END LOOP;

PL/SQL Loops , Iterative Statement in PL/SQL


Loops are used to repeat execution of a statement or a set of statements multiple times on base of a condition or expression
EXIT and EXIT-WHEN keywords are used to terminate the loop

EXIT- terminates the loop unconditionally and passes control to the next statement after the loop
EXIT-WHEN- terminates the loop when EXIT-WHEN clause is checked and if returns true then the loop is terminated and control is passed to next statement after the loop

The basic syntax of Loop in PL/SQL is like this

LOOP

Set of statements

END LOOP;