0% found this document useful (0 votes)
10 views18 pages

Chapter 3

Control structures enable programmers to control the flow and logic of a program. The two major categories are selection structures and repetition structures. Selection structures like if/else statements allow conditional execution of code depending on whether a condition is true or false. Repetition structures like while and for loops repeat code for a specified number of iterations or until a condition is met.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views18 pages

Chapter 3

Control structures enable programmers to control the flow and logic of a program. The two major categories are selection structures and repetition structures. Selection structures like if/else statements allow conditional execution of code depending on whether a condition is true or false. Repetition structures like while and for loops repeat code for a specified number of iterations or until a condition is met.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

3

CONTROL STRUCTURES
Control Structures
Control structures are programming constructs for controlling the flow of execution of a program and change the
behavior of the program accordingly. Normally, statements in a program (including the ones we have written so
far) are executed sequential order.

Control structures enable the programmer to specify the order in which programs are to be executed i.e. skip some
parts of the code, repeat some parts of the code, transfer of control to another program etc.

There are two major categories of control structures:


 Selection Structures
 Repetition Structures
Selection
The selection structures enable conditional execution of code i.e. the code makes decision depending on a condition
being met. The program selects one of two or more possible paths depending on the outcome of an already tested
condition(s).

The condition must of necessity be a statement


that evaluates to a Boolean. There are several
constructs that can be used to achieve such
conditional execution of code.
Python: Selection
The 3 available selection structures in Python are
The if Statement: It enables Python to choose among alternative courses of action. It either performs (selects) an
action if a condition (predicate) is true or skips the action if the condition is false. It is a single-selection structure
because it selects or ignores a single path of action.
Syntax: The general form of an if-structure is:
if (expression):
<python statements>
<python statements>

1 >>> Grade = 87
2 >>> if(Grade >= 60):
3 print "Passed“
4
5 Passed
Python: Selection
The if…else Statement: The if-else selection structure allows the programmer to specify an action to be performed
when a condition is true and a different action to be performed when the condition is false.
Syntax:
The general form of an if/else structure is:
if (expression):
<python statements1>
else:
<python statements2>
1 Grade = 60
2 if (Grade >= 40):
3 print("Pass !")
4 else:
5 print("Fail !")
6
7 >>>
8 Pass !
Python: Selection
The if…..elif……else Statement:
This allows for a case when we have multiple conditions and corresponding actions to perform
Syntax: The general form of an if..elif..else structure is:
if (expression 1):
<python statements1>
elif(expression 2): 1 Grade = 60
2 if (Grade >= 70):
<python statements2> 3 print("A")
4 elif (Grade >= 60):
else: 5 print("B")
6 elif (Grade >= 50):
<python statements3> 7 print("C")
8 elif (Grade >= 40):
9 print("D")
You can have as many elif as needed 10 else:
11 print("F")
12 >>> B
PHP: Selection
The “if” statement checks the truthfulness of an expression and, if the expression is true, evaluates the statement.
And if it evaluates to false - it'll ignore it
1 <?php
2 $num = 4
3 if ($num> 0){
4 echo “the number is positive”;
5 }
6 ?>

To specify an alternative statement to execute when the expression is false Use the “else” keyword:
1 <?php
2 $num = 4
3 if ($num> 0){
4 echo “the number is positive”;
5 }
6 else{
7 echo “the number is not positive”;
8 }
9 ?>
PHP: Selection
elseif extends an if statement to execute more than one alternative statements in cases where there
are several conditions to be met.
1 <?php
2 $num = 4
3 if ($num> 0){
4 echo “the number is positive”;
5 }
6 elseif($num == 0){
7 echo “the number is zero”;
8 }
9 else{
10 echo “the number is negative”;
11 }
12 ?>

TERNARY CONDITIONAL OPERATOR: The ternary conditional operator (? :) can be used to shorten
simple true/false tests.
1 <?php echo ($user_input> 0) ? "the number is positive" : "the
number is not positive"; ?>
PHP: Selection
The switch statement is similar to a series of IF statements on the same expression. In many occasions, the same
variable (or expression) is compared with many different values. The value (case) that evaluates to true is
executed up to the first break keyword it finds. If none match, and a default is given, all statements following the
default keyword are executed, up to the first break keyword encountered.

1 <?php
2 switch ($user_input){
3 case 0:
4 echo “the number is a multiple of 3”;
5 break;
6 case 1:
7 echo “the number is not a multiple of 3”;
8 break;
9 case 2:
10 echo “the number is not a multiple of
11 3”;
12 break;
13 default:
14 echo “input is invalid”;
15 break;
?>
Repetition
A repetition structure allows the programmer to specify that a program should repeat an action while some
condition remains true.
Python: Repetition
The while Statement: The while statement executes a suite until a condition is met. The expression or condition
should evaluate to false at some point, otherwise infinite loop occurs and the program hangs.
Syntax:
The general form of a while statement is:
while (expression):
<python statements1>
else:
<python statements2>
1 >>> num = 3
Note: The else clause is optional. 2 >>> while(num < 0):
3 print “counting”, num
4 num=num-1
5
counting 3
counting 2
counting 1
Python: Repetition
The for Statement
Sometimes it is desire that a set of instructions is to executed repeatedly, the Python for statement allow us to
iterate through a sequence of values.
Syntax:
The general form of a while statement is:
for <var> in <sequence>:
<body: python statements>
<body: python statements >
1 >>> num = range(3)
2 >>> for x in num:
3 print “counting”, x
4
counting 3
counting 2
counting 1
Python: Repetition
The range() function: The function range() is used to create a consecutive sequence of values: It produces a list
of numbers starting with 0 and continuing up to, but not including a specified maximum value.
The different mode of operation is shown below;
 range ( n)
Generates values starting from 0 up to n (but not including n)
 range ( start, n)
Generates values starting from start up to n but not including n
 range ( start, n, step )

1 >>> print range(6)


2 1,2,3,4,5
3 >>> print range(1,11)
4 1,2,3,4,5,6,7,8,9,10
5 >>> print range(0,20,3)
6 0,3,6,9,12,15,18
PHP: Repetition
The simplest form of repetition in PHP is the while statement:

If the expression evaluates to true, the statement is executed and then the expression is re-evaluated (if it is still
true, the body of the loop is executed again, and so on). The loop exits when the expression is no longer true, i.e.,
evaluates to false.

1 <?php
2 $i = 1;
3 while ($i<= 10) {
4 $i=Si-1;
5 echo $i;
6 }
7 ?>
PHP: Repetition
do...while loops are very similar to while loops, except the truth expression is checked at the end of each iteration
instead of in the beginning.

The main difference from regular while loops is that the first iteration of a do..while loop is guaranteed to run at
least once(the truth expression is only checked at the end of the iteration)

1 <?php
2 $i = 1;
3 do {
4 $i=Si-1;
5 echo $i;
6 } while ($i<= 10)
7 ?>
PHP: Repetition
The for statement is similar to the while statement, except it adds counter initialization and counter manipulation
expressions, and is often shorter and easier to read than the equivalent while loop.

The expression start is evaluated once, at the beginning of the for statement. Each time through the loop, the
expression condition is tested. If it is true, the body of the loop is executed; if it is false, the loop ends. The
expression increment is evaluated after the loop body runs.

1 <?php
2 for ($i=0; $i<= 10; $i++) {
3 echo $i;
4 }
5 ?>
PHP: Repetition
foreach works only on arrays, and will issue an error when you try to use it on a variable with a different data
type or an uninitialized variables. The foreach statement allows you to iterate over elements in an array.

1 <?php
2 $i=array(1,2,3,4,5,6,7,8,9)
3 foreach ($num as $i) {
4 echo $num;
5 }
6 ?>
Python and PHP

Control Structures Python PHP


Selection if, if….else, if…elseif…else if, if….else, if…elseif…else,
switch

Repetition while, for while, do….while, for, foreach

You might also like