The Case Control Structure Flashcards

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

switch-case-default

A

Allows us to make decisions from many choices.

switch (int output Exp)
case (constant 1) : { statement(s); }
break;
case (constant 2) : { statement(s); }
break;
default : { statement(s); }
If no case satisfied then default is executed. Case satisfied when that int matches with constant of case.

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

Use of break in switch-case-default

A

Its use is optional. If break is not used then the case which satisfies will get executed and all the subsequent cases and finally default.

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

Some tips of switch:

A
  1. Order of cases can be random.

2. u can use char value in case and switch. As said before, the use of ASCII values happens.

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

Execute the same set of Statements for multiple cases:

A
switch (int output Exp)
   case (constant 1): 
   case (constant 2) : { statement(s); }
                     break;
   case (constant 3):
   case (constant 4) : { statement(s); }
                     break;
                  default : { statement(s); }

Cuz remember once a case satisfied everything is executed until break is encountered or switch block completes.

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

Braces and case?

A

Well no need of braces for a block of case, cuz since satisfied it will run till break occurs or switch block is completed. But u can, y not, for neatness.

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

What if there is a statement which does not belong to any case?

A

Then compiler won’t report an error but its never executed.

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

What if no default case:

A

then program simply falls through entire switch and instruction after the closing brace of switch is executed if any.

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

Advantage of if Over Switch:

A

The case cant be i <= 20, it has be something that gets evaluated to int or char value not even float.

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

Advantage of switch over if:

A

Neat, easy life.

more so if multiple statements within each case.

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

Case expressions:

A

constant exp allowed.
case a + b: invalid
case 3+7: valid.

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

Continue and switch:

A

use of continue will not take the control to the beginning of switch.

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

nested switch:

A

Allowed but not preferred.

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

same expressions case?

A

It illegal.

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

Why is switch used even though it has so many limitations:

execution of switch by compiler:

A

Compiler generates a jump table for switch during compilation. compiler simply looks at jump table to decide which case to execute rather than actually checking if each case is satisfied.

Lookup in a jump table is faster than evaluation of a condition (10)especially complex one.

but simple and less conditions (2) in if-else would work faster than corresponding switch.

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

exit() function:

A

Standard lib function.

Terminates the execution of the program.

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

About goto:

why use it or avoid it:

A

The writer says, avoid it. As we can never be sure how we got to a certain point and it can always be avoided with good programming skills. It makes debug tedious.

Its preferred when we want to take control out of loop which is contained in several loops.

17
Q

goto example:

A
main()
{
  for(i=0;i<=3;i++)
   {
       for(j=0;j<=3;j++)
        {
             for(k=0;k<=3;k++)
                {
                   if(i==3&amp;&amp;j==3&amp;k==3)
                     goto out;
                   else
                       print("%d%d%d",i,j,k);
                  }
          }
   }
  exit()
  out:
        printf("Woo all are equal");
}