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

Lecture 3 Week 1

Note

Uploaded by

simrankaurchaa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
14 views4 pages

Lecture 3 Week 1

Note

Uploaded by

simrankaurchaa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 4
ae Saba Muskan Petey te pat for code Chapter 3: Secure Coding - Selection Structures in Programming Introduction In the realm of secure coding and software development, understanding control flow is paramount. This chapter delves into selection structures, which allow programmers to make decisions within their code. ‘The primary focus will be on Boolean operators, logical operations, relational operations, ‘and conditional statements, which are essential for controlling the flow of a program. These elements empower developers to create algorithms—structured sequences of instructions that dictate the behavior of software. Mastery of these concepts not only enhances the functionality ofa program but also contributes to its security by enabling developers to implement robust decision-making processes. Key Concepts + Algorithms: A sequence of instructions that perform a task. © Components of Algorithms: + Sequence: Steps are performed in a specific order. + Selection: Choices made between different paths based on conditions. + Iteration: Repeating a set of instructions until a condition is met. Boolean Operators and Logical Operations + Boolean Variables: These represent true or false values. © Example: bool male = true; bool athlete + Boolean Operators: © &B&: Logical AND co | [:Logical OR ©! Logical NOT ‘+ Relational Operators: : Equal to ' Not equal to Greater than Less than. reater than or equal to © <=:Less than or equal to ‘Scanned with |CamScanner De Morgan's Laws ‘+ De Morgan's Laws clarify the relationship between logical conjunctions and disjunctions: © Negation of a conjunction: © WA8&B)==18]| 1B ‘© Negation of a disjunction: + WAL| B)== 1A 88 1B If Statements: Basics and Examples If statements are fundamental for controlling program flow. They allow the program to execute certain blocks of code based on whether a condition evaluates to true or false. © Syntay if (Boolean condition) { // Code to execute if condition is true } © Example: int num = 4; if (num == 4) { cout <<"Num is 4"; Else Statements The else clause provides an alternative path when the initial condition is false. = Syntax: if (Boolean condition) {//If true }else {// If false } + Example: 4) {cout << "Num is 4"; }else { cout << "Num is not: if(num Nested If Statements Nesting occurs when an if statement is placed within another. This allows for more complex decision- making. + Syntax: if (condition) { if (condition2) { // Code if both conditions are true }else {// Code if condition is true and condition2 is false }} else { // Code if condition1 is false } The Switch Statement ‘The switch statement is a streamlined way to handle multiple conditions based on the value of a single variable, particularly when dealing with integer types. + Syntax: ‘Scanned with |CamScanner ‘switch (variable) { case option1: // Code for option1 break; case option2: // Code for option2 break; default: // Code if no options are matched } + Example: intichoice = 2; switch (iChoice) { case 1: cout << "First choice selected" << endl; break; case 2: cout << second choice selected" << endl; break; default: cout << "Invalid choice" << endl; break; } Conditional Operators Conditional operators provide a shorthand way of writing simple if-else statements. + Syntax: condition ? expressiont : expression2; + Example: b=(x<= 10) 20: 10; // Ifxis less than or equal to 10, bis 0; otherwise, b is 10 Real-World Applications + User Input Handling: Using if statements to respond to user input effectively. © Example: char input; cout << "Would you lke to continue? (y/n): cout << "Great, let's carry on" << endl; } in >> input; if (input / HL input ==") { + Password Verification: Checking user passwords against stored values to grant access. Conclusion ‘The concepts of selection structures—including if statements, else clauses, nested conditions, and switch statements—are foundational in programming, These structures allow developers to control the flow of their programs based on dynamic conditions, enabling the creation of responsive and interactive applications. Understanding when to use if statements versus switch statements can enhance code readability and maintainability. As secure coding practices evolve, the mastery of these selection constructs remains crucial for developing reliable and safe software applications. By grasping these principles, programmers can effectively break down complex problems and implement secure and efficient solutions. ‘Scanned with |CamScanner ——— = Overview of selection statements in progr anny —{ Selection Agenaa }~ : Bh importance of controling program flow (— @ Understanding logical operations: AND ( Boolean Operators }— fr tec “Relational operations greater than, less than, (— Dsymacot isomers if Stavamanns Basis} — \— Bil importance of conditional branches in controlling program flow a _f “acercepttsn crap ater isk acter Nesting IfStatements | Busing Pea eee Sach Satenene bis }—{ @ Syntax of switch statements: I How algorithms can be used to create calculators }-—{_Bxamptes: A = Bi Utizing selection statements to hancle user inputs and operations 2B remary operator syntax Conditional Operators | ———— Bi simpliying conditional logie with shorthand Ba Z 8 ey building blocks: else ant switch staternents 1—{ Summary of Selection Structures —{__ understanding when to use S80) (ype af statement or facie pr ograring so structures and cod Further Readine 9 @ Recommendation: far additional resource re coding pein ‘Scanned with

You might also like