What
is logic?
Program logic is a set of
instructions to solve a particular problem. Program logic is important for every
program as every program have some logic to solve the problem.
Logic is a special set of instructions given to the computer. The computer executes the given instructions and solves the problem by using those instructions.
Logic is a special set of instructions given to the computer. The computer executes the given instructions and solves the problem by using those instructions.
How to
think logically?
Every programmer should think logically to solve the problem. But how to think logically?
Every programmer should think logically to solve the problem. But how to think logically?
So in order to think
logically we should learn how logic will work and how to develop logic for a
program.
How to develop logic?
To develop logic for a program we should be capable
of thinking logically. They must have good concept in mathematics. Only a
logical thinker can be capable of developing logic for a program. But most of
the programmers and beginners struggle a lot in order to improve their logical
thinking skills.
Points to be remember while developing logic:-
Yes You can – faith on yourself:
Yes You can – faith on yourself:
Computer has no brain. They cannot understand
anything unless you give some instruction. You will write logic for a computer to
solve a particular problem. So without your logic a computer cannot solve any
problem. So a computer needs your logic in order to solve a particular problem.
Remember all Programming concepts you have learnt:
When developing logic first think how to solve this
problem mathematically. Then build that logic by using the syntax in a
particular language (C, C++). So it is necessary for a programmer to learn all
the programming concepts and recall them while developing logic for a
particular problem.
Real Time application of Logic:-
Suppose that you have to print all the even numbers
between 1 to 10.
To solve that problem first think that how to
calculate even numbers? A number which is divisible by 2 that number is even
number. But how computer can understand that a number is divided by 2 or not.
Here is logic – the number is divisible by 2 means the remainder is 0. So your
logic must be number mod by 2 is equal to 0 then the number is even number,
otherwise not.
If you use
C language then for checking the condition you have to use if case.
Syntax is -- if
(condition)
{
Body
of the if block ....
}
else
{
Body of the else block ....
}
So if you use if then the code is like that
if (( number % 2) == 0)
{
printf(“%d is even number”, number);
}
Next covering range 1 to 10 you have to use a
iteration statement. In C language there are 3 types of iteration statement.
Choose any one from that. Suppose you choose for loop.
Syntax of for loop is— for (initialization;
condition check; step value)
{
Body
of for loop
}
So covering 1 to 10 the code is like that
for( i=1;i<=10;i++)
{
........
}
Finally we put the logic of even number into the
for loop.
So the final code is like that
for( i=1;i<=10;i++)
{
if (( i % 2) == 0)
{
printf(“%d is even number”, number);
}
}
Here i starts with 1 and ends in 10. During this
iteration every i value check through if condition if the value is divisible by
2 and the remainder is equal to 0 then the i value is even number.
Here the output is – 2, 4, 6, 8, 10.
Final conclusion:-
So to
solve the problem first think logically then build the logic using any
programming language. Test the code that it produces proper output or not.
No comments:
Post a Comment