How to write if else code | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 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?

2nd May 2018, 7:33 PM
Paste4Life
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"}
2nd May 2018, 7:51 PM
Proff
Proff - avatar
+ 3
Paste4Life in which language?
2nd May 2018, 8:18 PM
Viryl15
Viryl15 - avatar
+ 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"; } }
2nd May 2018, 8:32 PM
ifl
ifl - avatar
+ 2
ifl I like using ternary operator. It's so cool
2nd May 2018, 9:54 PM
Viryl15
Viryl15 - avatar
+ 1
is it a trick question? and do you want 5he answer in a specific language?
2nd May 2018, 7:42 PM
ifl
ifl - avatar
+ 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
2nd May 2018, 8:20 PM
Paste4Life
+ 1
thanks a lot
2nd May 2018, 8:33 PM
Paste4Life
+ 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")); }
2nd May 2018, 9:13 PM
ifl
ifl - avatar
15th May 2018, 12:20 AM
­čç«­čç│Aayush Kumar­čç«­čç│
­čç«­čç│Aayush Kumar­čç«­čç│ - avatar