CSEC1003 - Secure Coding Lecture 03 - Selection PDF
Document Details
Uploaded by TopQualityMagnesium9592
De Montfort University Leicester
Tags
Summary
This document is a lecture on secure coding, specifically focusing on selection statements and related topics like Boolean operators, if/else statements, switch statements, and conditional operators. It provides examples and explanations for these programming concepts within secure coding, including a simplified calculator example.
Full Transcript
CSEC1003 - Secure Coding Lecture 03 Selection 1 Agenda Boolean operators Logical operations Relational operations Equality operations If statements Basics Nesting Examples Switch statements Basics ...
CSEC1003 - Secure Coding Lecture 03 Selection 1 Agenda Boolean operators Logical operations Relational operations Equality operations If statements Basics Nesting Examples Switch statements Basics Examples Example: A simple calculator A computer program is a sequence of instructions which perform a task We call these sets of instructions algorithms A written or verbal set of a logical sequence of instructions Typically algorithms are made up of three structural components: Sequence Step 1 happens before step 2 which happens before step 3 Selection Sometimes a choice has to be made between two or more options This can lead to the algorithm having branches Iteration The repeating of an instruction or a sequence of instructions Can stop after a set number or on a certain condition You can use these in combination to solve nearly all In last lecture we saw variables including Boolean variables Boolean operators Used to find the logical value of some expression && and || or ! Not == equal to != not equal to > greater than < less than >= great than or equal to Two Boolean variables: bool male = true; bool athlete = true; What do the following mean: athlete && male; // athlete & male !athlete && male; athlete && !male; !athlete && !male; !(athlete && male); athlete || male; // athlete or male !athlete || male; athlete || !male; !athlete || !male; !(athlete || male); De Morgan’s Laws The negation of a conjunction is the disjunction of the negations not (A and B) is the same as (not A) or (not B) !athlete && !male == !(athlete || male); not (A or B) is the same as (not A) and (not B) !(athlete || male) == !athlete && ! male; What do the following mean: bool gt = 0 > 9; bool lt = 0 < 9; bool gte = 9 >= 9; bool lte = 14 9; // false bool lt = 0 < 9; // true bool gte = 9 >= 9; // true bool lte = 14 ‘n’; bool lt2 = ‘0’ < ‘9’; What do the following mean: bool gt = 0 > 9; // false bool lt = 0 < 9; // true bool gte = 9 > 9; // true bool lte = 14 < 15; // true bool gt2 = ‘y’ > ‘n’; // true bool lt2 = ‘0’ < ‘9’; // true int a = 5; bool between = 0 < a && a < 10 Allow conditional branches in programs If some condition is true then do something If that condition is not true then do something else Previou Flow diagram: s stateme nt If (expression) Statement tru e fals e Next statement Allow conditional branches in programs If some condition is true then do something If that condition is not true then do something else Syntax: if (Boolean condition) { Do stuff in here if condition is true } Allow conditional branches in programs If some condition is true then do something If that condition is not true then do something else Syntax: if (Boolean condition) { Do stuff in here if condition is true } Example: int num = 4; if(num == 4) { Else part of the if statement only runs when condition is not met: Syntax: if (Boolean condition) { Do stuff in here if condition is true } else { Do stuff in here if condition is false } Else part of the if statement only runs when condition is not met: int Example: num 4; = if(num 4) == { // Only do when num == } this 4 cout