if else statement example in c
if else statement example in c
Simple if
Statement
Syntax:
if (test expression)
{
//statement is executed if statement is true
}
How
if statement works?
·
If
statement check the test expression inside
the parenthesis ()
.
· If the test
expression is evaluated to true, statements inside the body part of
if
are
executed.
· If the test
expression is evaluated to false, statements inside the body part of
if
are
not executed.
Example:
if statement
// Program to display a number if it is positive
#include <stdio.h>
#include<conio.h>
void main() {
int number;
printf(“Enter a number”);
scanf("%d", &number);
if (number > 0) {
printf(“number is positive”);
}
getch();
}
Output
Enter a number 2
Number is positive.
if...else Statement
The
if
statement has an optional else
block.
Syntax:
if (test expression) {
// statements is executed if the test expression is true
}
else {
// statements is executed if the test expression is false
}
How
if...else statement works?
If the test expression is evaluated
to true,
·
Statements
inside the body part of
if
are executed.
·
Statements
inside the body part of
else
are skipped from execution.
If the test expression is evaluated
to false,
·
statements
inside the body part of
else
are executed
·
Statements
inside the body part of
if
are skipped from execution.
Example:
if...else statement
// Program to Find out the number is even or odd.
#include <stdio.h>
#include<conio.h>
void main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
// True if the remainder is 0
if (number%2 == 0) {
printf("%d is an even.",number);
}
else {
printf("%d is an odd.",number);
}
getch();
}
Output
Enter an integer: 7
7 is an odd .
if...else Ladder
The
if...else
statement
executes two different codes depending on the condition is true
or false. Sometimes, a choice can be made from more than 2 possibilities.
The if...else ladder allows you to check between
multiple condition and execute different statements.
Syntax of if...else Ladder
if (test expression1) {
// statement(s)
}
else if(test expression2) {
// statement(s)
}
else if (test expression3) {
// statement(s)
}
else {
// statement(s)
}
Example
if...else Ladder
// Program to relate two numbers using ==, > or < symbol
#include <stdio.h>
#include<conio.h>
void main() {
int number1, number2;
printf("Enter two number: ");
scanf("%d %d", &number1, &number2);
//checks if the two numbers 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);
}
//checks if both test expressions are false
else {
printf("Result: %d < %d",number1, number2);
}
getch();
}
Output
Enter two integers: 15
25
Result: 15 < 25
Nested
if...else
It is
possible to include an
if...else
statement
inside the body part of another if...else
statement.
Example
Nested if...else
Find out
largest value from three numbers
If the body part of an
if...else
statement
has only one statement, you do not need to use brackets {}
.
Switch case
Switch statement tests the value of a variable and compares it
with different cases. Once the case is match, statements connected with that case is executed.Every case in a block of a switch has a different name or number which is referred to as an identifier. The value provided by the user is compared with each cases inside the switch block until the match is found.
If a case is not match, then the default statement is executed, and the control goes out of the switch.
Syntax
switch( expression )
{
case value-1:
Block-1;
Break;
case value-2:
Block-2;
Break;
case value-n:
Block-n;
Break;
default:
Block-1;
Break;
}
- The expression can be integer or a character.
- Switch case labels end with a
colon ( : ). Each of these cases is associated with a block.
- A block means multiple statements.
- Whenever the switch is run,
the value of test-expression is compared with all the cases which we have
defined inside the switch. Suppose the test expression contains value 'b'.
This value is compared with all the cases until case whose label 'b' is
found in the program. As soon as a case is found the block of statements connected with that particular case is executed.
- The break keyword in each case display the end of a particular case. If we do not use the break in every case then even though the specific case is executed, the switch will
continue to execute all the cases until the end is reached.
- The default case is an optional
one. Whenever the value of test-expression is not matched with any of the
cases, then the default will be executed.
Example
Following program
illustrates the use of switch:
#include <stdio.h>
int main() {
int num = 8;
switch (num) {
case 7:
printf("Value is 7");
break;
case 8:
printf("Value is 8");
break;
case 9:
printf("Value is 9");
break;
default:
printf("Out of range");
break;
}
return 0;
}
Output:
Value is 8
Limitations
1) logical operators can
not be used with switch statement.
2) switch case variables
can have only int and char data type. So float data type is not allowed