0% found this document useful (0 votes)
14 views2 pages

Control Structures

This document discusses different types of control structures in programming, including selection statements and repetition statements. Selection statements like if, if-else, and switch statements allow a program to choose between alternatives based on certain conditions being true or false. Repetition statements like while, do-while, and for loops allow a block of code to be executed repeatedly as long as a condition remains true. The document explains the syntax and usage of these different control structures.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views2 pages

Control Structures

This document discusses different types of control structures in programming, including selection statements and repetition statements. Selection statements like if, if-else, and switch statements allow a program to choose between alternatives based on certain conditions being true or false. Repetition statements like while, do-while, and for loops allow a block of code to be executed repeatedly as long as a condition remains true. The document explains the syntax and usage of these different control structures.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

LESSON 4: Control Structures

Study online at https://quizlet.com/_dbvl9r


Selection Statements and Repetition Statements 2 types of Control Structures
SIMPLE STATEMENTS a simple instruction that ends with a semicolon.
several instructions that are grouped into block enclosed in braces
COMPOUND STATEMENTS
({}).
- check certain conditions before executing a set of statements.
Selection Statements - provide the branching mechanism which allows a program to
choose from alternatives
selection statements can be: if statement, if-else statement, con-
true
ditional operator (?:), switch statement
- used for single selection.
if statement - the statement will be executed if and only if the expression
evaluates to true.
The __ keyword is used when there are two possible courses of
elose
action after an if statement.
conditional operator (?:) is a ternary operator, which means that it takes three arguments
it separates the execution of statements per labeled case so that
break
there is no "fall-through" into the other statements
the optional __ section is executed when expression does not
default
evaluate to any of the listed constants in the case section
In looping, a sequence of statements is executed until some
Repetition Statements
condition is satisfied which is placed for termination of the loop.
body of the loop and control statement A program loop consists of two segments
always tested for the execution of the body of the loop and can be
control statement
classified according to its position in the loop.
entry-controlled loop (pretest loop) and exit-controlled loop
Classification of CONTROL
(pretest loop)
Classification of CONTROL where the conditions are tested first
entry-controlled loop (pretest loop)
and if satisfied then only the body of loop is executed.
Classification of CONTROL where the test is made at the end of
exit-controlled loop (pretest loop)
the body, so the body is executed unconditionally first time.
while loop, do-while loop, for loop Looping can be of the following form:
- an entry-controlled loop statement.
while loop - The loop control variable must be:(1) initialized, (2) tested, and
(3) updated for the loop to execute correctly.
variable is usually set to a starting value before the while state-
initialize
ment is reached
test variable is tested before the start of each loop repetition
variable is updated within the body of the while loop during each
update
execution or pass
do-while loop - an exit-controlled loop statement.
- an entry-controlled loop that provides a more concise loop
control structure
for loop
- allows the user to efficiently write a loop that needs to be
executed for a specific number of times
More than one variable can be initialize data time in the for
true
statement.
It is also permissible to use expressions in the assignment state-
true
ments of initialization and increment sections.
Like the initialization section, the increment section may also have
true
more than one part.
true

1/2
LESSON 4: Control Structures
Study online at https://quizlet.com/_dbvl9r
One or more sections in the for statement can be omitted, if
necessary.
true Empty for loop is possible
true Declare loop control variable inside the for
This is a good, solid looping process with applications to numer-
while loop
ous situations
This is a good choice when you are asking a question, whose
do-while loop
answer will determine if the loop is repeated
This is a good choice when the number of repetitions is known or
for loop
can be supplied by the user
Counter-Controlled Loop, Sentinel-Controlled Loop Classification of LOOPING PROCESS
Classification of LOOPING PROCESS used to read in and
Counter-Controlled Loop process a sequence of data items when you know in advance the
number of items to be read
Classification of LOOPING PROCESS where the user enters a
"unique" data value, called a sentinel value, after the last data
Sentinel-Controlled Loop,
item; the loop repetition condition tests each data item and causes
the loop to exit when the sentinel value is read.
return 0;, exit();, break;, continue;, How to end a program or loop earlier than its normal termination?
It is possible to end a program (or function) prior to its normal
return 0;
termination by using an "extra" return0; statement.
- It requires the Standard Library header file, stdlib.
-The format is: exit (value);
exit(); -Using exit (1); returns a value of 1 to the IDE indicating that an
error must have occurred. This process is often used for error
trapping.
- Gets you out of a loop.
-The program continues with the next statement immediately fol-
break;
lowing the loop.
- It does not break out of a "nested loop" (a loop within a loop)
- performs a "jump" to the next test condition in a loop
-The continue statement may be used ONLY in iteration state-
continue;
ments (loops). It serves to bypass, or jumpover, certain iteration
sections of a loop.
Zero Iteration Loop, Flag-controlled Loop, Nested Loop Other types of LOOPING
There may be instances where the while loop will not execute
Zero Iteration Loop
because the test failed (False condition).
- are used to control the execution of loop.
- The value of the flag is initialized (if not assigned a value, it will be
Flag-controlled Loop
false) prior to loop entry and is redefined when a particular event
occurs inside the loop.
- placing of one loop inside the body of another loop
- most commonly used in for loops
Nested Loop
-When working with nested loops,the outer loop changes only
after the inner loop is completely finished (or is interrupted).

2/2

You might also like