List and describe at least two kinds of control statements P
List and describe at least two kinds of control statements? Provide examples and uses in VB & C# languages.
Solution
Two control statements in C# (for loop and if-the-else)
For
This loop is most useful when the number of iterations are known.
for (int idx = 0; idx < 5; idx++)
arr(idx) = \"apple\"
If-then-else
There are 3 forms of if-then-else statement:
// single selection
if (number > 0)
Console.WriteLine(\"{0} is positive\", number);
//if-then-else selection
if (number > 0)
Console.WriteLine(\"{0} is positive\", number);
else
Console.WriteLine(\"{0} is not positive\", number);
//multicase selection
if (number == 0)
Console.WriteLine(\"number is zero\");
else if (number > 0)
Console.WriteLine(\"{0} is positive\", number);
else
Console.WriteLine(\"{0} is negative\", number);
Two control statements in VB (for and while)
For
This loop is most useful when the number of iterations are known.
for (int idx = 0; idx < 5; idx++)
arr(idx) = \"apple\"
while
repeating a specific number of code until the guard condition is met
Dim product As Integer = 5
While product <= 1000
product = product * 2
End While
For..Next Loop
repeats=ing a group of code for a specified number of times mostly used when number of iterations are known.
start=1
end = 5
For var = start To end
show message
Next var

