C if ... else and nested if ... else statement
Normally a conditional statement is used in programming for decision making. In this chapter you will learn how to make decisions using if ... else and nested if ... else statement.C if ... else statement
If the condition is true then some code is false and for some other code execution, the C ... else statement is used.
if ... else syntax
if (testExpression) {
// True if this code will be executed.
}
else {
// If false this code will be executed.
}
If the test expression is true then the if statement will block the code block and avoid the else statement code block.
If the test expression is false then else the statement code block will be edited and if the statement will block the code block.
Example 1: C if ... else statement
#include <stdio.h>
#include <conio.h>
int main ()
{
int time = 16;
clrscr ();
if (time <12)
{
printf ("Good morning."); // The print will be less than 12 times.
}
else
{
printf ("Good after noon"); // Print time is more than 12 times.
}
getch ();
}
Output
Good after noon
Example 2: Si if ... else statement
// The number of integer inputs provided by the user is not even wired or program to check odd.
#include <stdio.h>
int main ()
{
int testNumber;
printf ("Enter a integer:");
scanf ("% d", & testNumber);
// True if zero (0) is true
if (testNumber% 2 == 0) {
printf ("% d is an even integer.", testNumber);
}
else {
printf ("% d is an integer integer.", testNumber);
}
return 0;
}
Output
Enter a integer: 9
9 is an odd integer
When the user enters 9, the value of the test expression (testNumber% 2 == 0) is false. So printf ("% d is an integer integer") in the else statement; The statement execution is done and if the statement in it is avoided.
Neestate if ... else statement
Conditions True / False Whatever we want to execute the code means that if we want to decide, if ... else statement is used. But sometimes decisions can depend on two or more conditions.
Nested if ... else statement is used to execute code by examining more than two expressions.
Neestate if ... else syntax
if (testExpression1)
{
// testExpression1 True if this code will be executed.
}
else if (testExpression2)
{
// testExpression1 false and testExpression2 True if this code block will be executed.
}
else if (testExpression3)
{
// testExpression1 and testExpression2 false and testExpression3 True if this code block will be executed.
}
.
.
else
{
// If all testExpression false, then this code block will be executed.
}
Example 1: C nested if ... else statement
#include <stdio.h>
#include <conio.h>
int main () {
int number = 0;
clrscr ();
printf ("enter a number:");
scanf ("% d", & number);
if (number == 10) {
printf ("number is equal to 10");
}
else if (number == 50) {
printf ("number is equal to 20");
}
else if (number == 100) {
printf ("number is equal to 30");
}
else {
printf ("number is not equal to 10, 20 or 30");
}
getch ();
}
Output
enter a number: 5
number is not equal to 10, 20 or 30
enter a number: 100
number is equal to 30
Example 2: Si nested if ... else statement
// =,> or <using the two integers between the two
#include <stdio.h>
int main ()
{
int testNumber1, testNumber2;
printf ("Enter two integers:");
scanf ("% d% d", & testNumber1, & testNumber2);
// Checks whether the two integers are equal.
if (testNumber1 == testNumber2)
{
printf ("Result:% d =% d", testNumber1, testNumber2);
}
// testNumber2 checks whether the testNumber1 is larger
else if (testNumber1> testNumber2)
{
printf ("Result:% d>% d", testNumber1, testNumber2);
}
// If both expressions are false.
else
{
printf ("Result:% d <% d", testNumber1, testNumber2);
}
return 0;
}
Output
Enter two integers: 15
225
Result: 15 <25
You can also use the Swiss statement to make decisions against many values.


0 Comments