Professional Documents
Culture Documents
L4 - Control Structures
L4 - Control Structures
USING COMPUTER
CONTROL STRUCTURES
CONTROL STRUCTURE
Control structures control the flow of execution in
program or function.
Types:
Selection structure
Repetition structure
FLOWCHART SYMBOLS USED WITH CONTROL
STRUCTURES
Sequence
no yes
Selection
no
Repetition
yes
3
SEQUENCE
Example: Draw flowchart to determine velocity and acceleration and print the result
4
SELECTION
Example: Flowchart to check whether denominator is <0.0001. If
so print denominator close to zero, otherwise find fraction value
and print it.
5
REPETITION
Example: Draw flow chart to find velocity and print over a period of
time t.
6
CONTROL STRUCTURE
SELECTION STRUCTURE:
- used in decision making
if Statement
Syntax:
if ( this condition is true ) if ( this condition is true )
execute statement1 ; {
statement1 ;
statement2 ;
}
Logical Operators
Logical AND -&&
Logical OR - ||
Logical NOT - !
Example:
Expressions 1 to 4 below contain different operands and operators.
Each expression’s value is given in the corresponding comment,
assuming x , y , and z are type double , flag is type int and the
variables have the values
1. !flag /* !0 is 1 (true) */
2. x + y / z <= 3.5 /* 5.0 <= 3.5 is 0 (false) */
3. !flag || (y + z >= x - z) /* 1 || 1 is 1 (true) */
4. !(flag || (y + z >= x - z)) /* !(0 || 1) is 0 (false) */
CONTROL STRUCTURE
/* Demonstration of if statement */
main( )
{
int num ;
printf ( "Enter a number less than 10 " ) ;
scanf ( "%d", &num ) ;
if ( num <= 10 )
printf ( "What an obedient servant you are !" ) ;
}
Decisions Using switch
The control statement that allows us to make a decision from the number of
choices is called a switch, or more correctly a switch-case-default, since these
three keywords go together to make up the control statement. They most often
appear as follows:
d) yes no
CONTROL STRUCTURE
Repition /looping structure
C contains three different loop structures—
while loop,
do/while loop,
for loop.
while Loop ( entry checking)
while (condition)
{
statements;
}
Statements within loop gets executed only when the
condition is true i-e non-zero value
When the condition evaluated to false i-e zero value, the
statements within loop gets doesn’t get executed .
PROBLEM
Generate a conversion table for converting degrees to
radians (note that the degree values start at 0°, increment
by 10°, and go through 360°) using while loop
Algotithm:
}
REPETITION/LOOPING STATEMENTS
do/while Loop
The do/while loop is similar to the while loop except that
the condition is tested at the end of the loop instead of
at the beginning of the loop.
The general form of the do/while loop is as follows:
do
{
statements;
} while (condition);
REPETITION/LOOPING STATEMENTS
do/while Loop
The do/while loop is similar to the while loop except that
the condition is tested at the end of the loop instead of
at the beginning of the loop.
The general form of the do/while loop is as follows:
do
{
statements;
} while (condition);
PROBLEM
Generate a conversion table for converting degrees to
radians (note that the degree values start at 0°, increment
by 10°, and go through 360°) using while loop
Algotithm:
Algorithm Development:
to make reversal we need to access individual digits of the
input number.
Since the number of digits in a number is not known, better to
start from accessing least significant digit.
REVERSING THE DIGITS OF AN INTEGER
Algorithm Development (contn)
The number 27953 is actually
3 is the remainder that results from dividing 27953 by 10. To get this
remainder we can use the mod function.
Applications
Hashing and information retrieval, data base applications.
REVERSING THE DIGITS OF AN INTEGER
PROBLEMS:
1. Design an algorithm that counts the number of digits in an integer.
2. Design an algorithm to sum the digits in an integer.
3. Design an algorithm that reads in a set of n single digits and converts them
into a single decimal integer. For example, the algorithm should convert the
set of 5 digits {2,7,4,9,3) to the integer 27493.
ARMSTRONG NUMBER