If Statement
An if statement, also known as conditional statement, is a method of executing a block of code based on a condition. The syntax of an if statement is the following:
javascript9123if(condition) {// code to execute}
It takes an expression and executes the code block if the expression is true
.
For example:
main.cs
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
using System;
namespace ConsoleApp
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Before Conditional");
if(10 > 9) {
Console.WriteLine("10 is greater than 9");
}
Console.WriteLine("After Conditional");
}
}
}
12345678910111213141516using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { Console.WriteLine("Before Conditional"); if(10 > 9) { Console.WriteLine("10 is greater than 9"); } Console.WriteLine("After Conditional"); } } }
In case the condition is false
the code block is ignored:
main.cs
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
using System;
namespace ConsoleApp
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Before Conditional");
if(10 > 10) {
Console.WriteLine("10 is greater than 9");
}
Console.WriteLine("After Conditional");
}
}
}
12345678910111213141516using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { Console.WriteLine("Before Conditional"); if(10 > 10) { Console.WriteLine("10 is greater than 9"); } Console.WriteLine("After Conditional"); } } }
Was alles duidelijk?
Bedankt voor je feedback!
Sectie 3. Hoofdstuk 5
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.