If/Else Statement:
The code between the curly brackets executes only if it meets the value of the boolean expression:
if(expression)
{
statement1
}
else
{
statement2
}
If expression is true, execute statement1, else if it is false, execute statement2. The following is a much more realistic example:
string sentence = "";
if(name == "Stephanie")
{
sentence = "Hello " + name;
}
else
{
sentence = "Hello there";
}
An improvement over this if/else statement is the nested if/else. This means that when the expression is executed, the next step is to find another expression to fullfill. Example:
string sentence = "";
if(name == "Stephanie")
{
if(surname == "Grima")
sentence = "Hello " + name + " " + surname;
else
sentence = "Hello " + name;
}
else
{
sentence = "Hello there";
}
In the example above, we first check if the user's name is Stephanie, and if it is, we check whether her surname is Grima or not. If it is, her full name is saved.
Switch Statement
It is a control statement, which lets you transfer control to different statements within the switch, according to the value in the switch expression.
switch(expression)
{
case "statement1" :
statement1 body;
break;
case "statement2" :
statement1 body;
break;
default :
default body;
break;
}
If the switch expression matches the criteria within one of the statements, the body beneath it is performed until it meets the end of the body, which is break. If the expression doesn't meet any of the criteria, the code within the default is executed, if it is there. An example is the following:
String output = "";
switch(choice)
{
case "delivered" :
output = "The packet has been recieved";
break;
case "transit" :
output = "The packet is still being transferred";
break;
default :
output = "The packet has been lost";
break;
}
If the expression is choice is equal to delivered or transit, the body between their case and break is performed. If the expression in choice is NOT equal to any of the cases, the default case is used. If the packet is not delivered or in transit, we assume that it has been lost.
While Statement
The while statement is continously executed a block of code, until the condition is not true anymore. The while statement is also called a loop since it executes repeatedly one after the other until the condition is not met anymore.
while (expression) {
statement/s
}
A proper example of a while loop is the following:
int counter = 1;
string result = "";
while (counter < 11)
{
result += "Count is: " + counter;
counter++;
}
Explanation: While the counter is less than 11, therefore equal to ten, add to the result variable "Counter is : " and the counter amount. The value of counter changes with "counter++" which means: increment the value in counter by one.
DO/WHILE Statement
So people may ask: What is the difference between a while loop and a do while loop?
- The difference is that the do... while has the expression in the bottom of the loop. This means that he code has to be executed AT LEAST, ONCE.
- The while statement on the other side, is not executed at all if the condition is false from the beginning.
This is how a do while looks:
do {
statement/s
} while (expression);
A more real example is the following:
int counter = 1;
results = "";
do
{
results + = "Count is: " + counter;
count++;
} while (counter <= 11);
For Statement
The for statement, mostly known as for loop, is a way to iterate over a range of values. It is mostly referred to as a loop since it is used as an iteration until the expression is fullfilled.
for(initialization; termination; increment)
{
statement/s
}
- Initialization: this is executed once, to start the loop
- Termination: the loop stops once this expression becomes false
- Increment: After each iteration, this incrementation is invoked. The value here is either of incrementation or decrementation
A proper example is as follows:
string result = "";
for(int i=1; i<6; i++)
{
result += "Count is: " + i;
}