5.ICP - Controlling The Flow of Execution Through The Use of Branches
5.ICP - Controlling The Flow of Execution Through The Use of Branches
branches
-A branch in a computer program is an instruction that allows the
program to start executing a different sequence of instructions,
moving away from the usual order of execution.
-Branch control statements enable the selection and execution of
specific blocks of code based on certain conditions.
-Branches are part of branching or control statements. Common
types include:
Conditional statements (decision-making statements), which
consist of:
- If statement
- If-else statement
- Switch case statements
1
Conditional statements
If Statement
● The if statement is used to check some given condition and
perform some operations depending upon the correctness of
that condition.
● It is mostly used in the scenario where we need to perform
the different operations for the different conditions. The
syntax of the if statement is given below.
Syntax
if(expression){
//code to be executed
}
Example
1.#include<stdio.h>
2.int main(){
3.int number=0;
4.printf("Enter a number:");
5.scanf("%d",&number);
6.if(number%2==0){
7.printf("%d is even number",number);
8.}
9.return 0;
10. }
2
Flowchart for if statements
3
If-else Statement
● The if-else statement is used to perform two operations for a
single condition.
● It is an extension to the if statement using which, we can
perform two different operations, i.e., one is for the
correctness of that condition, and the other is for the
incorrectness of the condition..
● Using if-else statement is more preferable since it always
invokes an otherwise case with every if condition.
Syntax
if(expression){
//code to be executed if condition is true
}
else{
//code to be executed if condition is false
}
4
Example of if-else statement
1.#include<stdio.h>
2.int main(){
3.int number=0;
4.printf("enter a number:");
5.scanf("%d",&number);
6.if(number%2==0){
7.printf("%d is even number",number);
8.}
9.else{
10. printf("%d is odd number",number);
11. }
12. return 0;
13. }
5
Flowchart for if else statement
6
Switch statement
● The switch statement in C is an alternate to if-else-if ladder
statement which allows us to execute multiple operations for
the different possible values of a single variable called switch
variable.
● We can define various statements in the multiple cases for
the different values of a single variable.
Syntax
switch(expression){
case value1:
//code to be executed;
break; //optional
case value2:
//code to be executed;
break; //optional
......
default:
code to be executed if all cases are not matched;
}
7
Switch case statement example
1.#include<stdio.h>
2.int main(){
3.int number=0;
4.printf("enter a number:");
5.scanf("%d",&number);
6.switch(number){
7.case 10:
8.printf("number is equals to 10");
9.break;
10. case 50:
11. printf("number is equal to 50");
12. break;
13. case 100:
14. printf("number is equal to 100");
15. break;
16. default:
17. printf("number is not equal to 10, 50 or 100");
18. }
19. return 0;
20. }
8
Jump statements/Instructions
● These statements are used in unconditional control of the
flow of execution throughout the functions in a program.
● Jump instructions include the break statement, goto
statement, continue statement and return statement.
Break statement
● This loop control statement is used to terminate the loop. As
soon as the break statement is encountered from within a
loop, the loop iterations stop there, and control returns from
the loop immediately to the first statement after the loop.
● The break statements are used in situations when we are
not sure about the actual number of iterations for the loop or
we want to terminate the loop based on some condition.
Syntax
break;
9
Continue statement
● The continue statement is opposite to that of the
break statement, instead of terminating the loop, it forces to
execute the next iteration of the loop.
Syntax
continue;
10
The goto statement
Syntax 2
label;
goto label:
11
The return statement
This returns the flow of the execution to the function from where it
is called. This statement does not mandatorily need any
conditional statements. As soon as the statement is executed, the
flow of the program stops immediately and return the control from
where it was called. The return statement may or may not return
anything for a void function, but for a non-void function, a return
value is must be returned.
Syntax:
return[expression];
12
Key terms
break
A branching statement that terminates the existing structure.
continue
A branching statement that causes a loop to stop its current
iteration and begin the next one.
exit
A predefined function used to prematurely stop a program and
return to the operating system.
goto
An unstructured branching statement that causes the logic to
jump to a different place in the program.
return
A branching statement that causes a function to jump back to the
function that called it.
13
References
Busbee, K., 2021. Branching Statements. [online]
Press.rebus.community. Available at:
<https://press.rebus.community/programmingfundamentals/chapt
er/branching-statements/>
[Accessed 30 November 2021].
14
https://www.javatpoint.com/branching-statements-in-java
https://www.javatpoint.com/branch-instruction-in-computer-organizati
on
https://www.enjoyalgorithms.com/blog/conditions-and-branching-in-p
ython/
https://www.prepbytes.com/blog/c-programming/branching-statement
s-in-c/
https://users.cs.utah.edu/~germain/PPS/Topics/branching.html
15