Chapter 1 Ds
Chapter 1 Ds
Programs written by programmer will be in human readable form that cannot be understood
by computer .In order to convert the program from human readable form to machine readable
form ,we need compiler software.From the program in its human-readable form of source
code, a compiler can derive machine code-a form consisting of instructions that the computer
can directly execute.
The first programmable computers required the programmers to write explicit instructions to
directly manipulate the hardware of the computer. This "machine language" was very tedious
to write by hand since even simple tasks such as printing some output on the screen require
10 or 20 machine language commands. Machine language is often referred to as a "low level
language" since the code directly manipulates the hardware of the computer.
By contrast, higher level languages such as "C", C++, Pascal, Cobol, Fortran, ADA and Java
are called "compiled languages". In a compiled language, the programmer writes more
general instructions and a compiler (a special piece of software) automatically translates
these high level instructions into low level machine language. The machine language is then
executed by the computer.
2.Control Structures
When a program is running, the code is being read by the computer line by line from top to
bottom, and from left to right. This is known as the "code flow”. This code flow will be
affected by control structure. A control structure is a block of programming that analyses
variables and chooses a direction in which to go based on given conditions. The computer
program has a strict set of rules to decide which direction to go. So, this decision that must be
made, that will in turn affect the flow of code, is known as a control structure.
1. Control will be moved from one place to other part of the program.
2. Re-running a certain piece of code again.
3. Skipping a bunch of code.
1 Sequence
A sequence of instructions in a
sequence structure must all be
carried out.
Example
Read a,b,c
C=a+b
Print c
2 Selection
In a selection structure certain instructions will only be carried out IF a certain
condition is found to be true.
(i) (IF-THEN-ELSE)
In this structure, one condition will be checked if it is TRUE then procedure 2 will
be followed or else procedure 3 will be followed.
Example
(ii) (IF-THEN-ELSEIF-THEN-ELSE)
In this structure,first one condition will be checked if it is TRUE then procedure 2 will be
followed or else another one condition will be checked if it true then procedure 3 will be
followed or else procedure 4 will be followed.
Example
3.Looping Structures
Loop structures allow you to run one or more lines of code repetitively. You can repeat the
statements in a loop structure until a condition is True, until a condition is False, a specified
number of times, or once for each element in a collection. Running a set of statements until a
condition becomes true
An infinite loop is one that lacks a functioning exit routine . The result is that the loop repeats
continually until the operating system senses it and terminates the program with an error or
until some other event occurs.
It provides the following types of loops to handle the looping requirements:
1. While Loop
2. For Loop
3. Do ... While Loop
1. While Loop
• While loop allows to repeatedly run the same block of code, until a given condition
becomes true.
• It is called an entry-controlled loop statement and used for repetitive execution of the
statements.
• The loop iterates while the condition is true. If the condition becomes false, the program
control passes to the next line of the code.
Example
2. For Loop
• For loop Is a compact form of looping.
• It is used to execute some statements repetitively for a fixed number of times.
• It is also called as entry-controlled loop.
• It is similar to while loop with only difference that it continues to process block of code
until a given condition becomes false and it is defined in a single line.
Syntax:
for (initialization; test-condition; increment/decrement)
Example
3. Do ... While Loop
• Do... While loop is executed at least once, even if the condition is false.
• It is called as an exit-controlled loop statement
• This loop does not test the condition before going into the loop. The condition will be
checked after the execution of the body that means at the time of exit.
• It is guaranteed to execute the program at least one time.
4. Flowchart
Flowcharts arc graphical methods of outlining program structure and generally utilize
a set of standard symbols known as the ANSI (American National Standards Institute)
symbols.
It is mainly used to understand the program structure clearly and completely
5. Syntax
In computer science, the syntax of a programming language is the set of rules that
define the combinations of symbols that are considered to be correctly structured
programs in that language. Documents that are syntactically invalid are said to have a
syntax error. The concept of syntax in programming language is similar to the
concepts of grammar and spelling in spoken language. When a sentence in English
has very poor grammar and spelling, it becomes difficult or even impossible to
understand. Similarly, when code has syntax errors, the program will not execute.
The difference is that when you read a sentence in English with a minor error, you can
typically still understand its meaning. Very small syntax errors in code, however,
make the program unusable. Coders, therefore, have to pay great attention to detail to
make sure their code is not only logical but also free of syntax errors. There are a
number of aspects to syntax, including statements, variables and keywords.