Compunet Corporation1 Programming with Visual Basic.NET Selection Structure If-Else Week 4 Tariq Ibn Aziz.

Slides:



Advertisements
Similar presentations
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 Making Decisions in a Program.
Advertisements

Programming with Microsoft Visual Basic th Edition
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 9 Decisions, Decisions, Decisions.
Subject: Information Technology Grade: 10
CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions.
ITEC113 Algorithms and Programming Techniques
Chapter 4: The Selection Structure Programming with Microsoft Visual Basic.NET, Second Edition.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
IS437: Fall 2004 Instructor: Dr. Boris Jukic Program Flow Control: Decisions and Conditions (Branching) Controlled Repetition (Looping)
Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be.
Decisions (Conditional Programming) Chapter 5 (Sec. 5.1 & 5.2)
Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands.
CSC110 Fall Chapter 5: Decision Visual Basic.NET.
Control Structures: Getting Started Sequence and Selection also arithmetic operators, data types, logical operators.
Chapter 4: Control Structures: Selection
1 Selection Structures. 2 Making Decisions Sample assignment statements to figure worker pay with possible overtime PayAmount = Hours * Rate PayAmount.
C++ for Engineers and Scientists Third Edition
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
Compunet Corporation1 Programming with Visual Basic.NET Arithmetic, Logical & Bitwise Operators Week # 3 Tariq Ibn Aziz.
The Program Design Phases
Chapter Four The Selection Structure
Microsoft Visual Basic 2008: Reloaded Fourth Edition
Chapter 4: The Selection Structure
Flow Charting. Goals Create Algorithms using Flow Charting procedures. Distinguish between Flow Charting and Pseudocode. Top-Down Design Bottom-up Design.
Chapter 4: The Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
Chapter 4: The Selection Structure
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Lecture Set 5 Control Structures Part A - Decisions Structures.
Problem Solving with Decisions
CONTROL STRUCTURE The if, elseif, and else & switch Statements 1.
Flow of Control Part 1: Selection
Chapter 5 - VB 2005 by Schneider1 Chapter 5 – Decisions 5.1 Relational and Logical Operators 5.2 If Blocks.
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
Compunet Corporation1 Programming with Visual Basic.NET While, Do and For – Next Loops Week 5 Tariq Ibn Aziz.
VBScript Language. What is VBScript Based on the Visual Basic family of languages Supports object oriented features Interpreted Loosely Typed Implicitly.
Control Structures.  Need to have program statements that control execution flow  Simple statements to branch execution based on conditions (If/Then/Else)
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Decisions  Relational operators  Operate on two numbers or two Strings  ==, !=, >, >=,
Conditional Expression One of the most useful tools for processing information in an event procedure is a conditional expression. A conditional expression.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Chapter Four The Selection Structure Programming with Microsoft Visual Basic th Edition.
Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Topics: Selection Statements. Processing Involving Selecting Instructions An instruction that allows deviation and selection to take place uses the ‘IF’
Controlling Program Flow with Decision Structures.
Tutorial 4: The Selection Structure 1 Tutorial 4 The Selection Structure.
ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software.
ALGORITHMS AND FLOWCHARTS. A typical programming task can be divided into two phases: Problem solving phase  produce an ordered sequence of steps that.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
Lecture 2: Introduction to Programming EEE2108: 공학프로그래밍 서강대학교 전자공학과 2011 학년도 2 학기 - Algorithms and Flowcharts -
Flow Charts And Pseudo Codes Grade 12. An algorithm is a complete step-by- step procedure for solving a problem or accomplishing a task.
TUTORIAL 4 Visual Basic 6.0 Mr. Crone. Pseudocode Pseudocode is written language that is part-code part- English and helps a programmer to plan the layout.
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Exam 1 Review Jeff has 100 students in his MIS120 class. He is giving a 50 question exam worth 100 points. The following commands are available to you.
Program design Program Design Process has 2 phases:
ALGORITHMS AND FLOWCHARTS
The if…else Selection Statement
COVERED BASICS ABOUT ALGORITHMS AND FLOWCHARTS
The Selection Structure
Chapter 4: The Selection Structure
Programming with Microsoft Visual Basic 2008 Fourth Edition
Microsoft Visual Basic 2005 BASICS
Making Decisions in a Program
ALGORITHMS AND FLOWCHARTS
Objectives After studying this chapter, you should be able to:
Microsoft Visual Basic 2005: Reloaded Second Edition
Computer Science Core Concepts
The Selection Structure
Presentation transcript:

Compunet Corporation1 Programming with Visual Basic.NET Selection Structure If-Else Week 4 Tariq Ibn Aziz

Compunet Corporation2 Objectives Write pseudocode for the selection structure Create a flowchart to help you plan an application’s code Write an If...Then...Else statement Write code that uses comparison operators and logical operators

Compunet Corporation3 The Selection Structure Condition: expression evaluating to true or false Selection (decision) structure –Chooses one of two paths based on a condition Example –If employee works over 40 hours, add overtime pay Four selection structures in Visual Basic –If, If/Else, If/ElseIf/Else, and Case

Compunet Corporation4 Writing Pseudocode for If and If/Else Selection Structures If selection structure –Contains only one set of instructions –Instructions are processed if the condition is true If/Else selection –Contains two sets of instructions –True path: instruction set following true condition –False path: instruction set following false condition

Compunet Corporation5 Writing Pseudocode for If and If/Else Selection Structures Figure 4-4: Examples of the If and If/Else selection structures written in pseudocode

Compunet Corporation6 Flowcharting the If and If/Else Selection Structures Flowchart –Set of standardized symbols showing program flow Oval: the start/stop symbol Rectangle: the process symbol Parallelogram: the input/output symbol Diamond: selection/repetition symbol

Compunet Corporation7 Flowcharting the If and If/Else Selection Structures (continued) Figure 4-5: Examples of the If and If/Else selection structures drawn in flowchart form

Compunet Corporation8 Structure chart Find roots of quadratic equation Read in coefficients Solve using quadratic formula Print results

Compunet Corporation9 Pseudocode Prompt user for inputs Read in a, b, c Calculate roots using quadratic eq. Print results

Compunet Corporation10 Flowchart Start Prompt user Read a,b,c x1 = [-b + sqrt(b 2 -4ac)]/2a x2 = [-b – sqrt(b 2 -4ac)]/2a Print x1, x2 Stop

Compunet Corporation11 Coding the If and If/Else Selection Structures Syntax –If condition Then statement block for true path [Else statement block for false path] End If condition must be a Boolean expression The Else clause is optional

Compunet Corporation12 Comparison Operators Comparison (relational) operators: –Test two items for equality or types of non-equality Rules for comparison operators –Cause an expression to evaluate to true or false –Have lower precedence than arithmetic operators –Are evaluated from left to right Example: 5 -2 >  3 > 3  False

Compunet Corporation13 Comparison Operators (continued) Figure 4-7: Listing and examples of commonly used comparison operators (continued)

Compunet Corporation14 Comparison Operators (continued) Figure 4-7: Listing and examples of commonly used comparison operators

Compunet Corporation15 IF Statement Visual Basic allows you to test conditions and perform different operations depending on the results of that test If ( condition ) Then statement

Compunet Corporation16 IF Statement Executing Statements if a Condition Is True Imports System.DateTime Public Module SingleIF Sub Main() Dim MyDate As Date = #2/13/1973# If MyDate < Now Then MyDate = Now System.Console.WriteLine (MyDate) End Sub End Module

Compunet Corporation17 If-Else Statement You can use If...Then...Else with the Else statement to define two blocks of executable statements. One block is executed if the condition is True, the other if it is False. If (expression) Then statement1 Else statement2 End If if expression is true statement1 will be executed otherwise statement2.

Compunet Corporation18 If-Else Statement Example Public Module IfElse Sub Main() Dim d As Decimal =10.28 If (d >= 0) Then System.Console.WriteLine(d) Else System.Console.WriteLine(-d) End If End Sub End Module

Compunet Corporation19 If-ElseIf Statement Visual Basic tests the conditions in the order they appear in the If...Then...Else statements tests until it encounters a True condition or an Else statement, at which point it executes the corresponding statement block. Execution then branches to the end of the If...Then...Else block

Compunet Corporation20 If-ElseIf Statement Example Public Module NestedIF Sub Main() DIM Performance As Integer = 2 DIM Salary As Integer = 2 If Performance = 1 Then System.Console.WriteLine (Salary + Salary * 0.1) ElseIf Performance = 2 Then System.Console.WriteLine (Salary + Salary * 0.09) ElseIf Performance = 3 Then System.Console.WriteLine (Salary + Salary * 0.07) Else System.Console.WriteLine (Salary + Salary * 0) End If End Sub End Module

Compunet Corporation21 If-ElseIf Statement Example Public Module IfElseLadder Sub Main() DIM I As Integer = 1 If I < 0 Then System.Console.WriteLine ("-ve num") ElseIf I = 1 Then System.Console.WriteLine ("One") ElseIf I = 2 Then System.Console.WriteLine ("Two") ElseIf I = 3 Then System.Console.WriteLine ("Three") Else System.Console.WriteLine ("> 3") End If End Sub End Module

Compunet Corporation22 IIf Function This example uses the IIf function to evaluate the TestMe parameter of the CheckIt procedure and returns the word "Large" if the amount is greater than 1000; otherwise, it returns the word "Small". Imports Microsoft.VisualBasic Public Module IIfFunction Sub Main() Dim CheckIt As String CheckIt = IIf (TestMe > 1000, "Large", "Small") System.Console.WriteLine ( CheckIt ) End Sub End Module

Compunet Corporation23 Select Case This example uses the Select Case statements to write a line corresponding to the value of the variable Number. The second Case statement contains the value that matches the current value of Number, so the statement that writes "Between 6 and 8" is executed.

Compunet Corporation24 Select Case Imports System.Console Public Module SelectCase Sub Main() Dim Number As Integer = 8 Select Number ' Evaluate Number. Case 1 To 5 ' Number between 1 and 5, inclusive. WriteLine ("Between 1 and 5") ' The following is the only Case clause that’s True. Case 6, 7, 8 ' Number between 6 and 8. WriteLine ("Between 6 and 8") Case 9 To 10 ' Number is 9 or 10. WriteLine ("Greater than 8") Case Else ' Other values. WriteLine ("Not between 1 and 10") End Select End Sub End Module

Compunet Corporation25 Relational & Boolean operator OperatorAction AND AND(always evaluate both sides) OR OR (always evaluate both sides) XOR Exclusive ORNOT

Compunet Corporation26 Relational & Boolean operator PQP AND QP OR QP XOR QNOT P False True FalseTrueFalseTrue False True False True False

Compunet Corporation27 Relational & Boolean operator This example uses the And operator to perform a logical conjunction on two expressions. The result is a Boolean value Public Module RB Sub Main() Dim A As Integer = 10 Dim B As Integer = 8 Dim C As Integer = 6 Dim myCheck As Boolean myCheck = A > B And B > C ' Returns True. System.Console.WriteLine (myCheck) myCheck = B > A And B > C ' Returns False. System.Console.WriteLine (myCheck) End Sub End Module

Compunet Corporation28 Relational Operator Imports System.Console Public Module RelationalOpr Sub Main() Dim j AS Integer =6 Dim k AS Integer =7 Write ( "j = " ) WriteLine ( j ) ' 6 Write ( "k = " ) WriteLine ( k ) ' 7 WriteLine ( "Relational Operator") Write ( "j < k " ) WriteLine ( j < k ) ' True Write ( "j <= k " ) WriteLine ( j <= k )'True Write ( "j = k " ) WriteLine ( j = k )'False Write ( "j > k " ) WriteLine ( j > k )'False Write ( "j >= k " ) WriteLine ( j >= k )'False Write ( "j <> k " ) WriteLine ( j <> k ) 'True End Sub End Module

Compunet Corporation29 Boolean Operator Imports System.Console Public Module LogicalOp Sub Main() Dim j AS Integer =6 Dim k AS Integer =7 Write ( "j = " ) WriteLine ( j )' 6 Write ( "k = " ) WriteLine ( k )' 7 WriteLine ( "Relational Operator") Write ( "j AND k " ) '110 & 111 WriteLine ( j AND k ) Write ( "j OR k " ) ' 110 or 111 WriteLine ( j OR k ) Write ( "j XOR k " ) ' 110 xor 111 WriteLine ( j XOR k ) Write ( " NOT k " ) WriteLine ( NOT k ) '1000 End Sub End Module