Conditional and Loops


    

Table of Contents

  1. Conditional
  2. Loops

Conditional

Conditional statement enables a program to execute only specific block of code. There are two type of conditional statement in general:

If, Else, and Else If

if statement specifies a block of code to be executed when a condition is true.

Syntax:

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

Example:

#include<stdio.h> int main() { int myNum = 10; if(myNum == 10){ printf("myNum is 10"); } return 0; }

In the example, the code printf("myNum is 10"); will only be executed when the value of myNum is 10.


    

However, in other cases, we need another statement to specify code to be executed when the condition is false. Then, we can use else for this case.

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:

#include<stdio.h> int main() { int myNum = 10; if(myNum == 10){ printf("myNum is 10"); }else{ printf("myNum is not 10"); } return 0; }

Lastly, the else if statement is used when there are more than two conditions that may occurred. Else if statement specifies a new condition if the first condition is false.

Example:

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:

#include<stdio.h> int main() { int myNum = 10; if(myNum<1){ printf("myNum is less than 1"); }else if (myNum<10){ printf("myNum is less than 10"); }else{ printf("myNum is greater than or equal with 10"); } return 0; }

Short-hand if-else, also called ternary operator, is a simple construction of an if-else statement. The ternary operator is used to simplify a block of if-else statements to a single line of code.

Example:

variable = (condition) ? expressionTrue : expressionFalse;

Below is the simplified version of the previous if-else example:

#include<stdio.h> int main() { int myNum = 10; myNum = (myNum == 10)? printf("myNum is 10"):printf("myNum is not 10"); return 0; }

Switch Statement

Besides the if-else statement, there is also the switch statement. The switch statement is used when there are too many conditions involving one variable. Instead of using many if-else statements, you can use the switch statement.

Syntax:

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

The expression in the code specifies which variable that will be inspected. If the expression fits the case x, then the program runs the first code block, etc. If the expression does not fit in any of the cases, it will run the default code block. The break statement at the end of each code block is used to breaks out the switch block and stops the execution.

Note:
the default code block is optional.

Example:

#include<stdio.h> int main() { int day = 2; switch (day) { case 1: printf("Monday"); break; case 2: printf("Tuesday"); break; case 3: printf("Wednesday"); break; case 4: printf("Thursday"); break; case 5: printf("Friday"); break; case 6: printf("Saturday"); break; case 7: printf("Sunday"); break; } return 0; }

Loops

As its name says, loops are used to loop codes. Loops enable a code to run endlessly as long as it still corresponds with the specified condition. There are three types of loops in C:

While Loop

While loop loops through a block of code as long as the specified condition is true.

Example:

while (condition) { // code block to be executed }

Example:

#include<stdio.h> int main() { int a = 1; while(a<5){ printf("a is %d\n", a); a++; } return 0; }

The loop in the example will produce the output below:

Output:


    a is 1
    a is 2
    a is 3
    a is 4
        

Notice that the loop while keeps looping until a<5. When the loop reaches a = 5, it will stop looping and the program is terminated.



    
Do-While Loop

Do-while loop is a variant of the while loop. But, the different is do-while loop will execute once before checking if the condition is true or not. While loop does not run when the condition is false, while do-while loop runs at least once in every condition (even if the condition is false).

Syntax:

do { // code block to be executed }while (condition);

Example:

#include<stdio.h> int main() { int a = 1; do{ printf("Hello!"); a++; } while (a<1); return 0; }

The example loop above will still run once, even if the condition is false. If we use a while loop in this example, the loop will not run.



    
For Loop

For loop is used when we know how many times we want to loop through a block.

Example:

for (statement 1; statement 2; statement 3) { // code block to be executed }
  • Statement 1 is the initialization of the loop condition.
  • Statement 2 is the condition that has to be met to enable the looping process.
  • Statement 3 is executed (every time) after a code block has been executed. Statement 3 is usually an increment or decrement of a value.

    

Example:

#include<stdio.h> int main() { for(int i = 0; i<5 ; i++){ printf("Hello"); } return 0; }

The output of the program above will be a "Hello" text repeated five times.

Code explanation:

  • Statement 1 initializes the variable i = 0
  • Statement 2 specifies the condition for the loop. If the condition is true, then the looping will keep happening.
  • Statement 3 increases the value of i each time the code block has been executed. So, each time a code block has been executed, the value of i also increases by 1. Then, when the code already runs 5 times (the value of i = 5), the loop will stop (because the condition of i<5 is not true anymore).


    
Break and Continue

As seen before, a break statement is used to jump out of a switch statement. A break statement can also be used to jump out of a loop.

Example:

#include<stdio.h> int main() { int a = 1; while(a<10){ printf("Hello!"); if(a == 4){ break; } a++; } return 0; }

The code above will produce the output of Hello! printed 4 times. When the variable a reaches 4, it will automatically jump out of the loop, resulting in only 4 printed outputs.


    

The continue statement is used to skip the rest of the code and jump to the next iteration in the loop.

Example:

#include<stdio.h> int main() { for(int i=0; i<5; i++){ if(i == 3){ continue; } printf("%d", i); } return 0; }

If we run the code above, we will get the output 0124 with no '3'. This is because when reaches 3, we use the continue statement to skip the rest of the loop and move on to the next iteration (i = 4).