Jumping, Branching & Looping
So far, the program written are executed sequentially i.e. one after another. However, statements can be executed non-sequentially. This can be done in three different ways.
1. Jumping
2. Branching
3. Looping
Jumping:
In this technique we might skip some statement in between. This can we done in two ways
1. Unconditional Jumping
2. Conditional Jumping
In the first method we execute some statement directly, whereas, in the second method statement are executed only for particular condition being true. This is also called branching, which is dealt in details later.
GOTO:
Using this GOTO statement we can transfer the program control to anywhere inside the program. Control is transfer from one point to another.
Syntax :- GOTO label
Here once the compiler encounter the GOTO statement, the control is transfer by the label. For example
x = 10
Print X+5
GOTO 10
Print x-5
10 End
In the above example the value of x is increse by 5 and printed and the program ends. The statement which decreases the value of x is skipped. If the statement where jumping is done is below the GOTO statement, it is called forward jump otherwise a backward jumping.
The main disadvantage of GOTO in that it can leasd to infinite looping. In Such case the program runs continuously and never breaks. The user has to stop it manually.
Branching:
In this method control is transfer to any statement only when a particular condition is true. We can branch to statement by using any of the following:
1. If
2. If-else
3. if-elseif
4. nested if
5. Switch case
If statement:
If statement is use to to check the condition, once it is true it execute the set of statements between curly braces '{statements...}'. If there is only one statement we can skep curly braces. the syntax is as follow:
If(condition)
{
Statement
}
OR
If(condition)
Statement
EXAMPLE
Write a program to check if number is positive or negative
Input A
If(a >0)
{
Print "A is positive"
}
if(a<0)
{
Print "A is negative"
}
Once the user enters a number it is checked using IF condition. If the condition evaluate to true, "A is positive" is printed otherwise next condition is evaluated which on being prove, "A is negative" is printed.
if else statement:
This branching statement is also same as if statement, but the only difference is that it has two parts one if true and one for false. the syntax is:
If(condition)
{
Statements
}
else
{
Statements
}
EXAMPLE
Let's consider same example
Input A
If(a >0)
{
Print "A is positive"
}
else
{
Print "A is negative"
}
Once the user enters a number it is checked using IF condition. If the condition evaluate to true, "A is positive" is printed otherwise "A is negative" is printed without checking any other condition.
if elseif else statement:
This is similar to if else, but the only difference is that the else part is not directly executed rather we give a condition in the else part which is evaluate and further execution take place. The syntax is:
If(condition)
{
Statements
}
else if(condition)
{
Statements
} else
{
Statements
}
EXAMPLE
Write a program to accept three numbers find the greatest of the tree.
Input A
Input B
Input C
If(A>B && A>C)
{
Print "A is greatest"
}
else if(B>C && B >A)
{
Print "B is greatest"
} else
{
Print "C is greatest"
}
Input: 12,23,34
Output: 34
Since the value of C is greater than A and B else part is executed.
Nested If statement:
If defined inside if is called nested if. Whenever we want to avoid logical operators we can use this. The syntax is :
If(condition)
{
If(condition)
{
Statement
}
}
EXAMPLE
Write a program to accept only positive number, If it's so find whether it is odd or not.
Input A
If(A>=0)
{
if(A%2 ==0)
{
Print "number is even"
} else
{
Print "number is odd"
} }
else
{
Print "number must be greater than 0"
}
If the number is positive control is transfer to the inner If and the inner if condition is true so "number is even" is printed else "number is odd" is printed. If the number is negative than "number must be greater than 0" is printed.
Switch case statement:
switch case statement work like if statement. The difference is that the switch-case statement can make the code simpler to read and work with. IT evaluates the test expression, and jumps to the appropriate control block. It supports strings, and numbers, as well as multiple options with one case block. The syntax are as follow:
switch(expression)
{
case 1:
statement
case 2:
statement
case 3:
statement
default:
statement
}
EXAMPLE
let's consider same example of odd event , if we want to replace the inner if with switch the the code will be.
Input A
If(A>=0)
{
switch(A%2)
{
case 0:
print "number is event"
case 1:
print "number is odd"
} }
else
{
Print "number must be greater than 0"
}
looping:
Statements can be used multiples times, which is referred to as LOOPING.
The set of statements are executed fixed number of times till the conditions is true. Once the conditions become false the control gets transferred to the statement after the loop. looping can be done in following ways:
1. while loop
2. do-while
3. for loop
In any of the methods looping consists of three parts. First part is initialization, Second part is the condition till which the loop should be executed and the third is increment and decrement part. If any of the part is missing it lead to an infinite loop.
While loop:
This loop is executed till the condition is true, Once the condition becomes false the loop will be terminated. While loop is used when we don't know how many time the loop will be executed. the syntax is:
while(condition)
{
Statement
}
EXAMPLE
Problem :Write a program to print Fibonacci series from 1 to 20.
Fibonacci series is : 0, 1 ,1 ,2 ,3 ,5 ,8 ,13 ...
A=0
B=1
Print A
Print B
C= A+B
while(C<=20)
{
Print C
A = B
B=C
C=A+B
}
do-While loop:
This is same as while loop, the only difference is it execute at least one time. In do-while loop the statement are executed after that the condition is checked, if the condition true than the loop will be continue else it will be terminated. The syntax is:
do
{
Statement
}While(condition)
EXAMPLE
Problem :Write a program to take input a number from user and print it until the user enter 0.
A = 0
do
{
Input A
Print A
} while(A !=0)
In the above code the execution take place at least one time even if the the value of A is initialize as 0.
For loop:
This is the simplest looping technique. it work same as while loop but varies only in the syntax. Initialization, incrementation and condition are given in the same line. the main difference is it id fixed loop. The syntax is:
for(initialization;condition;increment/decrement)
{
Statement
}
EXAMPLE
Problem :Write a program to print table of the number enter by user.
Input A
for(i=1;i<=10;i++)
{
Print A*i
}
These is all about Programming you need to know before starting any programing language. We will discuss all the points again in details with more real-time examples when we start learning programming languages. I hope you Enjoy this series. Thank you