if else statement example in c
if else statement example in cSimple if Statement
if (test expression)
{
//statement is executed if statement is true
}
How if statement works?If statement check the test expression inside the parenthesis ().if are executed.if are not executed.Example: if statement
// Program to display a number if it is positive
#include <stdio.h>
#include<conio.h>
{
int number;
scanf("%d", &number);
printf(“number is positive”);
}
getch();
}
Enter a number 2
Number is positive.
if...else Statement
if statement has an optional else block.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 are executed.else are skipped from execution.else are executedif 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
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.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 < 25Nested if...else
if...else statement inside the body part of another if...else statement.Example Nested if...else
if...else statement has only one statement, you do not need to use brackets {}.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.
#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