+ 2
If I write two IF statements, and if first statement gets true, will second statement be executed or not?
14 Réponses
+ 8
#include <iostream>
using namespace std;
int main() {
int age;
cin >> age;
if (age < 18) {
cout << "Sorry, you are below 18. You are not acceptable." << endl;
} else if (age > 60) {
cout << "Sorry, you are above 60. You are not acceptable." << endl;
} else {
cout << "Accepted!" << endl;
}
return 0;
}
+ 4
I wish I'll be good in programming too....
+ 2
int age;
cin >> age;
if (age>18 && age<60) {
court << "\n Accepted!" << endl;
}
else {
cout << "\n Sorry, you are below 18. You are not acceptable.";
}
if (age>60) {
cout << "\n You are above 60. Sorry, you are acceptable.";
}
In this program, I entered 24. The answer (according to program) comes Accepted!
But after that it also outputs "Sorry, you are below 18. You are not acceptable."
I checked the program, but no error is there. Please Help
+ 2
no, buddy because second time it is else if
+ 1
Depends if the second statement is true. The first if statement will execute, but if the second statement, is false, then it will not execute.
However, if the second statement is nested inside the first, then the second should execute.
+ 1
The second statement will execute if it is true
+ 1
no boy second one is not going to be executed because if the first one is true then the control goes to out from the block
+ 1
I think this (sorry for my English):
your code can be summarize:
if (age>18 && age<60) {case1}
else {case2}
if (age>60) {case3}
this not ok if you want divide in 3:
under 18,
between 18 and 60
after 60
because
under 18 and 18 - is ok, case2
between 18 and 60 - is ok, case1
after 60 - is NOT ok, case 2 & case3, because the condition of first if is not verified (then is execute the else)
and 60 is only case 2 because the condition in not verified in both the if
If you want you can write:
if (age>18 && age<60) {case1}
else
{ if (age<=18) {case2}
else {case3}
}
I write {} for easier reading (I hope)
note: in your code after the first if you write "court", you must remove "r".
0
@Zen, Thank you for solving my confusion. I didn't think the code can be written this easy..
I was rapidly reading, typing codes in my computer, and moving to next one...
And because of this, I got a Very Big Confusion.
0
@Jessa just practise 😉😊😃
0
If the first statement is true, the program will move on to the second statement-- execute it if it is true and not if it ain't.
0
@SammridhiJain, Got It!
0
no man. u used else if condition, which means first'if' condition fails it will check the elseif condition
0
if you write like this
if(condition1)
{ statements...}
if(condition2)
{statements}
then both the if will be work ,if conditions are true.
and if you will write like this
if(condition)
{statements..;
if(condition 1)
{statements..;}}
then if first condition will true then then the second if will also be executed when condition of that second if will true.