0% found this document useful (0 votes)
10 views15 pages

5.ICP - Controlling The Flow of Execution Through The Use of Branches

Uploaded by

Leo Nembaware
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views15 pages

5.ICP - Controlling The Flow of Execution Through The Use of Branches

Uploaded by

Leo Nembaware
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Controlling the flow of execution through the use of

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

Jump instructions, which include:


- Break statement
- Continue statement
- Return statement
- Goto statement

The flow of execution, which is the order in which programming


statements run, is managed using branches from conditional and
jump statements. Jump statements control the flow
unconditionally, while conditional statements determine which
blocks of code to execute based on boolean conditions.

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;

Flowchart for the break statement

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.

●​ The continue statement forces the loop to continue or


execute the next iteration. When the continue statement is
executed in the loop, the code inside the loop following the
continue statement will be skipped and the next iteration of
the loop will begin. ​

Syntax​

continue;

Flowchart for the continue statement

10
The goto statement

Referred to as the unconditional jump statement that can be used


to jump from one point to another within a function.

Syntax:
goto label;
label:

In the above syntax, the first line tells the compiler to go to or


jump to the statement marked as a label. Here label is a
user-defined identifier that indicates the target statement. The
statement immediately followed after ‘label:’ is the destination
statement. The ‘label:’ can also appear before the ‘goto label;’ like
below

Syntax 2
label;
goto label:

Flowchart for the goto statement

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].

O’Reilly Online Learning. 2021. MATLAB® and Its Applications in


Engineering: [Based on MATLAB 7.5 (R2007b)]. [online] Available
at:
<https://www.oreilly.com/library/view/matlab-and-its/97881317168
16/9788131716816_ch07lev1sec3.html>
[Accessed 30 November 2021].
www.javatpoint.com. 2021. C Switch Statement - javatpoint.
[online] Available at: <https://www.javatpoint.com/c-switch>
[Accessed 30 November 2021].
www.javatpoint.com. 2021. C if else statement - javatpoint.
[online] Available at: <https://www.javatpoint.com/c-if-else>
[Accessed 30 November 2021].

GeeksforGeeks. 2021. Decision Making in C / C++ (if , if..else,


Nested if, if-else-if ) - GeeksforGeeks. [online] Available at:
<https://www.geeksforgeeks.org/decision-making-c-c-else-nested-
else/>[Accessed 1 December 2021].
https://hsit.ac.in/E-LEARNING/FIRST%20YEAR/PROGRAMMING%20IN%
20C%20&%20DS%2017PCD13/PCD_Module2_Notes.pdf
https://en.wikipedia.org/wiki/Branch_(computer_science)
https://www.javatpoint.com/branching-statements-in-c
https://press.rebus.community/programmingfundamentals/chapter/br
anching-statements/

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

You might also like