I'm developing a trigger in PL/SQL.
It has this building structure:
if a = 1 then
if b = 1 then
(some code here);
else if b=2 then
(some code here);
else if b=3 then
(some code here);
else b=4
(some code here);
end if;
else if a=2 then
if b=1 then
(some code here);
else if b=2 then
(some code here);
else if b=3 then
(some code here);
else b=4
(some code here);
end if;
else
if b=1 then
(some code here);
else if b=2 then
(some code here);
else if b=3 then
(some code here);
else b=4
(some code here);
end if;
end if;
However, it seems like the last else statement(a=3) is not well defined, cause the compiler says to me that expects a " ( "
Can i have a help in here?
Thank you all
That's because you probably meant to say ELSIF, not ELSE IF (not only at the last place you mentioned, but everywhere).
Something like this:
if a = 1 then
if b = 1 then
(some code here);
elsif b=2 then
(some code here);
elsif b=3 then
(some code here);
elsif b=4
(some code here);
end if;
elsif a=2 then
if b=1 then ...
Related
I am trying to work on inserting if else to loop but it is still expecting some syntax. I cant figure out why this if/else statement wont compile. I am new to VHDL, please help. This is one of the steps along the way and i am stuck.
variable i : integer := 0;
begin
while i < 10 loop
report "i=" & integer'image(i);
i := i + 1;
if i = 6 then
report "okay good job";
else i = 5 then
report " okay nice work";
else
report " not equal to 5 and 6";
end loop;
wait;
end process;
end architecture;
In VHDL, the keywords are if, elsif and else
if condition_1 then
-- do something
elsif condition_2 then
-- do something else
else
-- do something when all other conditions are false
end if;
EDIT: Someone pointed out that I needed to break correctly so I am editing the question
Scenario:
Please see following code:
print "UserID: "
uid = $stdin.gets.chomp
print "Password: "
pwd = $stdin.gets.chomp
usr_inp = "#{uid};#{pwd}"
login_status = -1
# login_info.txt - "#{userid};#{password}" - format
File.open(File.join(File.dirname(__FILE__), 'login_info.txt'), "r") do |f|
f.each_line do |line|
puts line
if (line.chomp == usr_inp)
login_status = 1
elsif (line.chomp != usr_inp && line.include?(uid)) #case a person inputs invalid password
login_status = 0
elsif (line.chomp != usr_inp && !(line.include?(uid))) #case a person inputs an invalid id
login_status = 2
end
end
end
if (login_status == 1)
puts "\nLogged in successfully: #{uid}"
elsif (login_status == 2)
puts "\nSorry, that Employee does not exist."
elsif (login_status == 0)
puts "\nLogin failed.\nPlease check credentials."
end
Problem:
break if (condition) exists in Ruby. But I don't waht that.
I want to do something like:
if (condition x)
(do something)
break
elsif (condition y)
(do something else)
break
else
(whatever)
end
Maybe I am not understanding how ruby code works. Whenever I try to put the break as I want to use it, it associates with the next elsif.
Please help.
It depends on what you need and where you need it.
A script like this:
condition = 1
case condition
when 1
puts 'one'
break
when 2
puts 'two'
else
puts 'Other %s' % condition
end
puts 'end'
has a syntax error. break leaves a loop and there is no loop.
But with a loop, this works:
[1,2,3].each{|condition|
case condition
when 1
puts 'one'
break
when 2
puts 'two'
else
puts 'Other %s' % condition
end
puts 'end'
}
puts 'very end'
The output is:
one
very end
You see, the loop is stopped.
If you want to continue the loop with the next element, you need next (sorry, I'm just not aware what break is doing really in Java - it's been a long time since my last Java program):
[1,2,3].each{|condition|
case condition
when 1
puts 'one'
next
when 2
puts 'two'
else
puts 'Other %s' % condition
end
puts 'end %s' % condition
}
puts 'very end'
The result:
one
two
end 2
Other 3
end 3
very end
When you are not inside a loop (like in your code snippet), you may use exit (leave the program) or return (leave a method).
c = 5
until c == 0 do
print c
c -= 1
end
/break
c = 5
until c == 0
print c
c -= 1
end
What's the difference?
Both of them display 54321 as output.
do is optional. It indicates the beginning of the block of code to be repeatedly executed.
In your example it makes no difference. However if you try re-writing the code in one line, you can see why its needed:
c = 5
until c == 0 do print c; c -= 1 end
# 54321 => nil
Now try this without do:
c = 5
until c == 0 print c; c -= 1 end
# SyntaxError: (irb):115: syntax error, unexpected tIDENTIFIER, expecting keyword_do_cond or ';' or '\n'
As you can see there is no clear beginning for block, Ruby will throw a SyntaxError.
There is no difference between until and until do. Do is optional and both will show same output. If want to get some more idea about this while and while do
There is absolutely no difference. It is like the while [condition] [block] end vs while [condition] do [block] end or if [condition] [block] end vs if [condition] then [block] end.
In Ruby, often there are tons of ways of doing the same thing - the important is to be consistent, either way.
According to The Ruby Programming Language p.164.
If a begin statement doesn't propagate an exception, then the value
of the statement is the value of the last expression evaluated in
the begin, rescue or else clauses.
But I found this behavior consistent with the begin block together with else clause and ensure clause.
Here is the example code:
def fact (n)
raise "bad argument" if n.to_i < 1
end
value = begin
fact (1)
rescue RuntimeError => e
p e.message
else
p "I am in the else statement"
ensure
p "I will be always executed"
p "The END of begin block"
end
p value
The output is:
"I am in the else statement"
"I will be always executed"
"The END of begin block"
"I am in the else statement"
[Finished]
The value is evaluated to the else clause. This is inconsistent behavior as the ensure clause is the last statement executed.
Could someone explain what's happening within the begin block?
I'd interpret the goal of the begin/rescue/else/end block as:
Execute the code in the begin section, and then the code in the else section.
If something goes wrong in the begin section, execute the rescue section instead of the else section.
So either the rescue section or the else section will be executed after trying the begin section; so it makes sense that one of them will be used as the whole block's value.
It's simply a side effect that the ensure section will always be executed.
val = begin
p "first"; "first"
rescue => e
p "fail"; "fail"
else
p "else"; "else"
ensure
p "ensure"; "ensure"
end
val # => "else"
# >> "first"
# >> "else"
# >> "ensure"
But:
val = begin
p "first"; "first"
raise
rescue => e
p "fail"; "fail"
else
p "else"; "else"
ensure
p "ensure"; "ensure"
end
val # => "fail"
# >> "first"
# >> "fail"
# >> "ensure"
I'm just guessing here, but as the purpose of a ensure block is to finalize any resources that may remain open (cleanup in other words), and so it makes sense that the logical value should be the result of the else statement. It makes sense to me that it is by design.
In this case the begin block is just a way of defining a section for which you may want to do exception handling.
Remember that else in this case runs if no exceptions occur, and ensure will run regardless of exceptions or a lack thereof.
I have a background from languages which use {} to say that these are "block of statements" but i am learning ruby and really confused how it being done there.
So lets say in C i have
if ( condition )
{
statement1;
statement2;
}
else if (condition)
{
statement1;
statement2;
// nested if
if (condition)
{
dosomethinghere;
}
}
else
{
statement1;
statement2;
}
How i can put this code in ruby? and also mention how to understand synomenous of "{}" in ruby, thanks.
Well, to answer your first question:
if ( condition )
statement1
statement2
elsif (condition)
statement1
statement2
// nested if
if (condition)
dosomethinghere
end
else
statement1
statement2
end
The syntax rule for if statement is:
if expr [then]
expr...
[elsif expr [then]
expr...]...
[else
expr...]
end
Where everything between [] is optional
However, and in other direction, you can also create and pass code blocks, check this post to read more about this topic.
The ruby syntax for if is:
if condition
body
else
alternativa body
end
Or
if condition then body else alternative body end
It's the same for while loops except with do instead of then.
{ and } are used to pass anonymous functions (confusingly called "blocks" in ruby) as arguments to methods.
I'd suggest getting a decent book and sitting down and reading the first few chapters this should cover everything you asked here and a lot more. I'd suggest http://oreilly.com/catalog/9780596529864 although if you're trying to get something done really quick http://www.troubleshooters.com/codecorn/ruby/basictutorial.htm is quite a good brief intro to get you started.
in Ruby, the opening brace is implied after an if. to close the block, you use an end instead of a close brace. the only other difference is you use elsif (condition) instead of else if (condition).
If you're thinking "how do I create a new variable scope in Ruby"? ie:
{
var myvar = 1;
}
myvar = 2; // compile error because myvar isn't in this scope!
I'm not really sure how you would do that.
try running the following:
def example(x,y)
puts "X:#{x},Y:#{y}"
if ( x == 0 ) then
puts "Its true"
elsif (x == 1)
puts "Its not true"
puts "it certainly isn't"
if (y == 0) then
puts "i'm the nested if"
end
else
puts "i made it to the default case"
puts "freedom"
end
puts
end
example(0,0)
example(1,0)
example(1,1)
example(2,2)
If you want a scope you can define your own scope method:
def scope
yield
end
# use like this
scope {
x = 5
puts x #=> 5
}
x #=> undefined local variable
EDIT: for a better approach to 'scopes' in Ruby 1.9 see: http://banisterfiend.wordpress.com/2010/01/07/controlling-object-scope-in-ruby-1-9/