+ 2

If I write two IF statements, and if first statement gets true, will second statement be executed or not?

27th Sep 2016, 3:35 PM
Tanish Agarwal
Tanish Agarwal - avatar
14 Antworten
+ 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; }
27th Sep 2016, 4:09 PM
Zen
Zen - avatar
+ 4
I wish I'll be good in programming too....
28th Sep 2016, 1:53 PM
Jessa
+ 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
27th Sep 2016, 3:51 PM
Tanish Agarwal
Tanish Agarwal - avatar
+ 2
no, buddy because second time it is else if
12th Oct 2016, 2:48 AM
Yash Namdeo
Yash Namdeo - avatar
+ 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.
27th Sep 2016, 3:39 PM
Zachary
+ 1
The second statement will execute if it is true
27th Sep 2016, 3:43 PM
Carlos Alvarez
+ 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
7th Nov 2016, 2:21 PM
Akash Malawat
Akash Malawat - avatar
+ 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".
8th Nov 2016, 11:51 PM
Valeria Ferretti
Valeria Ferretti - avatar
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.
27th Sep 2016, 4:17 PM
Tanish Agarwal
Tanish Agarwal - avatar
0
@Jessa just practise 😉😊😃
28th Sep 2016, 1:58 PM
Tanish Agarwal
Tanish Agarwal - avatar
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.
2nd Oct 2016, 6:07 PM
Samriddhi Jain
Samriddhi Jain - avatar
0
@SammridhiJain, Got It!
2nd Oct 2016, 6:13 PM
Tanish Agarwal
Tanish Agarwal - avatar
0
no man. u used else if condition, which means first'if' condition fails it will check the elseif condition
9th Nov 2016, 4:35 PM
Gopinatha
Gopinatha - avatar
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.
17th Nov 2016, 5:28 PM
D E E P A N S H U YADAV
D E E P A N S H U YADAV - avatar