-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtutorial_decide_if_or_else.txt
More file actions
51 lines (38 loc) · 1.57 KB
/
Copy pathtutorial_decide_if_or_else.txt
File metadata and controls
51 lines (38 loc) · 1.57 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
///////////////////
Decide if or else
///////////////////
**if statement
if(expression)
{
statements
}
if the expression written within the brackets of if statement is true, then the statement written in the body of the if (enclosed by curly brackets) are executed
**if...else statement
if(expression)
{
statements 1
}
else
{
statements 2
}
if the expression is true, statements 1 are executed. Otherwise, statements 2 are executed.
**Nested if/else statements
We can also use if or else inside another if or else statements
**else if statement
**Switch...case
Normally, if we have to choose one case among many choices, nested if-else is used. But if the number of chices is large, switch...case is a better option as it makes code neat and easier.
switch(expression)
{
case constant1:
statements;
break;
case constant2:
statements;
break;
/*you can give any number of cases*/
default:
statements;
}
In switch...case, value of the expression enclosed in the brackets () following switch is checked. If the value of the expression matches the value of the constant in case, the statements corresponding with that case will be executed.
If the expression does not match any of the constant values, then the statements corresponding with default are executed.