-
Notifications
You must be signed in to change notification settings - Fork 0
/
if_else_code-5.cpp
117 lines (108 loc) · 4.77 KB
/
if_else_code-5.cpp
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include<bits/stdc++.h>
using namespace std;
int main()
{
int age, poll;
// Prompt the user to enter their age
cout << "Enter your age: ";
cin >> age;
// Check if the age is within the valid range
if (age >= 18 && age <= 150) // Assuming no one lives above 150 years!
{
// If the age is valid, allow the user to vote
cout << "Congrats!!! You can vote." << endl;
cout << "\n";
cout << "Vote for your party from the menu:" << endl;
cout << "1. India\n";
cout << "2. NDA\n";
cout << "3. Others\n";
cin >> poll;
cout << "\n";
// Process the user's vote choice
switch (poll)
{
case 1:
cout << "You voted for India\n";
break;
case 2:
cout << "You voted for NDA\n";
break;
case 3:
cout << "You voted for Others\n";
break;
default:
cout << "Invalid Response\n";
}
}
else if (age < 18)
{
// If the age is less than 18, user is not eligible to vote
cout << "Sorry!!! You are not eligible.\n";
}
else
{
// If age is beyond the reasonable limit
cout << "Invalid Response.\n";
}
return 0;
}
/*
if Statement | else if Statement | else Statement
-------------------------------------------------------------------------------
1. Syntax | Syntax | Syntax
if (condition) | else if (condition) | else
{ | { | {
// Code | // Code | // Code
} | } | }
| |
2. Execution | Execution | Execution
- Checks the | - Checks the | - Executes when
condition and | condition and | all preceding
executes code | executes code | conditions are
inside the block | inside the block | false.
if condition is | if condition is |
true. | true and all |
- If condition is | preceding |
false, skips | conditions are |
the block. | false. |
| - Skips the block |
| if condition is |
| false and there |
| is no else if or |
| if after it. |
-------------------------------------------------------------------------------
3. Multiple | Multiple else if | Only one else
Conditions | Conditions | block
- Only one block | - Only one block | - Optional
can be executed. | is executed, | - Executes if
- Executes when | the first true | no other
condition is | condition is | if or else if
true. | encountered. | conditions are
- Skips all other | - Skips all other | true.
conditions after | else if blocks |
it. | after it. |
| |
4. Overlapping | No overlapping | No overlapping
Conditions | conditions allowed | conditions allowed
- Conditions in | - Each else if | - Only one else
if and else if | condition is | block will be
blocks are | evaluated | executed, even
independent. | independently. | if multiple
| - If a condition | conditions are
| is true, | true.
| other else if |
| conditions are |
| not evaluated. |
-------------------------------------------------------------------------------
5. Default | Default condition | Default condition
condition | |
- Optional | - Optional | - Optional
- Executes if no | - Executes if no | - Executes if no
other condition | preceding | other condition
is true. | condition is | is true.
- Written using | true. | - Written using
else statement. | - Written using | else statement.
| else if statement. |
-------------------------------------------------------------------------------
end
*/