Lesson 04 Control Structures I : Decision Making

Slides:



Advertisements
Similar presentations
Microsoft Visual Basic: Reloaded Chapter Five More on the Selection Structure.
Advertisements

Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Week 4 Selections This week shows how to use selection statements for more flexible programs. It also describes the various integral types that are available.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Making Decisions.
Chapter 4 Making Decisions
C++ for Engineers and Scientists Third Edition
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Decision Structures and Boolean Logic
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Visual C# 2005 Decision Structures. Visual C# Objectives Understand decision making Learn how to make decisions using the if statement Learn how.
Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
1 Lecture 5: Selection Structures. Outline 2  Control Structures  Conditions  Relational Operators  Logical Operators  if statements  Two-Alternatives.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Flow of Control Part 1: Selection
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
Chapter 5: More on the Selection Structure
Programming with Microsoft Visual Basic th Edition
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Chapter Making Decisions 4. Relational Operators 4.1.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Controlling Program Flow with Decision Structures.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
1 Windows Forms II Chapter RadioButton / GroupBox Controls Used to solicit a multiple choice input. Radio buttons work as a group. Selecting one.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Control Structures- Decisions. Smart Computers Computer programs can be written to make computers seem smart Making computers smart is based on decision.
Lesson #4 Logical Operators and Selection Statements.
Lesson #4 Logical Operators and Selection Statements.
The if…else Selection Statement
More on the Selection Structure
Decision Structure ISYS 350.
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Chapter 4: Making Decisions.
Lecture 3- Decision Structures
EGR 2261 Unit 4 Control Structures I: Selection
Flow of Control.
The Selection Structure
Topics The if Statement The if-else Statement Comparing Strings
Chapter 4: Decision Structures and Boolean Logic
Chapter 4: Making Decisions.
Lecture 3 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz
Chapter 4: Making Decisions.
Java Programming: Guided Learning with Early Objects
Microsoft Visual Basic 2005 BASICS
Control Structures – Selection
Making Decisions in a Program
Topics The if Statement The if-else Statement Comparing Strings
Work with Data and Decision Structure
Chapter 8 JavaScript: Control Statements, Part 2
Chapter 3:Decision Structures
Chapter 4: Decision Structures and Boolean Logic
Chapter 7 Conditional Statements
Chapter 4: Control Structures I (Selection)
CprE 185: Intro to Problem Solving (using C)
Chapter 4: Decision Structures and Boolean Logic
Presentation transcript:

Lesson 04 Control Structures I : Decision Making MIT 31043, Rapid Application Development By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT Faculty of Management and Commerce South Eastern University of Sri Lanka

Decision Structures A control structure is a logical design that controls the order in which statements execute A sequence structure is a set of statements that execute in the order that they appear A decision structure execute statements only under certain circumstances A specific action is performed only if a certain condition exists Also known as a selection structure By: S. Sabraz Nawaz

A Simple Decision Structure The flowchart is a single-alternative decision structure It provides only one alternative path of execution In C#, you can use the if statement to write such structures. A generic format is: if (expression) { Statements; etc.; } The expression is a Boolean expression that can be evaluated as either true or false Cold outside True Wear a coat False By: S. Sabraz Nawaz

Relational Operators A relational operator determines whether a specific relationship exists between two values Operator Meaning Expression > Greater than x > y Is x greater than y? < Less than x < y Is x less than y? >= Greater than or equal to x >= y Is x greater than or equal to y? <= Less than or equal to x <= y Is x less than or equal to you? == Equal to x == y Is x equal to y? != Not equal to x != y Is x not equal to you? By: S. Sabraz Nawaz

Examples that use relational operators By: S. Sabraz Nawaz

BRANCHING By: S. Sabraz Nawaz

BRANCHING Branching is the act of controlling which line of code should be executed next. The line to jump to is controlled by some kind of conditional statement. This conditional statement is based on a comparison between a test value and one or more possible values using Boolean logic. This section describes three branching techniques available in C#: The ternary operator The if statement The switch statement By: S. Sabraz Nawaz

The Ternary Operator The simplest way to perform a comparison is to use the ternary (or conditional) operator; this operator works on three operands. By: S. Sabraz Nawaz

The if Statement The if statement is a far more versatile and useful way to make decisions. Unlike the ternary operator, if statements don’t have a result (so you can’t use them in assignments); instead, you use the statement to conditionally execute other statements. The simplest use of an if statement is as follows, where <test> is evaluated (it must evaluate to a Boolean value for the code to compile) and the line of code that follows the statement is executed if <test> evaluates to true: By: S. Sabraz Nawaz

The if Statement if (sales > 50000) bonus = 500;  { bonus = 500; } sales > 50000 True bonus = 500 False By: S. Sabraz Nawaz

The if / else Statement An if-else statement will execute one block of statement if its Boolean expression is true or another block if its Boolean expression is false It has two parts: an if clause and an else clause You can also specify additional code using the else statement in combination with an if statement. This statement is executed if <test> evaluates to false: Both sections of code can span multiple lines using blocks in braces: By: S. Sabraz Nawaz

Example of if-else Statement temp >40 display “hot” display “cold” True False if (temp > 40) { MessageBox.Show(“hot”); } else MessageBox.Show(“cold”); By: S. Sabraz Nawaz

Example By: S. Sabraz Nawaz

GUI Design By: S. Sabraz Nawaz

GUI – CheckBox A small area on a computer screen which, when selected by the user, shows that a particular feature has been enabled. A CheckBox control allows users to select a single or multiple options from a list of options. A typical CheckBox control has two possible states: Checked state is when the CheckBox has check mark on Unchecked is when the CheckBox is not checked Typically, we use a mouse to check or uncheck a CheckBox Checked property is true when a CheckBox is in checked state By: S. Sabraz Nawaz

GUI – CheckBox… By: S. Sabraz Nawaz

CheckedChanged Event Like the Click event for a button, the checkbox has CheckedChanged as its default event. The CheckedChanged event occurs when the value of the Checked property changes By: S. Sabraz Nawaz

GUI – RadioButton A RadioButton control provides a round interface to select one option from a number of options. Radio buttons are usually placed in a group on a container control such as a Panel or a GroupBox and one of them is selected. A typical RadioButton control has two possible states: Checked state is when the button has check mark on Unchecked is when the button is not checked Typically, we use a mouse to check or uncheck Checked property is true when a radiobutton is in checked state At a time only one radiobutton can be selected within a container By: S. Sabraz Nawaz

GUI – RadioButton… By: S. Sabraz Nawaz

Checking More Conditions Using if Statements You can also create a decision structure that evaluates multiple conditions to make the final decision using the if-else-if statement In C#, the generic format is: if (expression) { } else if (expression) … else By: S. Sabraz Nawaz

Checking More Conditions Using if Statements By: S. Sabraz Nawaz

Improved Marks Processor By: S. Sabraz Nawaz

Logical Operators The logical AND operator (&&) and the logical OR operator (||) allow you to connect multiple Boolean expressions to create a compound expression The logical NOT operator (!) reverses the truth of a Boolean expression Operator Meaning Description && AND Both subexpression must be true for the compound expression to be true || OR One or both subexpression must be true for the compound expression to be true ! NOT It negates (reverses) the value to its opposite one. Expression Meaning x >y && a < b Is x greater than y AND is a less than b? x == y || x == z Is x equal to y OR is x equal to z? ! (x > y) Is the expression x > y NOT true? By: S. Sabraz Nawaz

Sample Decision Structures with Logical Operators The && operator if (temperature < 20 && minutes > 12) { MessageBox.Show(“The temperature is in the danger zone.”); } The || operator if (temperature < 20 || temperature > 100) The ! Operator if (!(temperature > 100)) MessageBox.Show(“The is below the maximum temperature.”); By: S. Sabraz Nawaz

Nested Decision Structures You can create nested decision structures to test more than one condition. Nested means “one inside another” In C#, a generic format is: if (expression) { statements; } else statements By: S. Sabraz Nawaz

A Sample Nested Decision Structure Salary >= 40000 yearsOnJob >= 2 Display “Minimum salary requirement not met.” Display “You qualify for the load.” Display “Minimum years at current job not met.” End By: S. Sabraz Nawaz

A Sample Nested Decision Structure if (salary >= 40000) { if (yearOnJob >= 2) decisionLabel.Text = "You qualify for the loan." } else decisionLabel.Text = "Minimum years at current " + "job not met." decisionLabel.Text = "Minimum salary requirement " + "not met." By: S. Sabraz Nawaz

Example This program checks if the mark’s range is above 100 or below 0 (zero). If the mark is out of range (0-100), an error message is show using the MessageBox. If it is within the range, calculation is executed. By: S. Sabraz Nawaz

Example By: S. Sabraz Nawaz

switch

The switch Multiple-Selection Statement C# provides the switch multiple-selection statement to perform different actions based on the possible values of an expression. Each action is associated with the value of a constant integral expression or a constant string expression that the variable or expression on which the switch is based may assume. A constant integral expression is any expression involving character and integer constants that evaluates to an integer value or a constant. A constant string expression is any expression composed of string literals that always results in the same string. A switch statement compares ONE variable against MULTIPLE possible values By: S. Sabraz Nawaz

The syntax of the switch statement Supported data types: bool, char, String, integral, or enum By: S. Sabraz Nawaz

Sample switch Statement switch (month) { case 1: MessageBox.Show(“January”); break; case 2: MessageBox.Show(“February”); case 3: MessageBox.Show(“March”); default: MessageBox.Show(“Error: Invalid month”); } month Display “January” Display “February” Display “March” Display “Error: Invalid month”

switch Example I: Lottory By: S. Sabraz Nawaz

switch Example II: Month Finder By: S. Sabraz Nawaz

A switch statement that falls through the first case label By: S. Sabraz Nawaz

GUI - PictureBox You can display images on your form by using the PictureBox control. It is a simple control which has a main purpose of displaying images. All you have to do is browse for the desired image and Visual Studio will import it to your project. You can use several image formats such as JPEG, GIF, PNG, and BMP. Properties Description Image The image that will be displayed by the control. ImageLocation The path of the image to be displayed by the PictureBox. SizeMode Tells how the image will be displayed. By: S. Sabraz Nawaz

GUI – PictureBox… To display an image using the PictureBox control, there are multiple ways you can use. You can go to the Properties Window and find the Image property. Click the button to the right of it to bring out the Select Resource Dialog. By: S. Sabraz Nawaz

GUI – PictureBox… Once the image is displayed, it may not look like you want it to. If the loaded image is larger the size of the PictureBox, then the image will be clipped. You can use the SizeMode property to change the way the image is positioned or resized inside the control. PictureBoxSizeMode Description Normal The image will be positioned in the upper-left corner of the PictureBox and if the image is larger than the PictureBox, the image will be clipped. StretchImage Resizes the image to match the size of the PictureBox. AutoSize Resizes the PictureBox to match the size of the image. CenterImage The image is centered inside the PictureBox. If the image is larger than the PictureBox, the image will be clipped. Zoom Fits the whole image inside the PictureBox while maintaining the image’s size ratio. By: S. Sabraz Nawaz