Control Structure Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What are the conditional statements available in Ruby?

A

1) if
2) Case
3) unless

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How we can use if condition?

A

If conditon can be used in threee variants
if
end

if
else
end

if
elsif
else
end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is very popular one line condtitional statement?

A

tenary operator

condition ? true : false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Can case statements be assigned to any variable?

A

yes. This value can be assigned to a variable preceding the case statement.

happy = case

when …

end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the alternative way of expressing ‘if not’.?

A

unless

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What the looping structures available in Ruby?

A

for,until,while,loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Is each a loop?

A

no, its an iterator.Also it can be used as built in methods in classes like array,hash

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How we can use for loop?

A

It can be used in three different ways.

for i in 1..10
end

for i in 1..10 do
end

single line

for i in 1..10 do end - do is mandatory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

give the while loop alternate syntax?

A
i = 0
begin
	puts(arr[i])
	i += 1
end while i < arr.length
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

give the until loop alternate syntax?

A
i = 0
until i == arr.length 
	puts(arr[i])
	i +=1
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How “loop” keywords can be used in Ruby?

A

loop do

end

loop {

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a statement modifier?

A

A condition which follows a statement such as with x = 1 if a == true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Does the case statement in Ruby have fall-through behavior?

A

No. Fall through means , control moves to the matching case, and then execution continues (“falls through”) to the statements associated with the next case. fallthrough is usually prevented with a break keyword at the end of the matching body

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are some advantages of a case statement versus repeated elsif statements?

A

It shows intent. It DRY’s out the condition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Why might you use #each instead of for/in?

A

It’s the “Ruby way” - an example of how Ruby defines methods that mimic natural language concepts. Iterator methods such as #each read more naturally. #each is a block so it defines a new variable scope. for/in depends on the existence of #each which implies that #each is a more fundamental aspect of the language.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly