+ 3
How to write if else code
Okay, the title sucks, but my question is simple. How would you write a code, where the user inputs a number, and then if the number is bigger than 130 but at the same time smaller than 150, it would say message1, if the number was greater than or equal to 150 it would say message2, else it would say message3?
9 Antworten
+ 6
in js:
var x = document.getElementById("input_id");
if(x.value > 130 && x.value < 150) { alert("msg1") }
else if(x.value >= 150) {alert("msg2")}
else {alert("msg3"}
+ 3
Paste4Life in which language?
+ 2
//that's the equivalent of @Proff, but for c++.
#include <iostream>
using namespace std;
int main() {
int value ;
cin >> value;
if (value > 130 && value < 150){
cout << "msg1";
}
else if (value >= 150){
cout << "msg2";
}
else{
cout << "msg3";
}
}
+ 2
ifl I like using ternary operator. It's so cool
+ 1
is it a trick question? and do you want 5he answer in a specific language?
+ 1
Oh, sorry, i thought it would automatically say, im new to this app xd using if and else statements in c++, thanks. Also its a question, not a quiz xd
+ 1
thanks a lot
+ 1
//Now just for fun, another way without using if/else could be with the ternary operator (x?y:z);
#include <iostream>
using namespace std;
int main() {
int value ;
cin >> value;
cout <<(value <= 130 ? "msg3" : (value < 150 ? "msg1" : "msg2"));
}