Hello, suppose we have 1 if statement and 2 else if statement. The condition inside if is true and obviously the other 2 conditions inside both else if blocks is false. How the control will flow in this case.
If/else if statement
hello @yashsharma4304 if else condition first see if the statement in if satisfies the result then the if statement is executed and it moves to the next iteration in the loop but if that if condition executes out to be false then the else statement is executed .
if you still have any other doubt you can ask here .
and if your query is resolved you can mark this doubt as resolved and you can give the star ratings .
Happy Learning !!
No I was not asking about if/else statement. I am asking about if/else-if statement.
@yashsharma4304 could you please share the code in which you have doubt ?
or do you want me to explain with the code of if else if ?
No I don’t need the explanation using code. I just simply need the flow of if/else if statement. As I said earlier that we have 1 if statement and 2 else if statement. Then here I am trying to ask that suppose the condition inside if statement is true, then how the control will flow from if to else if. Will it be like this:
[ Firstly, the if statement will get executed because the condition is true, now it will pass the control to 1st else if , here the condition is false then again the control is passed to the last else if statement , here also the condtion is false so now the program will stop as there are no more blocks. ]
Is the above control flow is right or not ?
@yashsharma4304 no .
if the if statement is executed to be false then only the else statement will execute .
but if the if condition is true then the else block will never execute.
if the if statement is executed to be false then only the else statement will execute .
but if the if condition is true then the else block will never execute.
Please see I am not talking about else block. I am talking about else if block. For reference you can use this example code:https://ide.codingblocks.com/s/356448
Now how the control will flow in this program.
@yashsharma4304
please see this https://ide.codingblocks.com/s/356464
i have commented some lines in this .
if you have understood this pleas tell me here ,otherwise if there any other doubt persists you can ask here .
Happy Learning !!
Ok so it means that if the if condition is true then the flow will never move to else if blocks.
yes exactly .
but you know if there is another if statement after one true if statement and then that second if statement is also true then it will execute .
Yes that I know. Thank you for giving me some of your time in resolving my doubt.
@yashsharma4304 if your doubt is resolved please mark this doubt as resolved and also give the good star rating .
Happy to help you .
Happy Learning !!