If statement
The if
statement
if
statementAn 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:
In the above code, println()
will only execute when the condition is true. There are no parentheses needed for surrounding the condition, and the braces are always required.
The else
statement
else
statementAn else
statement is a programming conditional statement in which when if
evaluates to false then the code in else
block executes.
In this example, the code inside the else
block will execute because the condition in if
evaluates to false
.
The else if
statement
else if
statementThe 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.
Output
Nested if..else
statement
if..else
statementIt is always a good practice to nest if...else
statements which means you can use one if
, else
or else...if
statement inside another if
or else...if
statement.
Output
Using if..else
as expression
if..else
as expressionThe if..else
can also be used as an expression:
Output
Exercises
Write a V program to accept two integers and check whether they are equal or not.
Write a V program to check whether a given number is even or odd.
Write a V program to check whether a given number is positive or negative.
Write a V program to find whether a given year is a leap year or not.
Last updated