if (<condition>) {
<code>
} else if (<condition>) {
<code>
} else {
<code>
}
/* example */
int money = 50;
if (money < 15) {
go_home();
} else if (money >= 600) {
buy_all();
} else {
buy_tickets(money / 15);
}
/* You can chain together as many else ifs as you want. But if there are
too many it will make the code hard to understand. In that case I would
recommend trying other solutions. */
//INCLUDING BUILT-IN LIBRARIES...
#include <stdio.h>
#include <stdlib.h>
//PRE-DEFINE CONSTANT VALUES...
#define MAXNUM -12 //defining an integer
#define PI 3.1415 //defining a float
#define END "
Program has ended!!
" //defining a string
//PRE-DEFINING CONSTANT OPERATIONS...
#define ADD(a, b, c) (a + b + c) //Operation that will add its 3 parameters
int main(){
//using other definitions to check if the current device is Windows or UNIX
#ifdef _WIN32
printf("
Windows Operating System Detected
");
#elif linux
printf("
UNIX Operating System Detected
");
#else
printf("
Operating System could NOT be identified!
");
#endif
printf("
Using pre-defined values and operations: ");
printf("
• MAXNUM: %d",MAXNUM); //using pre-defined integer
printf("
• PI: %f",PI); //using pre-defined float
printf("
• ADD(): %.2f",ADD(2,5,99.5)); //using pre-defined function
printf(END); //using pre-defined string
return 0;
}
if(raining == 0 || (windSpeed > 15 && temperature < 10))// ** missing if statement **
{
printf("Stay indoors.
");
}
else
{
printf("You can go outside.
");
}
return 0;
}
if ( TRUE ) {
/* Execute these statements if TRUE */
}
else {
/* Execute these statements if FALSE */
}
if ( TRUE ) {
/* Execute these statements if TRUE */
}
else {
/* Execute these statements if FALSE */
}
if ( condition to be checked) {
Statements-if-condition-true ;
}
else{
statements-if-condition-false ;
}
if (test expression) {
// run code if test expression is true
}
else {
// run code if test expression is false
}
// C program to illustrate nested-if statement
#include <stdio.h>
int main() {
int i = 20;
if (i == 10)
printf("i is 10");
else if (i == 15)
printf("i is 15");
else if (i == 20)
printf("i is 20");
else
printf("i is not present");
}
if (test expression)
{
// code
}