Professional Documents
Culture Documents
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."
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) {
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) {
} else {
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) {
} else if (condition2) {
} else if (condition3) {
} else {
Syntax:
if (condition1) {
// code block 1
if (condition2) {
// code block 2
} else {
// code block 3
}
} else {
// code block 4
#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:
Example:
#include <stdio.h>
int main()
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) {
Example:
#include <stdio.h>
int main() {
int i = 0;
while (i < 5) {
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 {
} while (condition);
Example:
#include <stdio.h>
int main()
int i = 0;
do {
i++;
return 0;
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;
if (i == 5) {
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;
if (i == 3) {
return 0;
}
return statements:
SYNTAX:
return ;
EXAMPLE:
#include <stdio.h>
int main()
int num = 5;
int result;
result = square(num);
return 0;