+ All Categories
Home > Documents > Selection Structure

Selection Structure

Date post: 22-Feb-2016
Category:
Upload: junius
View: 42 times
Download: 0 times
Share this document with a friend
Description:
Selection Structure. by Yonglei Tao. Conditional Statements. Write an algorithm that asks the user for two numbers , and print the larger one. Get the values of a, b from user If the value of a is greater than the value of b then print “The larger number is ” & a Else - PowerPoint PPT Presentation
Popular Tags:
11
Selection Structure by Yonglei Tao
Transcript
Page 1: Selection Structure

Selection Structure

by Yonglei Tao

Page 2: Selection Structure

Conditional Statements Write an algorithm that asks the user for two

numbers, and print the larger one.

Get the values of a, b from userIf the value of a is greater than the value of b then

print “The larger number is ” & a Else print “The larger number is ” & b

Test 1: What does the program print if the user enters 10, 3?

Test 2: What does the program print if the user enters 2, 11?

Page 3: Selection Structure

Conditional Statements (Cont.) Write an algorithm to compute the distance

traveled and the average miles per gallon on a trip when given as input the number of gallons used and the starting and ending mileage readings on the odometer.

get gallons, start, enddistance = end – startmpg = distance / gallonsprint mpgif mpg > 25.0 then

print “You are getting good gas mileage”

Page 4: Selection Structure

Example One

Page 5: Selection Structure
Page 6: Selection Structure

Example One (Cont.)Private Sub btnCalculate_Click(…)

Dim operation As StringDim num1, num2, answer As Integer

operation = txtOperation.Textnum1 = Val (txtFirstNumber.Text) num2 = Val (txtSecondNumber.Text)

If operation = “1” Then answer = num1 + num2 lblDisplay.Text = “Sum: ” & answer Else answer = num1 – num2 lblDisplay.Text = “Difference: “ & answerEnd If

End Sub

Page 7: Selection Structure

Example Two

Page 8: Selection Structure
Page 9: Selection Structure

Example Two (Cont.)Private Sub btnDisplay_Click(…)

Dim num1, num2 As Integernum1 = Val (txtFirstNumber.Text) num2 = Val (txtSecondNumber.Text) If num1 > num2 Then Dim temp as Integer temp = num1 num1 = num2 num2 = tempEnd IflblDisplay.Text = “Lowest: “ & num1 &

ContrlChars.NewLine _& Highest: “ & num2

End Sub

Page 10: Selection Structure

Flowcharts for Conditional Logic

Page 11: Selection Structure

Exercise Write an algorithm that asks the user to

enter three numbers, and computes and prints out the largest number


Recommended