+ 3
What's wrong with this code?!!
#include <iostream> using namespace std; int main() { int age=18; if (age > 18){ if (age==18) { if (age == 20) { cout<<"you are adult"; } } } if (age > 0) { if (age < 18 ) { cout<<"you are a child" } } else { cout<<"something went wrong"; } return 0; }
19 Antworten
+ 6
@Lukman wat result do u want
+ 6
If u want "you are an adult" to print, that wont work because first: age is not > 18 and age is not = 20
+ 6
I repeat :
If u want "you are an adult" to print, that wont work because first: age is not > 18 and age is not = 20
+ 6
Lukman u can combine as many as u want in most languages
+ 4
Try this
#include <iostream>
using namespace std;
int main()
{
int age=18;
if (age > 18){
{
if (age==18) {
if (age == 20) {
cout<<"you are adult";
}
}
}
if (age > 0) {
if (age < 18 ) {
cout<<"you are a child"
}
}
}
else {
cout<<"something went wrong";
}
return 0;
}
EDIT:
#include <iostream>
using namespace std;
int main()
{
int age=18;
if (age >= 18){
if (age==18) {
//NOT NECESSARY if (age == 20) {
cout<<"you are adult";
}
}
// }
if (age > 0) {
if (age < 18 ) {
cout<<"you are a child";
}
}
else {
cout<<"something went wrong";
}
return 0;
}
+ 3
@Yerucham just the same!!!
+ 3
Yep
that's what I'm talking about
+ 3
I was just trying the nested if
+ 3
So, in C# can you inculde as many && as you want in the if statement
+ 2
@Tobias Hallmans but can't you see it. i've wrote:
if (age==18) {
cout <<"you are adult":
}
+ 2
@Ace so can I write
else (Statement) {
cout <<"......";
}
+ 2
So helpful! thank you so much 😊☺☺
+ 1
You ask whether age is strictly greater then 18. If so, you ask if it's equal to 18. That can never happen here.
+ 1
You're missing a semicolon here:
cout<<"you are a child"; <-- here
The output as the code is currently written will be nothing not "something went wrong".
0
that part of the code is never reached as it will be skipped by the surrounding if statement.
18 > 18 is false
0
why do you not use logical "and(&&) " and "or(||)". for example :
if (age>0 && age <18) . or in c++ it does not work( i learn c#)
- 1
Don’t know