Lab 4-ctrl Structure

  • Uploaded by: safuan_alcatra
  • 0
  • 0
  • May 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Lab 4-ctrl Structure as PDF for free.

More details

  • Words: 949
  • Pages: 5
LAB 4 – CONTROL STRUCTURE OBJECTIVE: The main objective is to introduce the students with the loop. There are a few types of loop, i.e while loop, for loop. Students will be able to know how to use the loop from the problem given to the students. INSTRUCTIONS: Instruction : Open VB 2008  Create Project  Console Application 1. Name your program as While.vb ' Fig. 4.5: While.vb ' Demonstration of While structure. Module modWhile Sub Main() Dim product As Integer = 2 ' structure multiplies and displays product ' while product is less than 1000 While product <= 1000 Console.Write("{0} ", product) product = product * 2 End While Console.WriteLine() ' write a blank line ' print result Console.WriteLine("Smallest power of 2 " & _ "greater than 1000 is {0}", product) Console.ReadLine() ' prevents window from closing End Sub ' Main End Module ' modWhile

' Fig. 5.1: WhileCounter.vb ' Using the While structure to demonstrate counter-controlled ' repetition. Module modWhileCounter Sub Main() Dim counter As Integer = 2 ' initialization Session: June - Dec 2008 Subject Code: FSB23103

Page: 1 of 5

LAB 4 – CONTROL STRUCTURE

While (counter <= 10) ' repetition condition Console.Write(counter & " ") counter += 2 ' increment counter End While End Sub ' Main End Module ' modWhileCounter

2. Name your program as Average.vb ' Fig. 4.14: Average.vb ' Using counter-controlled repetition. Module modAverage Sub Main() Dim total As Integer ' sum of grades Dim gradeCounter As Integer ' number of grades input Dim grade As Integer ' grade input by user Dim average As Double ' class average ' initialization phase total = 0 ' set total to zero gradeCounter = 1 ' prepare to loop ' processing phase While gradeCounter <= 10 ' prompt for input and read grade from user Console.Write("Enter integer grade: ") grade = Console.ReadLine() total += grade ' add grade to total gradeCounter += 1 ' add 1 to gradeCounter End While ' termination phase average = total / 10 ' write a blank line and display class average Console.WriteLine() Console.WriteLine("Class average is {0}", average)

Session: June - Dec 2008 Subject Code: FSB23103

Page: 2 of 5

LAB 4 – CONTROL STRUCTURE End Sub ' Main End Module ' modAverage

3. Name your program as ForCounter.vb ' Fig. 5.2: ForCounter.vb ' Using the For/Next structure to demonstrate counter-controlled ' repetition. Module modForCounter Sub Main() Dim counter As Integer ' initialization, repetition condition and ' incrementing are included in For structure For counter = 2 To 10 Step 2 Console.Write(counter & " ") Next End Sub ' Main End Module ' modForCounter

' Fig. 5.5: Sum.vb ' Using For/Next structure to demonstrate summation. Imports System.Windows.Forms Module modSum Sub Main() Dim sum = 0, number As Integer ' add even numbers from 2 to 100 For number = 2 To 100 Step 2 sum += number Next MessageBox.Show("The sum is " & sum, _ "Sum even integers from 2 to 100", _ MessageBoxButtons.OK, MessageBoxIcon.Information) End Sub ' Main End Module ' modSum

Session: June - Dec 2008 Subject Code: FSB23103

Page: 3 of 5

LAB 4 – CONTROL STRUCTURE INSTRUCTIONS: Write a program to convert the Celsius temperature to Fahrenheit. The user will input the initial Celsius temperature, the times of conversion needed and the increment of each temperature to be converted. The sample output was given as a guideline.

Sample output:

Double click the “Convert to F” button and simply add the below codes in the Private Sub cmdconvert click and End Sub button. (1)

Add three Labels control to the Form. Named each label as lblcelcius, lblconvert , lblctr

Session: June - Dec 2008 Subject Code: FSB23103

Page: 4 of 5

LAB 4 – CONTROL STRUCTURE (2)

Set its Text property to “Temperature in Celcius” for lblcelcius; “Times of coversions” for lblconvert and “Icrement the temp by ___ degrees” for lblctr. 

set the property ForeColor to Black;



click the ‘+’ box to the left of the Font property to expand the menu and set the property Size to 12 and Bold to True.

(3)

Add three texboxes and set its property Name as txtcelcius, txtconvert and txtctr next to the labels for user input.

(4)

Add a Button control named cmdconvert with the text ‘Convert to F’ to the form. Change the text font to bold and colour to blue.

(5)

Double click on the button ‘Convert to F’ to open the code window. Add the following code between the lines ‘Private Sub cmdconvert_Click’ and ‘End Sub’: Dim degF, degC As Double ' temparature variab Dim i, bil, inc As Integer Dim output As String degC = txtcelcius.Text bil = txtbil.Text inc = txtctr.Text i = 1 ' calculate amount after each year output &= "Times of conversion" & vbTab & vbTab & "Temp in C" & _ vbTab & vbTab & "Temp in F" & vbCrLf While i <= bil degF = degC * (9.0 / 5.0) + 32.0 output &= i & vbTab & vbTab & vbTab & _ String.Format("{0:F}", degC) & _ vbTab & vbTab & String.Format("{0:F}", degF) & vbCrLf degC = degC + inc i = i + 1 End While ' display output MessageBox.Show(output, "Temperature conversion", _ MessageBoxButtons.OK, MessageBoxIcon.Information)

(6)

Save the project File > Save All

(7)

Run the program by clicking on

QUESTIONS: Write a program to convert the Celsius temperature to Fahrenheit. The user will input the range of the temperature need to be converted i.e. the initial and the final of Celsius temperature, and the increment of each temperature to be converted. Please use the sample given above as your guideline.

Session: June - Dec 2008 Subject Code: FSB23103

Page: 5 of 5

Related Documents

Data Structure Lab Practical
November 2019 19
Structure
April 2020 17
Structure
April 2020 20
Structure
October 2019 31
Structure
November 2019 26