R programming - Control Structures and Output Functions
A program for statistical analysis - Part 3. Let's explore Control Structures and Output Functions in this blog.
We discussed variables, variable scope, and input functions in the previous blog. In this article, we can learn about output functions and control structures.
Using scan() method
As we have seen earlier that we can use two functions to obtain input. we have seen about readline() in the previous blog. Now we can learn about scan() function. This function is ideal for taking inputs faster and is used for mathematical calculations.
Syntax
var = scan()
var = scan()
print(var)
//output
123 - entered by the user
A point to be noted while using this function is that to terminate the input we should tap enter key twice.
Output Statements in R
The most common method to print the output is the print() function.
Syntax
print(var_name) or print("string")
"Quasar Community" -> var
print(var)
Print("Quasar Community")
//output
"Quasar Community"
"Quasar Community"
Using print function with paste()
In R, a function called paste() prints string along with variables.
Syntax
print(paste("string", var_name))
"Quasar community" -> x
print(paste("Empowering community with software -", x)
//output
"Empowering community with software - Quasar community"
Sending output through file:
There is a function called write(). This function can write a variable as data in a text file.
Syntax
write.table(var_name,file="file_name.txt)
var <- "Quasar community"
write.table(var,file="data1.txt")
//output
note: a text file will be opened with the specified name and the specified variable is printed on it.
Decision-making statements in R
Decision-making statements are special statements which work based on the condition given by the user. If the condition is evaluated to be true some statements will be executed and if it is false a different set of statements will be executed.
if condition
This is the most basic control structure which evaluates the test condition and if it's true the statements inside the braces will be executed.
Syntax
if(test-condition)
{
statement 1
statement 2
..........
}
100->var
if(var > 50)
{
print("The Given Number is Greater")
}
//output
"The Given Number is Greater"
if-else condition
The if-else condition is the same as the if statement, but the difference is that there will be two sets of statements, one set to be executed when the test condition is evaluated to true and one set is executed when the test condition is false.
Syntax
if (test-condition)
{
statement 1
statement 2
..........
}
else{
statements
....................
....................
}
var <- 10
if(var > 20)
{
print( "is greater than 20")
}
else
{
print("is lesser than 20")
}
//output
"is lesser than 20"
if-else-if ladder
The if-else-if ladder is the same as the if-else statement but the difference is that if the test condition in the if statement is evaluated to true the statements inside the if block gets executed. If the test condition is false then the else-if block's test condition is evaluated, if the test condition is true then the statements in the particular if block is executed and it goes on like this.
Syntax
if(test condition1)
{
statements
}
else if(test condition2)
{
statements
}
else
{
statements
}
var1 = 10
var2 = 20
var3 = 30
if(var1 > var2 && var2> var1)
{
print(" Var1 is greater than the two")
}
else if(var1 < var2 && var2 > var1)
{
print("Var2 is greater than the two")
}
else if(var1 < var2 && var2 < var1)
{
print("Var3 is greater than the two")
}
//output
"Var3 is greater than the two"
Nested if-else
If we have an if-else block inside an if-else block or within the else block then it is known as a nested if-else statement. If the if statement is evaluated to be false then the else statement is executed. This happens with the first If-else statement, which is the parent If-else statement. similarly, the if or else statement has another if or else statement nested in any of the two statements.
Syntax
if(First test condition)
{
if(nested if condition 1)
{
statement
}
else
{
statement
}
}
else
{
if(nested if condition 2)
{
statement
}
else
{
statement
}
}
var1 = 5
var2 = 10
if(var1==5)
{
if(var2==10)
{
print("a-5 b-10")
}
else
{
print("a-10 b-10")
}
}
//output
"a-5 b-10"
That's it for today's blog, we will see about switch statements and loops in the next blog. Follow us for more such blogs.