0% found this document useful (0 votes)
18 views19 pages

C If Else Statement

The document provides an overview of conditional statements in C programming, including if, if-else, if-else-if ladder, and nested if statements. It includes syntax examples and practical applications such as checking even/odd numbers, finding the largest of three numbers, and determining eligibility to vote. Additionally, it discusses logic for checking divisibility, identifying days of the week, and verifying if a character is an alphabet.

Uploaded by

killerboy0951
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views19 pages

C If Else Statement

The document provides an overview of conditional statements in C programming, including if, if-else, if-else-if ladder, and nested if statements. It includes syntax examples and practical applications such as checking even/odd numbers, finding the largest of three numbers, and determining eligibility to vote. Additionally, it discusses logic for checking divisibility, identifying days of the week, and verifying if a character is an alphabet.

Uploaded by

killerboy0951
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

C if else Statement

The if-else statement in C is used to perform the operations


based on some specific condition. The operations specified in
if block are executed if and only if the given condition is true.
There are the following variants of if statement in C language.
o If statement
o If-else statement
o If else-if ladder
o Nested if

If Statement
The if statement is used to check some given condition and
perform some operations depending upon the correctness of
that condition. It is mostly used in the scenario where we need
to perform the different operations for the different conditions.
The syntax of the if statement is given below.
1. if(expression){
2. //code to be executed
3. }
Flowchart of if statement in C
Let's see a simple example of C language if statement.
1. #include<stdio.h>
2. int main(){
3. int number=0;
4. printf("Enter a number:");
5. scanf("%d",&number);
6. if(number%2==0){
7. printf("%d is even number",number);
8. }
9. return 0;
10. }
Output
Enter a number:4
4 is even number
enter a number:5

Program to find the largest number of the three.


1. #include <stdio.h>
2. int main()
3. {
4. int a, b, c;
5. printf("Enter three numbers?");
6. scanf("%d %d %d",&a,&b,&c);
7. if(a>b && a>c)
8. {
9. printf("%d is largest",a);
10. }
11. if(b>a && b > c)
12. {
13. printf("%d is largest",b);
14. }
15. if(c>a && c>b)
16. {
17. printf("%d is largest",c);
18. }
19. if(a == b && a == c)
20. {
21. printf("All are equal");
22. }
23. }
Output
Enter three numbers?
12 23 34
34 is largest
If-else Statement
The if-else statement is used to perform two operations for a
single condition. The if-else statement is an extension to the if
statement using which, we can perform two different
operations, i.e., one is for the correctness of that condition, and
the other is for the incorrectness of the condition. Here, we must
notice that if and else block cannot be executed
simiulteneously. Using if-else statement is always preferable
since it always invokes an otherwise case with every if
condition. The syntax of the if-else statement is given below.
1. if(expression){
2. //code to be executed if condition is true
3. }else{
4. //code to be executed if condition is false
5. }
Flowchart of the if-else statement in C
Let's see the simple example to check whether a number is even
or odd using if-else statement in C language.
1. #include<stdio.h>
2. int main(){
3. int number=0;
4. printf("enter a number:");
5. scanf("%d",&number);
6. if(number%2==0){
7. printf("%d is even number",number);
8. }
9. else{
10. printf("%d is odd number",number);
11. }
12. return 0;
13. }
Output
enter a number:4
4 is even number
enter a number:5
5 is odd number

Program to check whether a person is eligible to


vote or not.
1. #include <stdio.h>
2. int main()
3. {
4. int age;
5. printf("Enter your age?");
6. scanf("%d",&age);
7. if(age>=18)
8. {
9. printf("You are eligible to vote...");
10. }
11. else
12. {
13. printf("Sorry ... you can't vote");
14. }
15. }
Output
Enter your age?18
You are eligible to vote...
Enter your age?13
Sorry ... you can't vote
If else-if ladder Statement
The if-else-if ladder statement is an extension to the if-else
statement. It is used in the scenario where there are multiple
cases to be performed for different conditions. In if-else-if
ladder statement, if a condition is true then the statements
defined in the if block will be executed, otherwise if some other
condition is true then the statements defined in the else-if block
will be executed, at the last if none of the condition is true then
the statements defined in the else block will be executed. There
are multiple else-if blocks possible. It is similar to the switch
case statement where the default is executed instead of else
block if none of the cases is matched.
1. if(condition1){
2. //code to be executed if condition1 is true
3. }else if(condition2){
4. //code to be executed if condition2 is true
5. }
6. else if(condition3){
7. //code to be executed if condition3 is true
8. }
9. ...
10. else{
11. //code to be executed if all the conditions are false
12. }
Flowchart of else-if ladder statement in C
The example of an if-else-if statement in C language is given
below.
1. #include<stdio.h>
2. int main(){
3. int number=0;
4. printf("enter a number:");
5. scanf("%d",&number);
6. if(number==10){
7. printf("number is equals to 10");
8. }
9. else if(number==50){
10. printf("number is equal to 50");
11. }
12. else if(number==100){
13. printf("number is equal to 100");
14. }
15. else{
16. printf("number is not equal to 10, 50 or 100");
17. }
18. return 0;
19. }
Output
enter a number:4
number is not equal to 10, 50 or 100
enter a number:50
number is equal to 50

Program to calculate the grade of the student


according to the specified marks.
1. #include <stdio.h>
2. int main()
3. {
4. int marks;
5. printf("Enter your marks?");
6. scanf("%d",&marks);
7. if(marks > 85 && marks <= 100)
8. {
9. printf("Congrats ! you scored grade A ...");
10. }
11. else if (marks > 60 && marks <= 85)
12. {
13. printf("You scored grade B + ...");
14. }
15. else if (marks > 40 && marks <= 60)
16. {
17. printf("You scored grade B ...");
18. }
19. else if (marks > 30 && marks <= 40)
20. {
21. printf("You scored grade C ...");
22. }
23. else
24. {
25. printf("Sorry you are fail ...");
26. }
27. }
Output
Enter your marks?10
Sorry you are fail ...
Enter your marks?40
You scored grade C ...
Enter your marks?90
Congrats ! you scored grade A ...

Logic to check divisibility of a


number
A number is exactly divisible by some other number if it gives 0 as
remainder. To check if a number is exactly divisible by some number we
need to test if it leaves 0 as remainder or not.

C supports a modulo operator %, that evaluates remainder on division of two


operands. You can use this to check if a number is exactly divisible by
some number or not. For example – if(8 % 2), if the given expression
evaluates 0, then 8 is exactly divisible by 2.
Step by step descriptive logic to check whether a number is divisible by 5
and 11 or not.

1. Input a number from user. Store it in some variable say num.


2. To check divisibility with 5, check if(num % 5 == 0) then num is divisible
by 5.
3. To check divisibility with 11, check if(num % 11 == 0) then num is
divisible by 11.
4. Now combine the above two conditions using logical AND
operator &&. To check divisibility with 5 and 11 both, check if((num % 5
== 0) && (num % 11 == 0)), then number is divisible by both 5 and 11.

Let us implement the logic.

Program to check divisibility of a


number
/**
* C program to check divisibility of any number
*/

#include <stdio.h>

int main()
{
int num;

/* Input number from user */


printf("Enter any number: ");
scanf("%d", &num);

/*
* If num modulo division 5 is 0
* and num modulo division 11 is 0 then
* the number is divisible by 5 and 11 both
*/
if((num % 5 == 0) && (num % 11 == 0))
{
printf("Number is divisible by 5 and 11");
}
else
{
printf("Number is not divisible by 5 and
11");
}

return 0;
}

Logic to find
day of week
Step by step descriptive logic to print day name of
week.
1. Input week day number from user. Store it in
some variable say week.
2. Print Monday if(week == 1). I have assumed
Monday as first day of week.
3. Similarly, check condition for all 7 days and
print the corresponding day name.

Program to
print day
name of
week
/**
* C program to print day name of week
*/

#include <stdio.h>

int main()
{
int week;

/* Input week number from user */


printf("Enter week number (1-7): ");
scanf("%d", &week);
if(week == 1)
{
printf("Monday");
}
else if(week == 2)
{
printf("Tuesday");
}
else if(week == 3)
{
printf("Wednesday");
}
else if(week == 4)
{
printf("Thursday");
}
else if(week == 5)
{
printf("Friday");
}
else if(week == 6)
{
printf("Saturday");
}
else if(week == 7)
{
printf("Sunday");
}
else
{
printf("Invalid Input! Please enter week
number between 1-7.");
}

return 0;
}
Copy
The above approach is easiest to code and
understand. However, use of if…else is not
recommended when checking condition with fixed
constants.

Logic to
check
alphabets
In C every printable and non-printable symbol is
treated as a character and has an ASCII value.
ASCII value is unique integer value for every
character. It is used to represent a character in
memory. In memory every character is stored as
an integer.
One of the beginner way to tackle the problem is
check input for every alphabet characters.
However, I will not explain this method neither I
recommend you to try.
An input character is alphabet if it is in between a-
z or A-Z.
Note: a and A both are different and have different
ASCII values.
Step by step descriptive logic to check alphabets.
1. Input a character from user. Store it in some
variable say ch.
2. Check if((ch >= 'a') && (ch <= 'z')) or if((ch
>= 'A') && (ch <= 'Z')). Then it is alphabet
otherwise not.
Let us implement above logic through C program.

Program to
check
alphabets
/**
* C program to check whether a character is
alphabet or not
*/

#include <stdio.h>

int main()
{
char ch;

/* Input a character from user */


printf("Enter any character: ");
scanf("%c", &ch);

if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch
<= 'Z'))
{
printf("Character is an ALPHABET.");
}
else
{
printf("Character is NOT ALPHABET.");
}

return 0;
}
Copy
Note: You can also use ASCII values to check
alphabets. ASCII value of a=97, z=122,
A=65 and Z=90.
Program to
check
alphabets
using ASCII
value
/**
* C program to check whether a character is
alphabet or not
*/

#include <stdio.h>

int main()
{
char ch;

/* Input a character from user */


printf("Enter any character: ");
scanf("%c", &ch);
if((ch >= 97 && ch <= 122) || (ch >= 65 && ch
<= 90))
{
printf("Character is an ALPHABET.");
}
else
{
printf("Character is NOT ALPHABET.");
}

return 0;
}
Copy
Enhance your skills by learning other approach to
solve the given program.

You might also like