You are on page 1of 11

Control statements

conditional statement:
A conditional statement, in programming and logic, is a statement that performs
different actions depending on whether a condition evaluates to true or false. It's
like saying "if this condition is true, then do this; otherwise, do something else."

In programming languages like Python, Java, or C++, conditional statements are


often implemented using constructs like if-else statements, switch statements, or
ternary operators.

There are generally three main types of conditional statements:


if statements:

These are used to execute a block of code only if a specified condition is true. If
the condition is false, the code block will be skipped or another block of code will
be executed (if there's an accompanying else statement).

Syntax:

if (condition) {

// code block to execute if condition is true

if-else statements:

These are used to execute one block of code if a specified condition is true and
another block of code if the condition is false.

Syntax:

if (condition) {

// code to be executed if the condition is true

} else {

// code to be executed if the condition is false

if-elif-else statements :
These are used when there are multiple conditions to be checked. The first
condition that evaluates to true will have its corresponding block of code
executed. If none of the conditions are true, an optional else block can be
executed.

Syntax:

if (condition1) {

// code to be executed if condition1 is true

} else if (condition2) {

// code to be executed if condition1 is false and condition2 is true

} else if (condition3) {

// code to be executed if condition1 and condition2 are false and condition3 is


true

} else {

// code to be executed if all conditions are false

If statements can be nested inside other if statements. This is


Nested If Statements:
useful when you need to check for multiple conditions within conditions .

Syntax:

if (condition1) {

// code block 1

if (condition2) {

// code block 2

} else {

// code block 3

}
} else {

// code block 4

use a if statement to check whether a number is positive:

#include <stdio.h>
int main()
{
int number;
printf("Enter a number: ");
scanf("%d", &number);
if (number > 0)
{
printf("The number is positive.\n");
}
return 0;
}
uses an if-else statement to check whether a number is even or odd:
#include <stdio.h>
int main()
{
int number;
printf("Enter a number: ");
scanf("%d", &number);
if (number % 2 == 0)
{
printf("The number is even.\n");
} else {
printf("The number is odd.\n");
}
return 0;
}
if-elif-else statements to determine the type of a triangle based on its
side lengths:
#include <stdio.h>
int main()
{
float side1, side2, side3;
printf("Enter the lengths of the three sides of the triangle: ");
scanf("%f %f %f", &side1, &side2, &side3);
if (side1 == side2 && side2 == side3) {
printf("It is an equilateral triangle.\n");
}
else if (side1 == side2 || side1 == side3 || side2 == side3) {
printf("It is an isosceles triangle.\n");
}
else {
printf("It is a scalene triangle.\n");
}
return 0;
}
example code demonstrating nested if conditions :
#include <stdio.h>

int main() {
int x = 10;
int y = 20;

if (x == 10) {
printf("x is equal to 10\n");

if (y == 20) {
printf("y is equal to 20\n");
} else {
printf("y is not equal to 20\n");
}
} else {
printf("x is not equal to 10\n");
}

return 0;
}
switch statement:
The switch statement in C is a powerful control flow mechanism that allows the
program to make decisions based on the value of a variable or expression. It
provides an efficient way to select one of many code blocks to execute based on
the value of a given expression.
SYNTAX:
switch (expression) {
case constant1:
// code to execute if expression equals constant1
break;
case constant2:
// code to execute if expression equals constant2
break;
// more case statements if needed
default:
// code to execute if expression doesn't match any constant
}
EXAMPLE:
#include <stdio.h>

int main() {
int choice;

printf("Select an option:\n");
printf("1. Option 1\n");
printf("2. Option 2\n");
printf("3. Option 3\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch(choice) {
case 1:
printf("You selected Option 1.\n");
break;
case 2:
printf("You selected Option 2.\n");
break;
case 3:
printf("You selected Option 3.\n");
break;
default:
printf("Invalid choice!\n");
}

return 0;
}

looping statements:
looping statements allow you to execute a block of code repeatedly as long as a
specified condition is true. There are three main types of looping statements:
for,while and do –while

for Loop:

The for loop is typically used when you know how many times you want to
execute a block of code. It consists of three parts: initialization, condition, and
update.

Syntax:

for (initialization; condition; update)

// code to be executed repeatedly

Example:

#include <stdio.h>

int main()

for (int i = 0; i < 5; i++) {

printf("Iteration: %d\n", i);

return 0;

while Loop:
The while loop is used when you want to execute a block of code as long as a
condition is true. The condition is checked before each iteration.

Syntax:

while (condition) {

// code to be executed repeatedly

Example:

#include <stdio.h>

int main() {

int i = 0;

while (i < 5) {

printf("Iteration: %d\n", i);

i++;

return 0;

do-while Loop:

The do-while loop is similar to the while loop, but the condition is checked after
each iteration. This guarantees that the code inside the loop will execute at least
once.

Syntax:

do {

// code to be executed repeatedly

} while (condition);

Example:

#include <stdio.h>
int main()

int i = 0;

do {

printf("Iteration: %d\n", i);

i++;

} while (i < 5);

return 0;

Control Transfer Statements:


These are used to transfer control from one part of the program to another.

break statements:

These are used to exit a loop or switch statement prematurely. The break
statement in C is used to terminate the execution of the innermost loop or switch
statement in which it appears. It allows you to exit the loop or switch statement
prematurely based on a certain condition.

SYNTAX:

break;

EXAMPLE:

#include <stdio.h>

int main()

int i;

for (i = 1; i <= 10; i++) {

if (i == 5) {

printf("Breaking out of loop at i = 5\n");


break; // Exit the loop when i equals 5

printf("i = %d\n", i);

return 0;

continue statements:

The continue statement in C is used to skip the current iteration of a loop and
proceed to the next iteration. It is commonly used within loops to skip certain
iterations based on a condition without terminating the loop entirely.

These are used to skip the current iteration of a loop and continue with the next
iteration.

SYNTAX:

continue;

EXAMPLE:

#include <stdio.h>

int main() {

int i;

for (i = 1; i <= 5; i++) {

if (i == 3) {

printf("Skipping iteration at i = 3\n");

continue; // Skip the current iteration when i equals 3

printf("i = %d\n", i);

return 0;
}

return statements:

The return statement in C is used to terminate the execution of a function and


return a value to the caller. It can also be used to return early from a function if
necessary.These are used to exit a function and return a value to the caller.

SYNTAX:

return ;

EXAMPLE:

#include <stdio.h>

// Function to calculate the square of a number

int square(int num)

int result = num * num;

return result; // Return the square of the number

int main()

int num = 5;

int result;

// Call the square function and store the result

result = square(num);

// Print the result

printf("The square of %d is %d\n", num, result);

return 0;

You might also like