VHDL Questions and Answers – IF Statement

«
»

This set of VHDL Multiple Choice Questions & Answers (MCQs) focuses on “IF Statement”.

1. What kind of statement is the IF statement?
a) Concurrent
b) Sequential
c) Assignment
d) Selected assignment
View Answer

Answer: b
Explanation: IF statement is a sequential statement which appears inside a process, function or subprogram. This statement is used to execute some block of statements if a condition executed comes to be true.
advertisement

2. Which of the following keyword is not associated with IF statement?
a) ELSE
b) THEN
c) ELSIF
d) WHEN
View Answer

Answer: d
Explanation: The IF statement can use the keywords ELSIF, THEN and ELSE but not the keyword WHEN. IF statement is followed by a condition which is followed by the keyword THEN. After which to add more conditions one can use ELSIF and ELSE keywords.

3. Which of the following represents the correct order for keywords?
a) IF, THEN, ELSIF, THEN, ELSE
b) IF, ELSE, THEN, ELSIF, THEN
c) IF, ELSIF, THEN, ELSE, THEN
d) IF, THEN, ELSE, THEN, ELSIF
View Answer

Answer: a
Explanation: In case of IF statement, the keyword IF is followed by the condition and then the keyword THEN. After this any other condition is entered by using ELSIF keyword and all the other exceptions are handled by using ELSE keyword. So, the correct order is shown in option a which is IF, THEN, ELSIF, THEN, ELSE.

4. What is the correct syntax for defining an IF statement?
a)

    IF (condition) THEN
    statements;
    ELSIF (condition) THEN
    statements;
    ….;
    ELSE (condition) THEN
    statements;
    END IF;

b)

    IF (condition) THEN
    statements;
    ELSIF (condition) THEN
    statements;
    ….;
    ELSE 
    statements;
    END IF;

c)

advertisement
    IF (condition) THEN
    statements;
    ELSIF (condition) THEN
    statements;
    ….;
    ELSE (condition)
    statements;
    END IF-ELSE;

d)

    IF (condition) THEN
    statements;
    ELSIF (condition) THEN
    statements;
    ….;
    ELSE 
    statements;
    END IF-ELSE;
View Answer
Answer: b
Explanation: For a sequential IF statement, the condition is evaluated and if it is found to be true then the statements under IF are executed and after that the sequence of ELSIFs is used and finally an ELSE is used and it is ended by using END keyword followed by IF.
 
 

5. If the condition of IF statement is an expression, then what should be the type of the result of the expression?
a) Bit
b) Std_logic
c) Boolean
d) Integer
View Answer

Answer: c
Explanation: It doesn’t matter what is the type of the expression, the result must be of Boolean type. It can have only two values which may be either TRUE or FALSE. If the result is true, THEN the statements under IF are executed otherwise ELSE is executed.

6. In the following lines, what should be the value of signal y, if a and b both are at logic high?

PROCESS (a, b)
BEGIN
IF( a XOR b <=1)
y <=1;
ELSIF (a AND b <=0)
y &lt;= a;
ELSE
y &lt;=0;
END IF;
END PROCESS;

a) a
b) b
c) 0
d) 1
View Answer

Answer: c
Explanation: At the time of synthesis, first the condition of IF statement is tested and it is found to be FALSE, so the statements under IF statements are skipped and the condition of ELSIF is tested. That condition again comes to be FALSE and hence the statement under else is executed. So, y is assigned 1.
advertisement

7. It is possible to use nested IF in VHDL.
a) True
b) False
View Answer

Answer: a
Explanation: Like other traditional languages, it is possible to use an IF statement inside another IF statement. In this case, when one IF statement is used inside another IF statement, this is called the nested IF statement. This allows to use more than one condition simultaneously.

8. Which of the following condition has topmost priority?
a) IF
b) ELSIF
c) ELSE
d) THEN
View Answer

Answer: a
Explanation: IF has the topmost priority which means the remaining block will be executed only if the condition under IF gives FALSE. Otherwise, if it is true, then the block is shifted until END IF statement. After the IF condition, the next priority is ELSIF condition. ELSE is executed only if every preceding condition is FALSE.

9. What logic is described in the following logic?

PROCESS (a, b)
IF (a =1AND b =0OR a=0AND b =1) THEN
y &lt;=1;
ELSIF (a =1AND b=1) THEN
y &lt;=0;
ELSE
 y &lt;=0;
END IF

a) EXOR
b) EXNOR
c) AND
d) NOR
View Answer

Answer: a
Explanation: Here, in the given code, the output is 1 if either a is 1 or b is 1. In the ELSIF, the condition is that the output will be zero if both the inputs are 1. So, both inputs can’t be high at the same time. Therefore, the logic described is exclusive OR logic.

10. One IF statement can have multiple ___________
a) IF
b) ELSIF
c) ELSE
d) CASE
View Answer

Answer: b
Explanation: It is possible to have multiple ELSIF parts within one IF – END IF block. The IF statement can have multiple ELSIF parts but can have only one ELSE statement part. ELSE part will be executed after each of the ELSIF part is checked and found to be FALSE.

11. More than one sequential statement can exist between each statement part.
a) True
b) False
View Answer

Answer: a
Explanation: Yes, It is not necessary that within a single IF or ELSIF, only one sequential statement is allowed. Multiple statements can be there between each statement part. Unlike, traditional languages it doesn’t need {} to write multiple statements.
advertisement

12. If a user gets an error at the time of simulation which is “ the IF statement is illegal” what could be the reason?
a) Using IF statement in architecture body
b) Using IF statement without ELSE
c) Using multiple ELSE statements
d) Using concurrent assignment in the IF
View Answer

Answer: a
Explanation: It is not allowed to use IF in the architecture body directly. IF statement is a sequential statement and hence can be used in a process, function or procedure. However, using IF statement without ELSE is not any error, doing this is possible. The only error is that it can’t be used in the concurrent.

13. In a clocked process, IF statement is used to __________
a) To run statements sequentially
b) To use concurrent assignment within process
c) To detect the clock signal
d) To implement sequential circuit
View Answer

Answer: c
Explanation: A clocked process is the process which uses a clock signal to design the circuit. In such process one may need to detect the rising or falling edge of the clock. For this purpose, IF statement can be used to detect the occurrence of a clock.

14. What will be the output in the following code?

ARCHITECTURE my_logic OF my_design IS
BEGIN
a &lt;= 1;
b &lt;= 1;
PROCESS (a, b)
BEGIN
IF (a AND b = 1) THEN
output &lt;= a;
ELSIF (a OR b = 1) THEN
output &lt;= b;
ELSE
output &lt;= 0;
END IF;
END PROCESS;
END my_logic;

a) 0
b) 1
c) b
d) a
View Answer

Answer: d
Explanation: Since the condition under IF is true so the statements under IF will be executed and hence output will be assigned the value of signal a. Though the condition under ELSIF is also TRUE but IF has the highest priority so all the following ELSIFs will be ignored. This is the problem in IF statement.

Sanfoundry Global Education & Learning Series – VHDL.

To practice all areas of VHDL, here is complete set of 1000+ Multiple Choice Questions and Answers.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn