Switch Case Not Showing Correct Results

Switch Case not showing correct results

When you write

switch (x) {
case(y):
...
}

it's equivalent to testing

if (x == y) {
...
}

So

case (marks < 20):

means:

if (marks == (marks < 20)) {

You can't use case for range tests like this, you need to use a series of if/else if:

if (marks < 20) {
console.log('Yes Freaking Failed');
} else if (marks < 80) {
console.log('Ahh Its OK');
} else {
console.log('Whooping');
}

Also notice that if it worked the way you thought, it could never execute marks > 80, because that would also match marks > 20, and the first matching case is always executed.

There's no need for the Cant say u maybe flunked case, because there are no other possibilities.

Switch Statement returns incorrect results

The behavior you want would be achieved with an if else, or the ternary operator (which is pretty much just a short hand way of writing an if/else). Here's a rough untested example.

function airport($one, $two, $three) {
if ( !empty($one) || !empty($two) || !empty($three) ) {
$one = !empty($one) ? "Airline Name: $one<br>" :"Airline Name: PLEASE PROVIDE AIRLINE NAME<br>";
$two = !empty($two) ? "Flight Number: $two<br>" : "Flight Number: PLEASE PROVIDE FLIGHT NUMBER<br>";
$three = !empty($three) ? "Departure Airport: $three<br>" : "Departure Airport: PLEASE PROVIDE DEPARTURE AIRPORT<br>";
}
echo $one, $two, $three;
}
airport($airline_name,$flight_number,$departure_airport);

As to why your switch doesn't perform as you expect, per the manual:

Only when a case statement is found with a value that matches the value of the switch expression does PHP begin to execute the statements. PHP continues to execute the statements until the end of the switch block, or the first time it sees a break statement. If you don't write a break statement at the end of a case's statement list, PHP will go on executing the statements of the following case.

-http://php.net/manual/en/control-structures.switch.php

JavaScript Switch statement is not returning the correct value

Instead of comparing Screen to boolean value, which caused your unexpected result, you should do the trick of comparing true to true by using switch(true) instead

const dist = 50;
const Screen = { xs: true };

const f = () => {
console.log(Screen.xs && dist >= 40);
switch (true) {
case Screen.lg && dist >= 50:
case Screen.md && dist >= 25:
case Screen.sm && dist >= 10 && dist <= 20:
case Screen.xs && dist >= 10 && dist <= 30:
return 8;
case Screen.sm && dist >= 25:
case Screen.xs && dist >= 40:
return 7;
default:
return 9;
}
};

console.log(f());

Switch statement not displaying any results

You can't replace a for loop with a switch statement. They are for totally different purposes. The for loop will run the line

data.add("SAMPLE #");

six times, but your switch statement will be executed only once and it will look for the item that matches 6, which in your case there isn't one, so that's why nothing is displayed. If you changed the value of SAMPLE_DATA_ITEM_COUNT to a number between 1 and 5 you'd see one item. To help you understand, your switch statement is exactly the same as the following if statement:

if (SAMPLE_DATA_ITEM_COUNT == 0) {
data.add("New Delhi");
} else if (SAMPLE_DATA_ITEM_COUNT == 1) {
data.add("Mumbai");
} else if (SAMPLE_DATA_ITEM_COUNT == 2) {
data.add("Kanpur");
} else if (SAMPLE_DATA_ITEM_COUNT == 3) {
data.add("Hyderabad");
} else if (SAMPLE_DATA_ITEM_COUNT == 4) {
data.add("Bangalore");
} else if (SAMPLE_DATA_ITEM_COUNT == 5) {
data.add("Noida");
}

but as in your case SAMPLE_DATA_ITEM_COUNT is 6 that's why you don't see anything.

To see all your data, you need to put a for loop around your switch statement - e.g.:

for (int i = 0; i < SAMPLE_DATA_ITEM_COUNT; ++i) {
switch (i) {

case 0:
data.add("New Delhi");
break;

case 1:
data.add("Mumbai");
break;

case 2:
data.add("Kanpur");
break;

case 3:
data.add("Hyderabad");
break;

case 4:
data.add("Bangalore");
break;

case 5:
data.add("Noida");
break;
}
}

You could simplify things and get rid of the switch statement and the for loop altogether and just do:

data.add("New Delhi");
data.add("Mumbai");
data.add("Kanpur");
data.add("Hyderabad");
data.add("Bangalore");
data.add("Noida");

why I get Different result in switch statements

When you do

switch(bill){

a case will be fulfilled if the expression that follows it is === to the value of bill. For example, if bill is 124, then switch(bill) would require

case: 124:
tip = bill * .15 // because 124 <= 200

for your program to work as expected. Your code is producing unexpected results because all the cases fail, and it falls through to the default.

The switch(true) works because when you have cases of

case bill > 0 && bill < 50:

this will effectively evaluate to

case true
// block below will run, because `true` is `===` to the expression that was switched against

or

case false:
// block below will not run, because `false` is `!==` to the expression that was switched against

and run the case block accordingly.

switch is not the right tool for the job, IMO - it's confusing and verbose. (It's never the right tool for any programming task in Javascript, I think.) I'd use if/else instead:

const getTip = (bill) => {  if (bill > 0 && bill < 50) {    return bill * 0.2;  } else if (bill >= 50 && bill <= 200) {    return bill * 0.15;  } else {    return bill * 0.1;  }};
function simpleTipCalculator(bill) { console.log(getTip(bill));}
simpleTipCalculator(124)simpleTipCalculator(48)simpleTipCalculator(268)

switch case not working properly in C

It looks like you are getting the numbers into Choice, and the chars into c.
but you are only using the Choice var in the switch, you are never checking the C var.

So essentially, if you hit a letter it is storing it in the C var, and then using the old value in Choice again.

Why won't my switch statement generate the correct results?

When you load index.php?post=myFile, you are generating the following element in the $_GET superglobal array:

$_GET = array ("post" => "myFile");

Then $post = empty($_GET['post']) ? '' : $_GET['post']; declares $post = "myFile".

This means that if (empty($post)) { evaluates to false (because it is not empty, it is myFile) and the switch-case block is ignored.

The else condition is executed. $titulo = 'Post';


Now, if you want the switch-case block to be executed, you must:

  • Change if (empty($post)) { to if (!empty($post)) {.

As for future trouble you may have within the switch-case block, make sure that you are identically matching the value for $pagina when writing each case. If you aren't sure about what values you are working with or what is being executed, just add echoes in each case to clarify.

I say this because posts/myFile is not the same string as myFile.


After more discussion about other hyperlinks, I'll recommend changing the default value for $pagina and extending the if logic to be more inclusive.

$pagina = empty($_GET['p']) ? '' : $_GET['p'];         // I changed 'home' to ''

if ($post != '' || ($post == '' && $pagina != '')) { // I changed the logic

Switch statement not returning values

Your test is not valid based on the values you are handling in your switch statement. You only handle cases for A and G but you passed ACG. Switch had no way to go if any of the cases specified do not match since you also are missing the default case. Your test would be valid if:

function pairElement(str) {switch (str) {    case "A":    console.log("some things")    break;    case "G":    console.log("some more things")    break;}}pairElement("A"); // some things - validpairElement("G"); // some more things - validpairElement("ACG"); // switch case and no default - NOT valid

Why the switch statement doesn't give an output?

You have some mistakes in your code

  1. you have conditions in your case, which will return boolean. so you have to pass boolean in switch
  2. you missed break statement
  3. you missed to print or do anything in default statement

var moyen = 9;
switch(true) { case (10 < moyen && moyen < 11.99): console.log("Acceptable"); break; case (12 < moyen && moyen < 13): console.log("Souhaitable"); break; case (14 < moyen && moyen < 15): console.log("Bien"); break; case (moyen >= 16): console.log("Très bien"); break; default: console.log("C'est pas un valide moyen !");};


Related Topics



Leave a reply



Submit