# ------------------------------------------------------------------
# File name: if_else.R
#
# This code asks the user to enter a number.
# If the number is non-negative, it prints the sqrt of the number;
# otherwise, it exits with a message.
# Decision is accomplished with an if() else statement
#
# Version: 2.1
# Authors: H. Kocak, University of Miami and B. Koc, Stetson University
# References:
# https://www.r-project.org
# ------------------------------------------------------------------
cat("Please enter a number to compute its square root \n")
# To get input from keyboard (single item)
x = scan(nmax = 1, quiet = TRUE)
cat("The number you have entered is", x, "\n")
# Else must be on the same line as the closing brace of if
if(x >= 0) {
cat("square root of", x, "is", sqrt(x), "\n")
} else {
cat("Sorry, please enter a nonnegative number \n")
}
cat("Bye!. \n")