Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

IP - Mod 2 - Program Control Structures and Analysis, Lecture notes of Programming Paradigms

This document introduces program control structures that enable a program to do sequential execution, selection, and repetition. It explains selection and repetition structures, relational and logical operators, and different types of loops. The document also provides some notes and precautions when using loops and discusses nested control structures. suitable for computer science students who are learning programming basics.

Typology: Lecture notes

2020/2021

Available from 04/02/2022

gwen-hermo
gwen-hermo 🇵🇭

10 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
IP Mod 2
Program Control Structures and Analysis
Introduction to Program Control Structures
enable a program not only to do sequential execution but also selection and repetition.
must only be answerable by yes or no
Selection Structure - execute a decision to alter the flow of the program depending on
some condition(s)
Repetition Structure - repeats a statement or a group of statements if a certain
condition(s) is still true.
Relational and Logical Operators
Comparison operators since they would make a Boolean (yes or no) decision by comparing the
left hand operand with a right hand operand
Relational operators - can be used on any simple data type.
Logical operators - enable you to combine Boolean statements together
- accept logical values (result of a Boolean operation) as operand and produce logical
values as result
o ! - reverse its result
o && - true if both of the right and the left expression are true
o || - true if only one or all of its statements are true.
Selection Structure
if statement - used for one, two and multiple path selection
switch - inherently a multiple path structure
One way selection structure
pf3
pf4
pf5

Partial preview of the text

Download IP - Mod 2 - Program Control Structures and Analysis and more Lecture notes Programming Paradigms in PDF only on Docsity!

Program Control Structures and Analysis

Introduction to Program Control Structures ➢ enable a program not only to do sequential execution but also selection and repetition. ➢ must only be answerable by yes or no

  • Selection Structure - execute a decision to alter the flow of the program depending on some condition(s)
  • Repetition Structure - repeats a statement or a group of statements if a certain condition(s) is still true. Relational and Logical OperatorsComparison operators since they would make a Boolean (yes or no) decision by comparing the left hand operand with a right hand operand
  • Relational operators - can be used on any simple data type.
  • Logical operators - enable you to combine Boolean statements together
  • accept logical values (result of a Boolean operation) as operand and produce logical values as result o! - reverse its result o && - true if both of the right and the left expression are true o || - true if only one or all of its statements are true. Selection Structure
  • if statement - used for one, two and multiple path selection
  • switch - inherently a multiple path structure → One way selection structure

Two way selection structure o Block of statements - group of code that is composed of more than one line; enclosing them within curly braces o Precaution for Relational Operator Associativity o Precaution for equality and assignment operatorMultiple Path selection (if-else if)

o If there is no match, the default label would execute. If there is no default label, the entire switch statement would be skipped. o The break statement is used to immediately exit from the switch statement. o The toupper (expression) statement on the switch expression is used to convert the gradeEquiv character to upper case. Repetition Structure ➢ repeatedly execute a statement or a group of statements until the specified condition is met.

  • pretest loop - evaluate a logical expression before the statement(s) within the loop is/are executed. → while loop - logical expression would act as the controller if the loop is to continue or to stop; - statements that are within the loop body can be a compound statement or a simple statement. o counter - initialized by a value then updated by a constant value o accumulator - used to add collect (add together) some value

o sentinel value - terminate a loop o flag value - a Boolean value that would terminate a loop → for loop - In summary the for loop operates with the following steps

  1. The initial statement is read.
  2. The loop condition is evaluated. If true the loop statement executes, then update statement is accomplished.
  3. Step 2 is repeated until loop condition would be evaluated as false.
  • Some notes when using for loop o If the loop condition is false the entire for loop statement will be skipped o The update statement must change the LCV (initialization) so that the condition would eventually be evaluated as false. o A semicolon before the loop body would immediately terminate the loop. o Omitting all three statements would implement an infinite loop.

continue statement - used skip the rest of the statements on a repetition structure.

  • Nested control structures - placing an entire loop (inner loop) within another loop (outside).