Switch & if-else

Which of the two is better, If-else or switch ? Everyone say Switch is better than if-else. But why ?

A sequence of if-else statement is translated as a sequence of labels and goto statements.

But for a switch statement, a compiler generates an internal table to find the matches at run time. Depending upon the constants in the case statement the table can be look up or range table. If the constants are unrelated the comparison are usualy done at the beggining and jump is made to specific entry in the table [lookup table]. If the constants are related and within a range [eg : ‘0’ to ‘9’ ] jump can be made to each range of values [i.e., the range table].

Div and Span

Div

  • Div is a block element
  • Takes as much of the width of the page as it can. It blocks out the whole section of the page, so that anything following it has to go to the next line beneath it.
  • Causes a paragraph break

<html>
<body>
<h1>Krishna's stumble's can be found <div> <a href = "https://krishnakummar.wordpress.com">here</a> <div></h1>
</body>
</html>

Div Tag Output
Div Tag Output

Span

  • Span is an inline element.
  • Takes as little space as possible, only enough to contain its contents.
  • Does not cause any paragraph break, will be inline


<html>
<body>
<h1>Krishna's stumble's can be found <span> <a href = "https://krishnakummar.wordpress.com">here</a> <span></h1>
</body>
</html>

Span Tag Output
Span Tag Output