FuzzDuck
FuzzDuck
If else
FuzzDuck
If else

If - Else statement

The if statement is used when we only want a specific code to be executed if certain conditions are met. When the program gets to the if statement it checks the condition and if the condition is true the code inside the stamen is run. We can extend the statement with either an else statement or an else-if statement. The else statement runs the content code if the first if condition turns out to be false. The else-if statement is checked like an ordinary if stamen only if the first if-statement turns out to be false.

Code Example - Declaring class with getter and setter methods
// --- Declaring starting point of the program run with "main" method --- //
public static void Main(string[] args){

 // --- Instantiating number to iterate through --- //
 int number = 25;

 // --- If "number" is over 100 the following code will run --- //
 if(number >= 100) {
  Console.WriteLine("This if statement is run if number is 100 or over");
 }

 // --- If the statement above is false, but the number is over 50 the following code will run --- //
 else if (number >= 50) {
  Console.WriteLine("This if else statement is run, if number is 50 or over");
 }

 // --- If none of the other if statements are executed, run through this code --- //
 else {
  Console.WriteLine("This else statement is run, if none of the other if statements are true");
 }

 // --- Invoking 'ReadKey()' to make time to read the output --- //
 Console.ReadKey();

}
// --- Declaring starting point of the program run with "main" method --- //
public static void main(String[] args) {

 // --- Instantiating number to iterate through --- //
 int number = 25;

 // --- If "number" is over 100 the following code will run --- //
 if(number >= 100) {
  System.out.println("This if statement is run if number is 100 or over");
 }

 // --- If the statement above is false, but the number is over 50 the following code will run --- //
 else if (number >= 50) {
  System.out.println("This if else statement is run, if number is 50 or over");
 }

 // --- If none of the other if statements are executed, run through this code --- //
 else {
  System.out.println("This else statement is run, if none of the other if statements are true");
 }

}
// --- Instantiating number to iterate through --- //
$number = 25;

// --- If "number" is over 100 the following code will run --- //
if($number >= 100) {
 echo "This if statement is run if number is 100 or over";
}

// --- If the statement above is false, but the number is over 50 the following code will run --- //
else if ($number >= 50) {
 echo "This if else statement is run, if number is 50 or over";
}

// --- If none of the other if statements are executed, run through this code --- //
else {
 echo "This else statement is run, if none of the other if statements are true";
}