Difference between revisions of "CSharp to Pseudo Code"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Assigning a value)
(Do While (Repeat Until) Loop)
 
(6 intermediate revisions by the same user not shown)
Line 12: Line 12:
  
 
===C#===
 
===C#===
 +
Remember to check if something is equal in C# is '=='.
 +
 +
Remember 'Greater than or Equal' or Less than or Equal' is '>=' or '<='.
 +
 
<syntaxhighlight lang=csharp>
 
<syntaxhighlight lang=csharp>
 
if (factorfound == false)
 
if (factorfound == false)
Line 39: Line 43:
 
}
 
}
 
else
 
else
 +
{
 +
    .........
 +
}
 +
</syntaxhighlight>
 +
 +
==For Loop==
 +
In pseudo code a for statement has several parts, the command word 'FOR' followed by the name of a variable. This is assigned the value ('←') of a starting number, through to a finishing number.
 +
 +
For example:
 +
 +
FOR Count = 1 to 3
 +
      ..........
 +
ENDFOR
 +
 +
When this code is run for the first time 'Count' will equal '1', once the internal code is run the next time through 'Count' will equal '2', and then the final time 'Count' will equal '3'.
 +
 +
===C#===
 +
We still have all of the same elements, a Variable set to the starting value and a condition for the finishing value. The final section is what increments the count to the next value:
 +
 +
<syntaxhighlight lang=csharp>
 +
for (int Count = 1; Count <= 3 ; Count++)
 +
{
 +
    .........
 +
}
 +
</syntaxhighlight>
 +
 +
==Foreach Loop==
 +
In pseudo code a for statement has several parts, the command word 'FOREACH' followed by a data type, followed by the name of a variable.Then the word 'IN' and then the name of a data structure:
 +
 +
For example:
 +
 +
FOREACH (Character C in String)
 +
      ..........
 +
ENDFOR
 +
 +
Sometimes it might not have a data type, For example:
 +
 +
FOREACH (HexDigit in HexString)
 +
      ..........
 +
ENDFOR
 +
 +
When this code is run for the first time 'Count' will equal '1', once the internal code is run the next time through 'Count' will equal '2', and then the final time 'Count' will equal '3'.
 +
 +
===C#===
 +
We still have all of the same elements, a data type of the element, a variable name for the element. Then the command word 'in' followed by a data structure:
 +
 +
<syntaxhighlight lang=csharp>
 +
string words = "computer science"
 +
foreach (char letter in words)
 
{
 
{
 
     .........
 
     .........
Line 71: Line 124:
 
     ............
 
     ............
 
}
 
}
 +
</syntaxhighlight>
 +
 +
==Do While (Repeat Until) Loop==
 +
The command words used in the exam questions is:
 +
DO
 +
    ............
 +
WHILE (Condition)
 +
 +
or:
 +
REPEAT
 +
    ............
 +
UNTIL (Condition)
 +
 +
So the condition is always contained within the round brackets.
 +
 +
===C#===
 +
Remember to check if something is equal in C# is '=='.
 +
 +
Remember 'Greater than or Equal' or Less than or Equal' is '>=' or '<='.
 +
 +
<syntaxhighlight lang=c#>
 +
do
 +
{
 +
    ............
 +
}
 +
while (factorfound == false)
 +
 
</syntaxhighlight>
 
</syntaxhighlight>

Latest revision as of 09:11, 23 April 2021

Assigning a value

'←' is to assign a value to a variable, essentially just like '=' in C#.

IF

In pseudo code an if statement has several parts, the command word 'IF' followed by a condition, followed by the word 'THEN'.

For example:

IF factorfound = FALSE THEN

The code to run for the if will be between the 'THEN' and the 'ENDIF.

C#

Remember to check if something is equal in C# is '=='.

Remember 'Greater than or Equal' or Less than or Equal' is '>=' or '<='.

if (factorfound == false)
{
     .........
}

If Else

The same as above, however the code to run for the if will be between the 'THEN' and the 'ELSE'. The code between the 'ELSE' and the 'ENDIF' will be run when the condition is false.

For example:

IF factorfound = FALSE THEN
     .........
ELSE
     .........
ENDIF

The code to run for the if will be between the 'THEN' and the 'ENDIF.

C#

if (factorfound == false)
{
     .........
}
else
{
     .........
}

For Loop

In pseudo code a for statement has several parts, the command word 'FOR' followed by the name of a variable. This is assigned the value ('←') of a starting number, through to a finishing number.

For example:

FOR Count = 1 to 3
     ..........
ENDFOR

When this code is run for the first time 'Count' will equal '1', once the internal code is run the next time through 'Count' will equal '2', and then the final time 'Count' will equal '3'.

C#

We still have all of the same elements, a Variable set to the starting value and a condition for the finishing value. The final section is what increments the count to the next value:

for (int Count = 1; Count <= 3 ; Count++)
{
     .........
}

Foreach Loop

In pseudo code a for statement has several parts, the command word 'FOREACH' followed by a data type, followed by the name of a variable.Then the word 'IN' and then the name of a data structure:

For example:

FOREACH (Character C in String)
     ..........
ENDFOR

Sometimes it might not have a data type, For example:

FOREACH (HexDigit in HexString)
     ..........
ENDFOR

When this code is run for the first time 'Count' will equal '1', once the internal code is run the next time through 'Count' will equal '2', and then the final time 'Count' will equal '3'.

C#

We still have all of the same elements, a data type of the element, a variable name for the element. Then the command word 'in' followed by a data structure:

string words = "computer science"
foreach (char letter in words)
{
     .........
}

While Loop

The command words used in the exam questions is:

WHILE (Condition)
     ....................
ENDWHILE

Condition will be something which equates to either true or false. Some examples of the conditions are:

WHILE (root * root) < Number

or:

WHILE (factorfound = false)

So the condition is always contained within the round brackets.

C#

Remember to check if something is equal in C# is '=='.

Remember 'Greater than or Equal' or Less than or Equal' is '>=' or '<='.

while (factorfound == false)
{
     ............
}

Do While (Repeat Until) Loop

The command words used in the exam questions is:

DO
    ............ 
WHILE (Condition)

or:

REPEAT
    ............ 
UNTIL (Condition)

So the condition is always contained within the round brackets.

C#

Remember to check if something is equal in C# is '=='.

Remember 'Greater than or Equal' or Less than or Equal' is '>=' or '<='.

do
{
     ............
}
while (factorfound == false)