Nested Loops
It is also possible to place a loop inside another loop. This is called a nested loop. Continue reading C Nested Loops
It is also possible to place a loop inside another loop. This is called a nested loop. Continue reading C Nested Loops
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:
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
To demonstrate a practical example of the while loop, we have created a simple “countdown” program:
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
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
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.
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:
switch (expression) { case x: // code block break; case y: // code block break; default: // code block }
This is how it works :
switch
expression is evaluated oncecase
break
statement breaks out of the switch block and stops the executiondefault
statement is optional, and specifies some code to run if there is no case matchThis example shows how you can use if..else
to “open a door” if the user enters the correct code:
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
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
Use the else if
statement to specify a new condition if the first condition is false
.
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
}
int time = 22; if (time < 10) { printf("Good morning."); } else if (time < 20) { printf("Good day."); } else { printf("Good evening."); } // Outputs "Good evening."
Use the else
statement to specify a block of code to be executed if the condition is false
.
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 }
int time = 20; if (time < 18) { printf("Good day."); } else { printf("Good evening."); } // Outputs "Good evening."