You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
An `if` statement is a programming conditional statement that, if proved true, performs a function or displays information. Below is a general example of an if statement in V:
5
+
An `if` statement is a programming conditional statement that, if proved true, executes the code given in the block. Below is a general example of an if statement in V:
6
6
7
7
```go
8
8
john_height:=100
@@ -15,6 +15,28 @@ if john_height < maria_height {
15
15
}
16
16
```
17
17
18
+
In the above code, `fn println()` will only execute when the condition is true else no statement would be printed.
19
+
20
+
## `else` statement
21
+
22
+
An `else` statement is a programming conditional statement in which when `if` evaluates to false then the code in else block executes.
23
+
24
+
```go
25
+
joey_age:=12
26
+
kevin_age:=15
27
+
28
+
if joey_age > kevin_age {
29
+
println("Joey is older")
30
+
} else {
31
+
println("Kevin is older")
32
+
}
33
+
```
34
+
35
+
In this example the block in else will execute because the condition in `if` evaluates to false.
36
+
37
+
38
+
## `else if` statement
39
+
18
40
The `if...else` statement executes two different codes depending upon whether the test expression is `true` or `false`. Sometimes, a choice has to be made from more than 2 possibilities. The `if...else if...else` ladder allows you to check between multiple test expressions and execute different statements.
19
41
20
42
```go
@@ -38,13 +60,15 @@ ashia_age := 38
38
60
39
61
if tom_age < ashia_age {
40
62
if tom_age < 18 {
41
-
println('tom_age < 18 and younger than Ashia.')
42
-
} else {
43
-
println('tom_age > 18 and older than Ashia.')
44
-
}
45
-
} else {
46
-
println('$tom_age == $ashia_age')
47
-
println('Tom and Ashia have the same age.')
63
+
println('tom_age < 18 and younger than Ashia.')
64
+
} else {
65
+
println('tom_age >= 18 and younger than Ashia.')
66
+
}
67
+
}
68
+
elseif tom_age > ashia_age {
69
+
println('$tom_age > $ashia_age')
70
+
} else {
71
+
println('$tom_age == $ashia_age')
48
72
}
49
73
```
50
74
@@ -63,7 +87,11 @@ s := if tom_age < ashia_age {
63
87
}
64
88
65
89
print(s)
66
-
>> Tom is the youngest
90
+
```
91
+
92
+
Output
93
+
```bash
94
+
Tom is the youngest
67
95
```
68
96
69
97
## Exercises
@@ -72,73 +100,3 @@ print(s)
72
100
2. Write a V program to check whether a given number is even or odd.
73
101
3. Write a V program to check whether a given number is positive or negative.
74
102
4. Write a V program to find whether a given year is a leap year or not.
75
-
76
-
## For
77
-
78
-
`for` loops offer a quick and easy way to do something repeatedly. They're handy, if you want to run the same code over and over again, each time with a different value. You can think of a loop as a computerized version of the game where you tell someone to take X steps in one direction then Y steps in another; for example, the idea "Go five steps to the east" could be expressed this way as a loop:
79
-
80
-
```go
81
-
fori:=0; i < 5; i++ {
82
-
println('Walking one step');
83
-
}
84
-
```
85
-
86
-
V has the `for` looping construct and the loop can be written in different ways:
87
-
88
-
1.`in` operator
89
-
90
-
```go
91
-
ages:= [18, 25, 32, 43, 50]
92
-
93
-
for age in ages {
94
-
println(age)
95
-
}
96
-
```
97
-
98
-
Note, that the value is read-only.
99
-
100
-
2.`while` loop form
101
-
102
-
A `while` loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. There are no parentheses surrounding the condition, and the braces are always required.
103
-
104
-
```go
105
-
mut factorial:=1
106
-
mut counter:=1
107
-
108
-
for {
109
-
counter++
110
-
factorial=factorial*counter
111
-
112
-
if counter > 5 {
113
-
print(factorial)
114
-
break
115
-
}
116
-
}
117
-
118
-
println(counter)
119
-
>> 120
120
-
```
121
-
122
-
3.`for` with the traditional C style
123
-
124
-
```go
125
-
mut factorial:=1
126
-
mut counter:=1
127
-
128
-
fori:=0; i < 5; i++ {
129
-
factorial=factorial*counter
130
-
if i == 6 {
131
-
print(factorial)
132
-
continue
133
-
}
134
-
println(i)
135
-
}
136
-
```
137
-
138
-
## Exercises
139
-
140
-
1. Write a V program to display the first 10 natural numbers.
141
-
2. Write a V program to find the sum of first 10 natural numbers.
142
-
3. Write a V program to display n terms of natural number and their sum.
143
-
4. Write a V program to read 10 numbers from keyboard and find their sum and average.
144
-
5. Write a V program to display the cube of the number upto given an integer.
Copy file name to clipboardExpand all lines: examples/functions/functions.md
+73Lines changed: 73 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,6 +4,8 @@ A function is a block of organized, reusable code that is used to perform a sing
4
4
5
5
Ideally you should consider using the [single responsibility principle](https://en.wikipedia.org/wiki/Single_responsibility_principle) (SOLID) which states that every module or function should have responsibility over a single part of the functionality provided by the software to keep your code maintainable.
V supports single line comments `//` and multi-line comments `/* */`. They should be used for documenting the code for letting the other users know how the code works. It can also be used for temporarily commenting the code which has to be used later on.
0 commit comments