Question

Language C How does a switch differ from an if? What is a switch's controlling expression?...

Language C

How does a switch differ from an if?

What is a switch's controlling expression?

What happens in a switch if the break's are not used?

Why might if(0 <= y <= 9) not work as it might be expected?

How does the ?: operator work?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Dear Student,

here are the answer..

Ans:1

Switch stament does not compare the values it recieved a value and match the case as per the value recieved while and if condition is used to check the validation of the statement passed inside it.

Ex:

int num = 1;

switch(num)

{

case 1:

------

break;

case 2:

-------

break;

case 3:

-------

break;

default:

--------

break;

}

Example of if condition:

if(num>1)

{

statement..

}

==============================================================

Ans:2

Example of switch controlling expression

if num = 1

switch(num)

{

case 1:     //switch stament executes this case and avoid further cases.

------

break;

case 2:

-------

break;

case 3:

-------

break;

default:

--------

break;

}

===============================================================

If break condition is not used in cases then switch stament first goes to that case and executes all the furtehr cases.

Ex: In the below example control goes to case 1 and becasue we have not used the break so all the cases after case 1 got executed.

switch(num)

{

case 1:

------

case 2:

-------

case 3:

-------

default:

--------

break;

}

===============================================================

if(0 <= y <= 9) //this will not work because this is not the correct way in programming language.

The correct statement would be..

if(y>= 0 && y <=9)

=============================================================

?: this is called the ternary operator.

Ex:

int num = 5

int res = num > 0 ? printf("true") : printf("False");

in the above given expression if num value is greter than zero then res will be true otherwise res would be false.

================================================================

KIndly Chek and Verify Thanks...!!!

Add a comment
Know the answer?
Add Answer to:
Language C How does a switch differ from an if? What is a switch's controlling expression?...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT