C Nested Loops

Nested Loops

It is also possible to place a loop inside another loop. This is called a nested loop. Continue reading C Nested Loops

C For Loop

For Loop

When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop:

Syntax

for (expression 1; expression 2; expression 3) {
  // code block to be executed
}

Expression 1 is executed (one time) before the execution of the code block.

Expression 2 defines the condition for executing the code block.

Expression 3 is executed (every time) after the code block has been executed. Continue reading C For Loop

C While Loop Examples

Real-Life Examples

To demonstrate a practical example of the while loop, we have created a simple “countdown” program:

Example

int countdown = 3;

while (countdown > 0) {
  printf("%d\n", countdown);
  countdown--;
}

printf("Happy New Year!!\n");

In this example, we create a program that only print even numbers between 0 and 10 (inclusive): Continue reading C While Loop Examples

C Do/While Loop

The Do/While Loop

The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true. Continue reading C Do/While Loop

C While Loop

Loops

Loops can execute a block of code as long as a specified condition is reached.

Loops are handy because they save time, reduce errors, and they make code more readable.

Continue reading C While Loop

C Switch

Switch Statement

Instead of writing many if..else statements, you can use the switch statement.

The switch statement selects one of many code blocks to be executed:

Syntax

switch (expression) {
  case x:
    // code block
    break;
  case y:
    // code block
    break;
  default:
    // code block
}

This is how it works :

  • The switch expression is evaluated once
  • The value of the expression is compared with the values of each case
  • If there is a match, the associated block of code is executed
  • The break statement breaks out of the switch block and stops the execution
  • The default statement is optional, and specifies some code to run if there is no case match

Continue reading C Switch

C If … Else Examples

Real-Life Examples

This example shows how you can use if..else to “open a door” if the user enters the correct code:

Example

int doorCode = 1337;

if (doorCode == 1337) {
  printf("Correct code.\nThe door is now open.");
} else {
  printf("Wrong code.\nThe door remains closed.");
}

This example shows how you can use if..else to find out if a number is positive Continue reading C If … Else Examples

C Short Hand If Else

Short Hand If…Else (Ternary Operator)

There is also a short-hand if else, which is known as the ternary operator because it consists of three operands. It can be used to replace multiple lines of code with a single line. It is often used to replace simple if else statements: Continue reading C Short Hand If Else

C Else If

The else if Statement

Use the else if statement to specify a new condition if the first condition is false.

Syntax

if (condition1) {
  // block of code to be executed if condition1 is true
} else if (condition2) {
  // block of code to be executed if the condition1 is false and condition2 is true
} else {
  // block of code to be executed if the condition1 is false and condition2 is false
}

Example

int time = 22;
if (time < 10) {
  printf("Good morning.");
} else if (time < 20) {
  printf("Good day.");
} else {
  printf("Good evening.");
}
// Outputs "Good evening."

Continue reading C Else If

C Else

The else Statement

Use the else statement to specify a block of code to be executed if the condition is false.

Syntax

if (condition) {
  // block of code to be executed if the condition is true
} else {
  // block of code to be executed if the condition is false
}

Example

int time = 20;
if (time < 18) {
  printf("Good day.");
} else {
  printf("Good evening.");
}
// Outputs "Good evening."

Continue reading C Else