Pages

Friday, December 2, 2011

Writing basic program structure using ideone.com compiler





By: razi.smartcomputing123@gmail.com



Pre-requisite

You are expected to know the following topics before moving to the next slides. Click the links to learn more about them.

What is Computer Programming? (learn more)

What is ideone.com compiler? (learn more)



Introduction

This presentation demonstrates the application of basic control structure using C++ and VB.net language.

All codes can be tested on ideone.com compiler.



3 Basic Control Structures



Source Code Templates

C++

#include «iostream»

using namespace std;

int main() {

return 0;

}

VB.net

Imports System

Public Class Test

Public Shared Sub Main()

End Sub

End Class



The followings are the source code templates that is provided by ideone.com

Use this template as a starting point for you to do the programming exercises.

Sequential Control Structure

C++

#include «iostream»

using namespace std;

int main() {

cout««"Hello World"««endl;

cout««"........... "««endl;

return 0;

}






Sequential Control Structure is the default control structure, i.e computer execute statements one after another in top-down direction unless told otherwise.

The following codes display “Hello World” on the first line and dots on the second line.

Sequential Control Structure

Visual Basic .NET

Imports System

Public Class Test

Public Shared Sub Main()

Console.WriteLine("Hello World" & vbCrLf)

Console.WriteLine("..........." & vbCrLf)

Console.ReadLine() 'Wait for key to be pressed

End Sub

End Sub






Sequential Control Structure is the default control structure, i.e computer execute statements one after another in top-down direction unless told otherwise.

The following codes display “Hello World” on the first line and dots on the second line.

Sequential Control Structure & Variable Declaration

C++

#include «iostream»

using namespace std;

int main() {

int num1;

int num2;

int num3;

num1=1;

num2=2;

num3=num1+num2;

cout««"The output is "««num3««endl;

return 0;

}






Line 4,5,6 declare integer variables. Line 7,8,9 assign values to them. Line 10 outputs the value.

Sequential Control Structure & Variable Declaration

Visual Basic .NET

Imports System

Public Class Test

Public Shared Sub Main()

Dim num1 as integer

Dim num2 as integer

Dim num3 as integer

num1=1

num2=2

num3=num1+num2

Console.WriteLine("The output is " & num3 & vbCrLf)

End Sub

End Class






Line 4,5,6 declare integer variables. Line 7,8,9 assign values to them. Line 10 outputs the value.

Selection Control Structure

C++

#include «iostream»

using namespace std;

int main() {

int iMark;

iMark=50;

if (iMark»=40) {

cout««"Pass!"««endl;}

else {

cout««"Failed!"««endl;}

return 0;

}



Line 5 assigns the value for iMark. Line 6 evaluate whether the value is greater than or equals to 40. If TRUE, PRINT “Pass!”. Otherwise, PRINT “Failed!”

Selection Control Structure

Visual Basic .NET

Imports System

Public Class Test

Public Shared Sub Main()

Dim iMark as integer

iMark=50

if (iMark»=40) then

Console.WriteLine("Pass!" & vbCrLf)

else

Console.WriteLine("Failed!" & vbCrLf)

end if

End Sub

End Class



Line 5 assigns the value for iMark. Line 6 evaluate whether the value is greater than or equals to 40. If TRUE, PRINT “Pass!”. Otherwise, PRINT “Failed!”

Loop Control Structure

C++

#include «iostream»

using namespace std;

int main() {

int iMark;

iMark=30;

while (iMark«40) {

cout««iMark««endl;

iMark++;

}

return 0;

}



Line 5 assigns the value for iMark. Line 6 evaluate whether the value is less than 40. If it is TRUE, run a loop and add 1 to iMark in each loop. Line 8 uses increment operator ++

Loop Control Structure

Visual Basic .NET

Imports System

Public Class Test

Public Shared Sub Main()

Dim iMark as integer

iMark=30

do while (iMark«40)

Console.WriteLine(iMark & vbCrLf)

iMark+=1

loop

End Sub

End Class



Line 5 assigns the value for iMark. Line 6 evaluate whether the value is less than 40. If it is TRUE, run a loop and add 1 to iMark in each loop. Line 8 use increment operator +=

Summary

We have learned:

Console window output via ideone.com

3 basic control structures:

Sequential

Selection [IF … ELSE …]

Loop [WHILE (…) DO …]

Increment operator

Newline character



Next topics:

SWITCH SELECTION

DO (…) WHILE (…) LOOP

FOR LOOP

No comments:

Stats