ADVERTISEMENT
ADVERTISEMENT

Ruby conditional statements – Find output programs (set 1)

This section contains the Ruby conditional statements find output programs (set 1) with their output and explanations.
Submitted by Nidhi, on February 11, 2022

Program 1:

var = "Hello World";

if (var == "Hello World") {
  print("Hello");
} else {
  print("Hiii");
}

Output:

HelloWorld.rb:3: syntax error, unexpected '{', expecting keyword_then or ';' or '\n'
if (var == "Hello World") {
                          ^
HelloWorld.rb:5: syntax error, unexpected '}', expecting end-of-input
} else {
^

Explanation:

The above program will generate a syntax error due to the syntax of the if statement. The correct program is given below,

var = "Hello World";

if (var == "Hello World")
  print("Hello");
else
  print("Hiii");
end

# Output: Hello

Program 2:

var = "Hello World";

if (var == "Hello World") {
  print("Hello");
} else {
  print("Hiii");
}
end

Output:

HelloWorld.rb:3: syntax error, unexpected '{', expecting keyword_then or ';' or '\n'
if (var == "Hello World") {
                          ^
HelloWorld.rb:5: syntax error, unexpected '}', expecting end-of-input
} else {
^

Explanation:

The above program will generate a syntax error because we cannot use curly braces with an if statement like this. The correct program is given below,

var = "Hello World";

if (var == "Hello World")
  print("Hello");
else
  print("Hiii");
end

# Output: Hello

Program 3:

val = 1;

if (val)
  print("Hello");
else
  print("Hiii");
end

Output:

Hello

Explanation:

In the above program, we created a variable val initialized with 1. Then we used the value of variable val to check the if statement and printed the "Hello" message.


Program 4:

if (0)
  print("Hello");
else
  print("Hiii");
end

Output:

Hello

Explanation:

In the above program, we used the if statement. if statement will be false, only when we use false into if statement.


Program 5:

num = 8.54;

if (num.to_i() % 2 == 0)
  print("Even Number");
else
  print("Odd Number");
end

Output:

Even Number

Explanation:

In the above program, we created a float variable num initialized with 8.54. Then we converted the variable num from float to integer using the to_i() function and checked the condition to find out the resulting number is an Even number or Odd number.

Ruby Find Output Programs »



ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.