-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConditional statement
More file actions
80 lines (61 loc) · 1.49 KB
/
Conditional statement
File metadata and controls
80 lines (61 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
Conditional Statement : A conditional statement is a control statement that executes different blocks of code depending on whether a condition is true or false.
A conditional statement is used to make decisions in a program based on a condition.
1] IF Condition:
#include <stdio.h>
int main() {
int age = 20;
// If statement
if (age >= 18) {
printf("Eligible for vote");
}
}
2] IF - ELSE Condition:
#include<stdio.h>
int main()
{
int a;
scanf("%d",&a);
if(a>=13)
{
printf("a is higher");
}
else
{
printf("a is lower");
}
return 0;
}
3] Nested IF-ELSE:
#include <stdio.h>
int main(){
int age = 11;
if (age >= 18) {
if (age >= 60)
printf("Eligible to vote (Senior Citizen)\n");
else
printf("Eligible for vote\n");
}
else {
printf("Not eligible to vote (Under 18)\n");
if (age >= 13)
printf("teenager\n");
else
printf("not a teenager\n");
}
return 0;
}
4] if-else-if Ladder: The if else if ladder statements are used when the user has to decide among multiple options.
#include <stdio.h>
int main() {
int i = 20;
// If else ladder with three conditions
if (i == 10)
printf("Not Eligible");
else if (i == 15)
printf("wait for three years");
else if (i == 20)
printf("You can vote");
else
printf("Not a valid age");
return 0;
}