Dart Branching (if else)

Add Your Heading Text Here

Dart branching or decision making is used whenever we have to use the conditional test.

Dart supports branching using,

  • if
  • if else
  • if else ladder (Nested if else)

If

If is used whenever we need to perform tasks or operations based on decision making.

For ex :

if the condition satisfies then and then you can perform that particular task.

Syntax :

if(conditional test){

// statements

}

For ex :

void main() {
   var a,b;
   a=10;
   b=5;

  if(a>b){
  print("a is greater than b");
  }
}

In above program, print statement will be executed only when the condition a>b satisfies.

if and else

if and else is used whenever there is conditional possibilities and you have to behave accordingly.

Syntax :

if(conditional test)
{

// statements

}
else
{

// statements

}

For ex :

void main(){
  	var a=10, b=5;
	if(a>b){
		print("a is greater.");
	}else{
		print("b is greater.");
	}	
}

if else ladder (Nested if else).

It is an extended version of if else branching.

It is used to test multiple conditions and perform the satisfied operation.

For ex :

if(conditional test)
{

// statements

}
else if(conditional test)
{

// statements

}
else
{

// statements

}

For ex :

void main(){
	if(a>b){
		print("a is greater");
	}else if(b>c){
		print("b is greater");
	}else{
		print("c is greater);
        }
}

It can have multiple if-else conditions based on your logic.

In above example, If first if condition satisfies then it goes inside that if branch otherwise moves to the second if branch. If the second if condition gets satisfied then will go inside second if brach and performs the operation otherwise moves to the else condition.

In nested if-else branching conditional check starts with the first if-else condition checking to the last if-else. The flow of execution moves to any if branch if particular if condition gets satisfied. Otherwise, the flow of execution moves to the else branch if there is any.

I suggest you to practice if-else branching as much possible because it’s a fundamental concept to achieve better programming skills.

Feel free to ask your questions in the comment section. Keep reading and commenting your suggestions to make toastguyz a better site to learn different programming languages.

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
riekus
riekus
3 years ago

good content, thanks

Show Buttons
Hide Buttons