In this video, we discuss the basics of IF and IF-ELSE statements. Comparisons of numbers and variables are covered.
C if statement
if (testExpression) { // statements }
The if
statement evaluates the test expression inside parenthesis.
If test expression is evaluated to true (nonzero), statements inside the body of if
is executed.
If test expression is evaluated to false (0), statements inside the body of if
is skipped.
To learn more on test expression (when test expression is evaluated to nonzero (true) and 0 (false)), check out relational and logical operators.
Flowchart of if statement
Example #1: C if statement
// Program to display a number if user enters negative number
// If user enters positive number, that number won't be displayed
#include <stdio.h>
int main()
{
int number;
printf("Enter an integer: ");
scanf("%d", &number);
// Test expression is true if number is less than 0
if (number < 0)
{
printf("You entered %d.\n", number);
}
printf("The if statement is easy.");
return 0;
}
Output 1
Enter an integer: -2 You entered -2. The if statement is easy.
When user enters -2, the test expression (number < 0)
becomes true. Hence, You entered -2 is displayed on the screen.
Output 2
Enter an integer: 5 The if statement in C programming is easy.
When user enters 5, the test expression (number < 0)
becomes false and the statement inside the body of if
is skipped.
C if…else statement
The if...else
statement executes some code if the test expression is true (nonzero) and some other code if the test expression is false (0).
Syntax of if…else
if (testExpression) { // codes inside the body of if } else { // codes inside the body of else }
If test expression is true, code inside the body of if
statement is executed; and code inside the body of else
statement is skipped.
If test expression is false, code inside the body of else
statement is executed; and code inside the body of if
statement is skipped.
Flowchart of if…else statement
Example #2: C if…else statement
// Program to check whether an integer entered by the user is odd or even
#include <stdio.h>
int main()
{
int number;
printf("Enter an integer: ");
scanf("%d",&number);
// True if remainder is 0
if( number%2 == 0 )
printf("%d is an even integer.",number);
else
printf("%d is an odd integer.",number);
return 0;
}
Output
Enter an integer: 7 7 is an odd integer.
When user enters 7, the test expression ( number%2 == 0 )
is evaluated to false. Hence, the statement inside the body of else
statement printf("%d is an odd integer");
is executed and the statement inside the body of if
is skipped.
Nested if…else statement (if…elseif….else Statement)
The if...else
statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities.
The nested if…else statement allows you to check for multiple test expressions and execute different codes for more than two conditions.
Syntax of nested if…else statement.
if (testExpression1) { // statements to be executed if testExpression1 is true } else if(testExpression2) { // statements to be executed if testExpression1 is false and testExpression2 is true } else if (testExpression 3) { // statements to be executed if testExpression1 and testExpression2 is false and testExpression3 is true } . . else { // statements to be executed if all test expressions are false }
Example #3: C nested if…else statement
// Program to relate two integers using =, > or <
#include <stdio.h>
int main()
{
int number1, number2;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
//checks if two integers are equal.
if(number1 == number2)
{
printf("Result: %d = %d",number1,number2);
}
//checks if number1 is greater than number2.
else if (number1 > number2)
{
printf("Result: %d > %d", number1, number2);
}
// if both test expression is false
else
{
printf("Result: %d < %d",number1, number2);
}
return 0;
}
Output
Enter two integers: 12 23 Result: 12 < 23
You can also use switch statement to make decision between multiple possibilites.
Hey! Great tutorial, but I'm just curious, why is this
"Please input a single digit value: h
Please input another single digit value:
0 is less than 32767
Program ended with exit code: 0"
the case, instead of just the error message in the else statement?
you mentioned all possible conditions so no need for "else"
or you can use "else" instead of "else if( num_1 == num_2 )"
your tutorial in general is good but try to make your voice loader
does it have to have the "else"?
When I tried it I forgot to put the "else" before the "if" and it worked just fine…
Nice tutorial…you are doing a great job. Cheers!!!!!
How nice of your dad to get you a watch :)
good job man, thanks for the entire tutorial, not bad of a language kind of like java