Lesson 04. Control Structures I : Decision Making. MIT 31043, VISUAL PROGRAMMING By: S. Sabraz Nawaz

Similar documents
Decision Structures. Lecture 3 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Decision Structures. Lesson 03 MIT 11053, Fundamentals of Programming

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

Chapter 2. Ans. C (p. 55) 2. Which is not a control you can find in the Toolbox? A. Label B. PictureBox C. Properties Window D.

Flow Control. CSC215 Lecture

Control Structures in Java if-else and switch

Software Design & Programming I

Chapter 4: Making Decisions

Lesson #4. Logical Operators and Selection Statements. 4. Logical Operators and Selection Statements - Copyright Denis Hamelin - Ryerson University

Chapter 4: Making Decisions

Control Structures in Java if-else and switch

Chapter 4: Making Decisions

Making Decisions and Working with Strings

5. Control Statements

Chapter 4 - Notes Control Structures I (Selection)

LECTURE 04 MAKING DECISIONS

Chapter 4: Making Decisions. Copyright 2012 Pearson Education, Inc. Sunday, September 7, 14

Decision Making in C

Lesson 7 Part 2 Flags

4.1. Chapter 4: Simple Program Scheme. Simple Program Scheme. Relational Operators. So far our programs follow a simple scheme

Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Advance mode: Auto

Dive Into Visual C# 2010 Express

Lecture 5 Tao Wang 1

Chapter 4 The If Then Statement

Introduction to Decision Structures. Boolean & If Statements. Different Types of Decisions. Boolean Logic. Relational Operators

Following is the general form of a typical decision making structure found in most of the programming languages:

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS240 BRANCHING STATEMENTS

CS313D: ADVANCED PROGRAMMING LANGUAGE

Conditional Statement

Dive Into Visual C# 2008 Express

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Chapter 2. Flow of Control. Copyright 2016 Pearson, Inc. All rights reserved.

C++ Programming: From Problem Analysis to Program Design, Third Edition

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

Conditions, logical expressions, and selection. Introduction to control structures

Chapter 2. Creating Applications with Visual Basic Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of

Flow Charts. Visual Depiction of Logic Flow

conditional statements

Chapter 3: Decision Structures

Introduction to Visual Basic and Visual C++ Arithmetic Expression. Arithmetic Expression. Using Arithmetic Expression. Lesson 4.

Decisions. Arizona State University 1

Topics. Chapter 5. Equality Operators

8.3 Common Loop Patterns

Chapter 3: Decision Structures

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 3: Decision Structures

Introduction to C Programming

switch case Logic Syntax Basics Functionality Rules Nested switch switch case Comp Sci 1570 Introduction to C++

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

BRANCHING if-else statements

Lesson 06 Arrays. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Graphical User Interface Canvas Frame Event structure Platform-free GUI operations Operator << Operator >> Operator = Operator ~ Operator + Operator

Intel x86 Jump Instructions. Part 5. JMP address. Operations: Program Flow Control. Operations: Program Flow Control.

Linear execution of statements. CISC 1600/1610 Computer Science I. Alternatives to linear execution. Linear execution of statements

Intel x86 Jump Instructions. Part 5. JMP address. Operations: Program Flow Control. Operations: Program Flow Control.

Computer Science 202 Introduction to Programming The College of Saint Rose Fall Topic Notes: Conditional Execution

Chapter Overview. More Flow of Control. Flow Of Control. Using Boolean Expressions. Using Boolean Expressions. Evaluating Boolean Expressions

Review. Relational Operators. The if Statement. CS 151 Review #4

Chapter 2. C++ Basics

Lesson 02 Working with Data Types MIT 31043, Visual Programming By: S. Sabraz Nawaz

Introductionto the Visual Basic Express 2008 IDE

Loops and Conditionals. HORT Lecture 11 Instructor: Kranthi Varala

DECISION STRUCTURES: USING IF STATEMENTS IN JAVA

Chapter Goals. 3.1 The if Statement. Contents 1/30/2013 DECISIONS

UEE1302(1102) F10: Introduction to Computers and Programming

5. Selection: If and Switch Controls

Full file at Programming in Visual Basic 2010

Administration. Conditional Statements. Agenda. Syntax. Flow of control. Lab 2 due now on floppy Lab 3 due tomorrow via FTP

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Operators. Java operators are classified into three categories:

CONDITIONAL EXECUTION: PART 2

BITG 1223: Selection Control Structure by: ZARITA (FTMK) LECTURE 4 (Sem 1, 16/17)

Making Decisions In Python

COMP Flow of Control: Branching 2. Yi Hong May 19, 2015

EEE-425 Programming Languages (2013) 1

Conditions, logical expressions, and selection. Introduction to control structures

Oct Decision Structures cont d

EEE-425 Programming Languages (2013) 1

Fundamentals of Programming Session 9

Logical Operators and switch

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

In this lab, you will learn more about selection statements. You will get familiar to

Chapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Lecture 8 CSE11 Fall 2013 While Loops, Switch Statement, Simplifying Conditionals

PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND T-SQL)

PIC 10A Flow control. Ernest Ryu UCLA Mathematics

Full file at

Control and Environments Fall 2017 Discussion 1: August 30, 2017 Solutions. 1 Control. If statements. Boolean Operators

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

BBM 101 Introduc/on to Programming I Fall 2013, Lecture 4

Control and Environments Fall 2017 Discussion 1: August 30, Control. If statements. Boolean Operators

Programming in C++ PART 2

You can call the project anything you like I will be calling this one project slide show.

Computer Programming, I. Laboratory Manual. Experiment #3. Selections

Microsoft Visual Basic 2005 CHAPTER 5. Mobile Applications Using Decision Structures

Transcription:

Lesson 04 Control Structures I : Decision Making MIT 31043, VISUAL PROGRAMMING 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 o A specific action is performed only if a certain condition exists o Also known as a selection structure

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; Statements; etc.; Cold outside False True Wear a coat The expression is a Boolean expression that can be evaluated as either true or false

Relational Operators A relational operator determines whether a specific relationship exists between two values Operator Meaning Expression Meaning > 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?

Examples that use relational operators strfirstname == "Rapid" // equal to a string literal yearstextbox.text == "" // equal to an empty string strmessage == String.Empty // equal to an empty string dbldiscountpercent == 2.3 // equal to a numeric literal blnisvalid == false // equal to the false value deccode == decproductcode // equal to another variable strlastname!= "Apps" dblyears > 0 inti < intmonths // not equal to a string literal // greater than a numeric literal // less than a variable dblsubtotal >= 500 // greater than or equal to // a literal value intquantity <= intreorderpoint // less than or equal to // a variable Slide 5

BRANCHING Slide 6

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#: o The ternary operator o The if statement o The switch statement Slide 7

The Ternary Operator The simplest way to perform a comparison is to use the ternary (or conditional) operator; this operator works on three operands. Slide 8

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. Slide 9

The if Statement 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: if (sales > 50000) bonus = 500; if (sales > 50000) bonus = 500; sales > 50000 True False bonus = 500 Slide 10

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 Both sections of code can span multiple lines using blocks in braces: 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: Slide 11

Example of if-else Statement False temp >40 True display cold display hot if (temp > 40) MessageBox.Show( hot ); else MessageBox.Show( cold );

Example Slide 13

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 if (expression) else Slide 14

Checking More Conditions Using if Statements Slide 15

Improved Marks Processor Slide 16

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?

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) MessageBox.Show( The temperature is in the danger zone. ); The! Operator if (!(temperature > 100)) MessageBox.Show( The is below the maximum temperature. );

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) if (expression) statements; else statements; else statements

A Sample Nested Decision Structure Salary >= 40000 Display Minimum salary requirement not met. yearsonjob >= 2 Display Minimum years at current job not met. Display You qualify for the load. End Slide 20

A Sample Nested Decision Structure if (salary >= 40000) else 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."

A Sample Nested Decision Structure if (strcustomertype == "R") if (dblsubtotal >= 100) decdiscountpercent =.2m; else decdiscountpercent =.1m; else // customertype isn't "R" decdiscountpercent =.4m; Slide 22

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. Slide 23

Example Slide 24

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 Slide 26

The syntax of the switch statement Supported data types: bool, char, String, integral, or enum Slide 27

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

switch Example I: Lottory Slide 29

switch Example II: Month Finder Slide 30

A switch statement that falls through the first case label switch (strcustomertype) case "R": case "C": decdiscountpercent =.2m; break; case "T": decdiscountpercent =.4m; break; Slide 31

GUI Design Slide 32

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: o Checked state is when the CheckBox has check mark on o 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 Slide 33

GUI CheckBox Slide 34

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 Slide 35

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: o Checked state is when the button has check mark on o 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 Slide 36

GUI RadioButton Slide 37

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 Image ImageLocation SizeMode Description The image that will be displayed by the control. The path of the image to be displayed by the PictureBox. Tells how the image will be displayed. Slide 38

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. Slide 39

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 StretchImage AutoSize 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. Resizes the image to match the size of the PictureBox. Resizes the PictureBox to match the size of the image. CenterImage Zoom The image is centered inside the PictureBox. If the image is larger than the PictureBox, the image will be clipped. Fits the whole image inside the PictureBox while maintaining the image s size ratio. Slide 40

THANK YOU Slide 41