Write about the branching programs. Please give an example of flow chart of conditional statements.
Please answer the question in text.
Answer:- In simple terms, branching is a condtion where the program or algorithm have a choice to select any one of the option from many various options. Branching is basically of two types :-
In Conditonal Branching, basically a program test various condition on the basis of which it selects a correct option. Various programing method can be used in order to conditional branching. Most popular method is if-else and switch.
Syntax of if-else :
if(condition){
statement // the statement will execute if condition holds true
}
else{
statement // the statement will execute if condition holds false
}
Syntax of switch:
switch(expression) {
case constant-expression :
statement; //if the expression matches with this case then this statement will be executed
break; // it is optional
case constant-expression :
statement; //if the expression matches with this case then this statement will be executed
break; // it is optional
/* a switch can have any number of case statements */
default : // it is optional
statement; // if no case match then this statement will execute
}
Flow chart of If-else:
Flow Chart of Switch:-
In Unconditional Branching, program doesn't check for any kind of condition. It will be made to transfer the control to any other block or statement. Some of the examples of unconditional branching are return, continue and break.
Syntax of return:-
return [expression]; //it will return the control to the block of code who calls it
Syntax of continue:-
continue; //it force to execute the next iteration of the loop
Sytnax of break:-
break; //it will terminate the loop or the condition
Get Answers For Free
Most questions answered within 1 hours.