IT Questions and Answers :)

Monday, December 11, 2017

What logic construct most efficiently allows you to execute one of a number of code blocks based on the value of a single variable?

What logic construct most efficiently allows you to execute one of a number of code blocks based on the value of a single variable?

  • while
  • if
  • switch
  • for 

What logic construct most efficiently allows you to execute one of a number of code blocks based on the value of a single variable?

EXPLANATION

You can supply a variable to a "switch" statement, and provide a code block for each possible value.
Example using PowerShell:
switch ($var) {
    1 { <# Executes if $var = 1 #> }
    2 { <# Executes if $var = 2 #> }
    3 { <# Executes if $var = 3 #> }
    4 { <# Executes if $var = 4 #> }
    default { <# Executes if $var doesn't match any other case #> }
}
A single "switch" can take the place of a chain of "if/elseif/else".
"for" and "while" are both loops.



Share:

0 comments:

Post a Comment

Popular Posts

Blog Archive