Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre If-else Operator | Control Statements
C Basics

book
If-else Operator

if-else

The if-else statement is a staple in programming. It enables you to direct the flow of your program based on certain conditions.

Note

Just a heads-up! Code blocks { } were introduced in Section 1, Chapter 2.

The if-else structure is pretty straightforward:

python
if (condition)
{
// Execute this block of code if condition is true
}
else
{
// Execute this block of code if condition is false
}

Take, for instance, the temperature variable that receives data from a temperature sensor.

Suppose we want a program that alerts us when the temperature gets too high:

c

Main

copy
#include <stdio.h>

int main()
{
int temperature = 200; // in celsius

if (temperature > 80)
{
printf("Temperature is so high: %d degrees Celsius\n", temperature);
}

else
{
printf("Temperature is normal: %d degrees Celsius\n", temperature);
}

return 0;
}
123456789101112131415161718
#include <stdio.h> int main() { int temperature = 200; // in celsius if (temperature > 80) { printf("Temperature is so high: %d degrees Celsius\n", temperature); } else { printf("Temperature is normal: %d degrees Celsius\n", temperature); } return 0; }

You can incorporate multiple if statements in a program, especially when you need to evaluate various conditions.

Note

Conditions can also include logical operators.

Else If

The if-else statement can be further expanded with else-if:

c

Main

copy
#include <stdio.h>

int main()
{
int tempereture = 50; // in celsius

if (tempereture > 50) // condition 1
{
printf("Temperature is high: %d degrees Celsius\n", tempereture); // instruction_1
}

else if (tempereture > 100)// condition 2
{
printf("Temperature is so high: %d degrees Celsius\n", tempereture);// instruction_2
}

else if (tempereture > 150)// condition 3
{
printf("Temperature is critically high: %d degrees Celsius\n", tempereture);// instruction_3
}

else
{
printf("Temperature is normal: %d degrees Celsius\n", tempereture); // instruction_4
}

return 0;
}
12345678910111213141516171819202122232425262728
#include <stdio.h> int main() { int tempereture = 50; // in celsius if (tempereture > 50) // condition 1 { printf("Temperature is high: %d degrees Celsius\n", tempereture); // instruction_1 } else if (tempereture > 100)// condition 2 { printf("Temperature is so high: %d degrees Celsius\n", tempereture);// instruction_2 } else if (tempereture > 150)// condition 3 { printf("Temperature is critically high: %d degrees Celsius\n", tempereture);// instruction_3 } else { printf("Temperature is normal: %d degrees Celsius\n", tempereture); // instruction_4 } return 0; }

1. What is the purpose of an if-else statement in programming?

2. What is a basic structure of an if-else statement in C, including the syntax for code blocks.

question mark

What is the purpose of an if-else statement in programming?

Select the correct answer

question mark

What is a basic structure of an if-else statement in C, including the syntax for code blocks.

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 4. Chapitre 1

Demandez à l'IA

expand
ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

We use cookies to make your experience better!
some-alt