0% found this document useful (0 votes)
26 views12 pages

Conditional Statements PDF

The document explains conditional statements in C programming, which allow programs to execute code based on specific conditions. It covers various types of conditional statements including if, if-else, else if ladder, and nested if statements, providing syntax and examples for each. The document serves as a guide for understanding how to implement decision-making in C code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views12 pages

Conditional Statements PDF

The document explains conditional statements in C programming, which allow programs to execute code based on specific conditions. It covers various types of conditional statements including if, if-else, else if ladder, and nested if statements, providing syntax and examples for each. The document serves as a guide for understanding how to implement decision-making in C code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Intelle Learn

CONDITIONAL STATEMENTS
INTELLE LEARN
What is a Conditional Statement in C?
A conditional statement in C programming is used to make decisions based
on certain conditions. It allows the program to execute a block of code only
when a specific condition is true. If the condition is false, the program can
execute another block of code or skip the conditional block entirely.

C provides several types of conditional statements:


if statement
if-else statement
else if ladder
nested if statement
switch statement
1. THE IF STATEMENT
The if statement is the simplest form of a conditional statement. It checks a
condition, and if the condition is true, the block of code associated with the if
statement is executed. If the condition is false, the program skips the block.

Syntax:
if (condition) {
// code to be executed if the condition is true
}
Example:
#include <stdio.h>

int main() {
int number = 10; Output:
if (number > 0) { The number is positive.
printf("The number is positive.\n"); In this example, the condition number > 0 is true, so the message
} "The number is positive." is printed.

return 0;
}
2. THE IF-ELSE STATEMENT
The if-else statement allows for two blocks of code: one that executes if the
condition is true, and one that executes 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
}
Example:
#include <stdio.h>

int main()
{
int number = -5; Output:
if (number > 0) { The number is negative.
printf("The number is positive.\n");
Here, since number is less than zero, the program will output "The
}
number is negative."
else
{
printf("The number is negative.\n");
}

return 0;
}
3. THE IF ELSE LADDER STATEMENT
When there are multiple conditions to check, the if else ladder is used. It
allows for multiple conditions to be checked one after another.

Syntax:
if (condition1) {
// code to be executed if condition1 is true
}
else if (condition2)
{
// code to be executed if condition2 is true
}
else
{
// code to be executed if all conditions are false
}
Example:
#include <stdio.h>

int main() {
int number = 0;

{
if (number > 0)
Output:
printf("The number is positive.\n");
}
else if (number < 0)
The number is zero.
{
In this case, since number is zero, the program will output "The number is zero."
printf("The number is negative.\n");
}
else
{
printf("The number is zero.\n");
}

return 0;
}
4. NESTED IF STATEMENTS
A nested if statement is when one if or else if statement is placed inside
another if or else if. It allows for more complex decision-making.

Syntax:
if (condition1)
{
// code to be executed if condition1 is true
if (condition2)
{
// code to be executed if condition2 is also true
}
}
Example:
#include <stdio.h>

int main() {
int number = 10;
Output:
if (number > 0) {
printf("The number is positive.\n");
The number is positive.
The number is even.
if (number % 2 == 0) {
Here, the program will first check if the number is positive. If true, it will then
printf("The number is even.\n");
check if the number is even. The output will be:
}
}

return 0;
}
Thank you!!

From: Intellelearn

You might also like