Categories
Angular

ng if else condition in angular with example

In this post, we will learn about ng if else condition in angular with example

NgIf the directive will be used to display DOM based on a condition. It is very useful to show or hide a HTML div

Sample Code
<div ngIf="show">
    <p>This is can be show</p>
</div>
import { Component, OnInit } from '@angular/core';
import { StudentService } from '../../service/student.service';
import { Student } from '../../model/student.model';

@Component({

  selector: 'app-student-details',
  templateUrl: './student-details.component.html',
  styleUrls: ['./student-details.component.css']
})
export class StudentDetailsComponent implements OnInit {

  constructor() { }

  show: boolean;

  ngOnInit(): void {   
    this.show=true;
  }

}

In the above example, we declared a boolean variable called as show. We are changing that boolean value to true on ngOnInit method

In the html file we are using that show boolean variable to display the paragraph tag

if the show boolean is false then the html div will not display

ngIf else Example

We need to define the else block inside a ng-template block. below is the example for ng if else condition

<div ngIf="show; else notshowing">
    <p>This is can be show</p>
</div>

<ng-template #notshowing>
    <p>This will be shown on else statment</p>
</ng-template>
Conclusion

In the above post, we learned about ngIf else condition in angular with example

Related Articles

http post request from angular with example

Categories
java

do while in java with example

do while in java with example

In this tutorial, we will learn do while in java with example

do while is little bit different from other looping statements, because other looping statements while execute only when condition satisfy

But do while will execute without condition once, after that it will check condition to execute next statement

Syntax

do{  
//code
}while(condition);  

Example

public class DoWhileExample {
	  
	public static void main(String[] args) {
       int i = 1;
       do {
          System.out.println("Loop Count " + i);
          i++;
      } while (i <= 5);
   }
}

Output

Loop Count 1
Loop Count 2
Loop Count 3
Loop Count 4
Loop Count 5
Download

https://github.com/rkumar9090/BeginnersBug/blob/master/BegineersBug/src/com/geeks/example/DoWhileExample.java

Related Articles

https://beginnersbug.com/while-loop-using-boolean-datatype-in-java/

Categories
java

for loop in java

for loop used to iterate an array or used to repeat a set of code for predefined times

Syntax

for(;;){
    // your logic
}

Example

   for (int i = 0; i < 10; i++) {
             System.out.println("Loop count " + i);
         }

Ouput

Loop count 0
Loop count 1
Loop count 2
Loop count 3
Loop count 4
Loop count 5
Loop count 6
Loop count 7
Loop count 8
Loop count 9