control structures.pptx
TRANSCRIPT
-
8/14/2019 Control Structures.pptx
1/17
C L A N G U A G EB Y : R H O N I E L F . M E D R A N O
Control Structures
-
8/14/2019 Control Structures.pptx
2/17
Control Structures
Definition:
One of the instructions, statements or groups of
statements in a programming language whichdetermines the sequence of execution of otherinstructions or statements (the control flow).
-
8/14/2019 Control Structures.pptx
3/17
Types of Control Structures
Conditional structure
Iteration structures (loops)
Jump statements
The selective structure
-
8/14/2019 Control Structures.pptx
4/17
Conditional Structure
if and else A high-level programming language statement that
compares two or more sets of data and tests theresults. If the results are true, the THEN instructionsare taken; if not, the ELSE instructions are taken.
Format:if (condition) statement
Where condition is the expression that is being evaluated. If this conditionis true, statement is executed. If it is false, statement is ignored (not
executed) and the program continues right after this conditionalstructure.
-
8/14/2019 Control Structures.pptx
5/17
Iteration Structures (loops)
KINDS:The while loop
The do-while loop
The for loop
-
8/14/2019 Control Structures.pptx
6/17
Iteration Structures (loops)
while loopis a control flow statement that allows code to be executed
repeatedly based on a given boolean condition. The whileloop can be thought of as a repeating if statement.
Format: while (expression) statement
and its functionality is simply to repeat statement while the condition setin expression is true.
-
8/14/2019 Control Structures.pptx
7/17
Iteration Structures (loops)
do-while loop A high-level programming language structure that repeats
instructions based on the results of a comparison. In a DO WHILE loop, the instructions within the loop are
performed if the comparison is true.
Format:do statement while (condition);
Its functionality is exactly the same as the while loop, except thatcondition in the do-while loop is evaluated after the execution ofstatement instead of before, granting at least one execution of
statement even if condition is never fulfilled.
-
8/14/2019 Control Structures.pptx
8/17
Iteration Structures (loops)
for loop A loop construct found in many procedural
languages whichrepeatedly executes some instructions while a condition i
s true.
Format:for (initialization; condition; increase) statement;
and its main function is to repeat statement while condition remains true, like the while loop.
But in addition, the for loop provides specific locations to contain an initializationstatement and an increase statement.
So this loop is specially designed to perform a repetitive action with a counter which isinitialized and increased on each iteration.
-
8/14/2019 Control Structures.pptx
9/17
Jump Statements
The break statement
The continue statement
The goto statement
The exit function
-
8/14/2019 Control Structures.pptx
10/17
Jump Statements
break statementUsing break we can leave a loop even if the condition
for its end is not fulfilled. It can be used to end an
infinite loop, or to force it to end before its naturalend.
Example:10 9 8 7 6 5 4 aborted..
-
8/14/2019 Control Structures.pptx
11/17
Jump Statements
The continue statementThe continue statement causes the program to skip the
rest of the loop in the current iteration as if the end
of the statement block had been reached, causing itto jump to the start of the following iteration.
Example:10 9 8 7 6>
-
8/14/2019 Control Structures.pptx
12/17
Jump Statements
goto statement goto allows to make an absolute jump to another point in the program.
You should use this feature with caution since its execution causes anunconditional jump ignoring any type of nesting limitations.
The destination point is identified by a label, which is then used as anargument for the goto statement. A label is made of a valid identifier followed by a colon (:).
-
8/14/2019 Control Structures.pptx
13/17
Jump Statements
exit functionexit is a function defined in the cstdlib library.
The purpose of exit is to terminate the current program with aspecific exit code.
Its prototype is: void exit (int exitcode);
The exitcode is used by some operating systems and may beused by calling programs. By convention, an exit code of zeromeans that the program finished normally and any other
value means that some error or unexpected results happened.
-
8/14/2019 Control Structures.pptx
14/17
The Selective Structure
The syntax of the switch statement is a bit peculiar. Itsobjective is to check several possible constant values
for an expression.
Something similar to what we did at the beginning ofthis section with the concatenation of several if and
else if instructions.
-
8/14/2019 Control Structures.pptx
15/17
The Selective Structure
Format:switch (expression){case constant1:group of statements 1;
break;case constant2:group of statements 2;break;...default:default group ofstatements}
-
8/14/2019 Control Structures.pptx
16/17
The Selective Structure
switch evaluates expression and checks if it is equivalent toconstant1, if it is, it executes group of statements 1 until it finds the
break statement.
When it finds this break statement the program jumps to the end ofthe switch selective structure. If expression was not equal to
constant1 it will be checked against constant2 . If it is equal tothis, it will execute group of statements 2 until a break keyword is
found, and then will jump to the end of the switch selectivestructure.
Finally, if the value of expression did not match any of the previouslyspecified constants (you can include as many case labels as values you want to check), the program will execute the statements
included after the default : label, if it exists (since it is optional).
-
8/14/2019 Control Structures.pptx
17/17