6  Statements and Looping

6.1 Logical Operations

Includes ==, !=, >, <, >=, <=.

Equality: == checks if two values are equal.

Inequality: != checks if two values are not equal.

Greater than: > checks if the value on the left is greater than the value on the right.

Less than: < checks if the value on the left is less than the value on the right.

Greater than or equal to: >= checks if the value on the left is greater than or equal to the value on the right.

Less than or equal to: <= checks if the value on the left is less than or equal to the value on the right.

6.2 Loops

Use for, while.

for loop

The for loop in R is used to iterate over a sequence (like a vector or a list) and execute a block of code for each element in the sequence.

while loop

The while loop executes a block of code as long as the specified condition is TRUE


Summary

Concept Description
Logical Operators
Equality (==) Returns TRUE when two values are equal, FALSE otherwise
Inequality (!=) Returns TRUE when two values are not equal, FALSE otherwise
Greater Than (>) Returns TRUE when the value on the left is strictly larger than the value on the right
Less Than (<) Returns TRUE when the value on the left is strictly smaller than the value on the right
Greater Than or Equal (>=) Returns TRUE when the value on the left is larger than or equal to the value on the right
Less Than or Equal (<=) Returns TRUE when the value on the left is smaller than or equal to the value on the right
Loops
for Loop Iterates a block of code over each element of a known sequence, such as 1:5 or a vector of names
while Loop Repeats a block of code for as long as its condition remains TRUE, useful when the number of iterations is not known in advance