x

Choose Country Code

x

Direction

x

Ask a Question

  • Ask a Question
  • Scan a Question
  • Post MCQ
  • Note: File extension must be of jpg, jpeg, png, bmp format and file size must not exceed 5 MB
x

Ask a Question

x

x
x
x
Hire a Tutor

Answers and Solutions

What's Your Question?
Answer

“If-else” and “switch” both are selection statements. The selection statements, transfer the flow of the program to the particular block of statements based upon whether the condition is “true” or “false”. The fundamental difference between if-else and switch statements is that the if-else statement “selects the execution of the statements based upon the evaluation of the expression in if statements”. The switch statements “selects the execution of the statement often based on a keyboard command”.

if (expression){

statement(s)

}

else{

statement(s)

}

 

Answer
If - else structure is used to take a decision whether the condition is true or false, If the Condition is true if part will be executed otherwise else part will be executed Switch is used to test the value from the given set of N Values in Switch case by checking while the condition is true or false.
Answer

In If - else , If the Condition is true if part will be executed otherwise else part will be executed

example:

if (x>10)  printf("x>10");

else printf("x<=10");

Switch works when an action has to be performed depending on user's choice

switch(x)

{

case 1: printf("hello");

              break;

case 2: printf("hi");

             break;

default: printf("default");

}

Answer

If - else structure is used to take a decision whether the condition is true or false, If the Condition is true if part will be executed otherwise else part will be executed

Switch is used to test the value from the given set of N Values in Switch case by checking while the condition is true or false

Answer
If else structure evaluates for given set of conditions which can check integer, character and float(not for equality in case of floating datatype) for ranges and equality values. Switch statement works for character and integer datatype to check for exact values
Answer

The if-else structure is of type strict condition check,while switch is of type jump value catching.

Answer

This is the general syntax of if-else ladder: if (condition1){ //Body of if } else if (condition2) { //Body of if } else if (condition3) { //Body of if }

And this is the general syntax for switch: switch ( variable ) { case <variable value1>: //Do Something break; case <variable value2>://Do Something break;default: //Do Something break; }    The if-else ladder is of type strict condition check, while switch is of type jump value catching.   Limitations of switch over if-else ladder:

The variable expression are also not allowed in cases.

case i+2: is not allowed in switch, but is vaild on if-else. You cannot test a flat expression using switch. You cannot use similar expressions for multiple cases. For instance below switch statement in c is illegal:

switch (expression)

{

case 5: //...break;

case 2+3: //...

break;

}

switch statement must know the value inside it during compilation. But in c programming most of the time, it is require depending on the user input. In such cases, you cannot use switch.

Advantages of switch over if-else ladder:

A switch statement works much faster than equivalent if-else ladder. It is because compiler generates a jump table for a switch during compilation. Consequently, during execution, instead of checking which case is satisfied, it only decides which case has to be executed. It is more readable and in compare to if-else statements. It is more manageable for having higher level of indentation than if. For instance check below two source codes (solving same problem one using if where the other using switch) to check error messages.

Where to use switch over if-else ladder:

If there are large number of compares for a condition in your program, use switch over if-else ladder. For more complex comparisons.

Where to use if-else ladder over switch: In case of simple and few compares, if-else executes faster and easy write. Thus as per program’s requirement, a programmer should decide himself where to use which one condition control

Answer

Both the switch and if-else-if statements enable us to select one of several alternative statements for execution. However, they differ in several aspects:

1. The switch statement restricts the selection of alternative statements for execution based on the value of a single control expression. The if-else-if statement, on the other hand, is very flexible and permits selection of alternative statements based on values of arbitrary expressions.

2. In the switch statement, each desired value of the control expression must be mentioned explicitly using a case keyword. However, in addition to the individual values, the if-else-if statement allows a range of values to be specified in test expressions.

3. The control expression in a switch statement must be an integral expression, whereas the if-else-if statement allows us to test integral and floating-point expressions as well as character strings (using the library functions provided in the <string. h>).

4. The syntax of the swit.ch statement is much cleaner compared to the if-else-if statement. Thus, programs using switch statements are usually more readable.

5. The switch statement has the ability to execute more than one alternative statement by omitting break statements. This is not possible in if-else-if statement.

Answer

In if-else condition, if condition satisfied then control moves into if block , if condition not satisfied then only it goes to else block.

In switch case, case name would be predefined. for different case different block will be defined. according to input value(case name) of switch method control will directly move into corresponding case.

 

Answer

IF-else structure.

i am here explaining you the structure with example.

The following test decides whether a student has passed an exam with a pass mark of 45

if (result >= 45) printf("Pass\n"); else printf("Fail\n");

It is possible to use the if part without the else.

if (temperature < 0) print("Frozen\n");

Each version consists of a test, (this is the bracketed statement following the if). If the test is true then the next statement is obeyed. If is is false then the statement following the else is obeyed if present. After this, the rest of the program continues as normal.

If we wish to have more than one statement following the if or the else, they should be grouped together between curly brackets. Such a grouping is called a compound statement or a block.

if (result >= 45) { printf("Passed\n"); printf("Congratulations\n") } else { printf("Failed\n"); printf("Good luck in the resits\n"); }

Sometimes we wish to make a multi-way decision based on several conditions. The most general way of doing this is by using the else if variant on the if statement. This works by cascading several comparisons. As soon as one of these gives a true result, the following statement or block is executed, and no further comparisons are performed. In the following example we are awarding grades depending on the exam result.

if (result >= 75) printf("Passed: Grade A\n"); else if (result >= 60) printf("Passed: Grade B\n"); else if (result >= 45) printf("Passed: Grade C\n"); else printf("Failed\n");

In this example, all comparisons test a single variable called result. In other cases, each test may involve a different variable or some combination of tests. The same pattern can be used with more or fewer else if's, and the final lone else may be left out. It is up to the programmer to devise the correct structure for each programming problem.

Switch Case

This is another form of the multi way decision. It is well structured, but can only be used in certain cases where;

  • Only one variable is tested, all branches must depend on the value of that variable. The variable must be an integral type. (int, long, short or char).
  • Each possible value of the variable can control a single branch. A final, catch all, default branch may optionally be used to trap all unspecified cases.

Hopefully an example will clarify things. This is a function which converts an integer into a vague description. It is useful where we are only concerned in measuring a quantity when it is quite small.

estimate(number) int number; /* Estimate a number as none, one, two, several, many */ { switch(number) { case 0 : printf("None\n"); break; case 1 : printf("One\n"); break; case 2 : printf("Two\n"); break; case 3 : case 4 : case 5 : printf("Several\n"); break; default : printf("Many\n"); break; } }

Each interesting case is listed with a corresponding action. The break statement prevents any further statements from being executed by leaving the switch. Since case 3 and case 4 have no following break, they continue on allowing the same action for several values of number.

Both if and switch constructs allow the programmer to make a selection from a number of possible actions.

The other main type of control statement is the loop. Loops allow a statement, or block of statements, to be repeated. Computers are very good at repeating simple tasks many times, the loop is C's way of achieving this.

 

 

Post Answer and Earn Credit Points

Get 5 credit points for each correct answer. The best one gets 25 in all.

Post Answer