Verschachtelte if-Anweisungen in Java
Nested if Statement is one of the decisions making statements in Java that flows according to certain conditions. The branching of these conditions is a result of the program’s state change. That is, there will be an if-else condition inside another if-else. If, if-else, if-else-if, jump, switch-case, etc., are some of the other decision making statements in Java. Now, let us see the Nested-if Statement in detail.
ADVERTISEMENT Popular Course in this category JAVA MASTERY - Specialization | 78 Course Series | 15 Mock TestsSyntax of Nested if Statement
Following is the syntax of Nested if Statement in Java.
If (cond1) { // Executes when the cond1 is satisfied If (cond2) { // Executes when the cond2 is satisfied } }
Here, Cond1 is Condition 1, and Cond2 is Condition 2.
Example:
If (A1= =A2) { Print A1 is equal to A2 If (A1= =A3) { Print A1, A2 and A3 are equal. } }
Flowchart
The following figure depicts the flow chart of the Nested-if condition.
Working of Nested if Statements in Java
Nested-If works similar to the normal If-else condition. The only difference is that there will be an if condition inside another if condition. The working will be as follows.
- If Condition 1 is True, then go to if condition 2. If condition 2 is satisfied, its body will execute; otherwise, else part will execute.
- If Condition 1 is False, then the body of the else part will be executed.
- Once the condition check is finished, exit the loop.
- Continue the execution of statements after the loop
The count of the if-else condition varies depending on the user’s requirement.
Examples of Nested if Statements
In order to understand Nested-if in detail, let us see the examples using Java.
Example #1
A simple java program to implement Nested-if condition with only if conditions.
//Nested-if Java program with if conditions only public class NestedIfExample { public static void main(String args[]) { //declare 2 variables and store some values in it int num1 = 23; int num2 = 45; //if the number 1 is 23 if( num1 == 23 ) { //if number is 45 if( num2 == 45 ) { System.out.print("Number 1 is :"+ num1 +" and Number 2 is :"+ num2); } // end of if condition 2 } //end of if condition 1 } //end of main method } //end of class
Output:
In this program, two variables- num1 and num2 are declared that stores two numbers, 23 and 45, respectively. In the if condition, num1 is checked whether it is 23. As it is true, then nested if gets executed. That is, another if condition, whether number 2 is 45, is also checked. As it is also true, a line is printed, displaying “Number 1 is 23, and Number 2 is 45”.
Example #2
A simple java program to implement Nested-if condition with both if and else conditions.
//Nested-if Java program with both if and else conditions public class NestedIfExample { public static void main(String args[]) { //declare 2 variables and store some values in it int num1 = 23; int num2 = 48; //if the number 1 is 23 if( num1 == 23 ) { //if number is 45 if( num2 == 45 ) { System.out.print("Number 1 is :"+ num1 +" and Number 2 is :"+ num2); } // end of if condition 2 else { System.out.print("Number 2 is not 45"); }//end of else condition 2 } //end of if condition 1 } //end of main method } //end of class
Output:
In this program, two variables- num1 and num2 are declared that stores two numbers, 23 and 48, respectively. In the if condition, num1 is checked whether it is 23. As it is true, then nested if gets executed. That is, another if condition, whether number 2 is 45, is also checked. As it is not true, a line gets printed displaying “Number 2 is not 45”.
Example #3
A simple java program to implement a Nested-if condition that takes input from the user.
//Nested-if Java program that takes input from user and checks the condition import java.util.Scanner; public class NestedIfExample { public static void main(String args[]) { //create object of scanner Scanner <u>sc</u>= new Scanner(System.in); System.out.print("Enter the number to be checked: "); int num1 = sc.nextInt(); //if the number 1 is greater than or equal to 23 if( num1 >= 23 ) { System.out.print("Number 1 is :"+ num1 +" and it is greater than 23."); //if number is 45 if( num1 >= 45 ) { System.out.print("Oh!! it is greater than 45 also"); } // end of if condition 2 else { System.out.print(" But, the number "+num1+" is less than 45"); }//end of else condition 2 } //end of if condition 1 else { System.out.print("The number "+num1+" is less than 23"); }//end of else condition 2 } //end of main method } //end of class
Output:
In this program, the num1 variable is declared. Then, the user is asked to input num1. Here, 33 is given as input, and In the if condition, num1 is checked whether it is greater than or equal to 23. As it is true, then nested if it gets executed. That is, another if condition, whether number 2 is greater than or equal to 45, is also checked. As it is not true, a line is printed, displaying, “Number 1 is 33, and it is greater than 23. But the number 33 is less than 45”.
Suppose we have given input as 20. What will be the output?? Let us check how the flow will be in that case.
In this case, as the first condition itself is not satisfied, the else part gets executed. That is, a line will be printed as” The number 20 is less than 23”.
Conclusion
Nested if Statement is a decision-making statement in Java containing certain branches with an if condition inside another if condition. Syntax, working, and examples of Nested-if is discussed in this document.
Das obige ist der detaillierte Inhalt vonVerschachtelte if-Anweisungen in Java. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Heiße KI -Werkzeuge

Undresser.AI Undress
KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover
Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool
Ausziehbilder kostenlos

Clothoff.io
KI-Kleiderentferner

Video Face Swap
Tauschen Sie Gesichter in jedem Video mühelos mit unserem völlig kostenlosen KI-Gesichtstausch-Tool aus!

Heißer Artikel

Heiße Werkzeuge

Notepad++7.3.1
Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version
Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1
Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6
Visuelle Webentwicklungstools

SublimeText3 Mac-Version
Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

Heiße Themen











PHP und Python haben jeweils ihre eigenen Vorteile, und die Wahl sollte auf Projektanforderungen beruhen. 1.PHP eignet sich für die Webentwicklung mit einfacher Syntax und hoher Ausführungseffizienz. 2. Python eignet sich für Datenwissenschaft und maschinelles Lernen mit präziser Syntax und reichhaltigen Bibliotheken.

PHP ist eine Skriptsprache, die auf der Serverseite weit verbreitet ist und insbesondere für die Webentwicklung geeignet ist. 1.PHP kann HTML einbetten, HTTP -Anforderungen und Antworten verarbeiten und eine Vielzahl von Datenbanken unterstützt. 2.PHP wird verwendet, um dynamische Webinhalte, Prozessformdaten, Zugriffsdatenbanken usw. mit starker Community -Unterstützung und Open -Source -Ressourcen zu generieren. 3. PHP ist eine interpretierte Sprache, und der Ausführungsprozess umfasst lexikalische Analyse, grammatikalische Analyse, Zusammenstellung und Ausführung. 4.PHP kann mit MySQL für erweiterte Anwendungen wie Benutzerregistrierungssysteme kombiniert werden. 5. Beim Debuggen von PHP können Sie Funktionen wie error_reporting () und var_dump () verwenden. 6. Optimieren Sie den PHP-Code, um Caching-Mechanismen zu verwenden, Datenbankabfragen zu optimieren und integrierte Funktionen zu verwenden. 7

Java 8 führt die Stream -API ein und bietet eine leistungsstarke und ausdrucksstarke Möglichkeit, Datensammlungen zu verarbeiten. Eine häufige Frage bei der Verwendung von Stream lautet jedoch: Wie kann man von einem Foreach -Betrieb brechen oder zurückkehren? Herkömmliche Schleifen ermöglichen eine frühzeitige Unterbrechung oder Rückkehr, aber die Stream's foreach -Methode unterstützt diese Methode nicht direkt. In diesem Artikel werden die Gründe erläutert und alternative Methoden zur Implementierung vorzeitiger Beendigung in Strahlverarbeitungssystemen erforscht. Weitere Lektüre: Java Stream API -Verbesserungen Stream foreach verstehen Die Foreach -Methode ist ein Terminalbetrieb, der einen Vorgang für jedes Element im Stream ausführt. Seine Designabsicht ist

PHP eignet sich für die Webentwicklung, insbesondere für die schnelle Entwicklung und Verarbeitung dynamischer Inhalte, ist jedoch nicht gut in Anwendungen auf Datenwissenschaft und Unternehmensebene. Im Vergleich zu Python hat PHP mehr Vorteile in der Webentwicklung, ist aber nicht so gut wie Python im Bereich der Datenwissenschaft. Im Vergleich zu Java wird PHP in Anwendungen auf Unternehmensebene schlechter, ist jedoch flexibler in der Webentwicklung. Im Vergleich zu JavaScript ist PHP in der Back-End-Entwicklung präziser, ist jedoch in der Front-End-Entwicklung nicht so gut wie JavaScript.

PHP und Python haben jeweils ihre eigenen Vorteile und eignen sich für verschiedene Szenarien. 1.PHP ist für die Webentwicklung geeignet und bietet integrierte Webserver und reichhaltige Funktionsbibliotheken. 2. Python eignet sich für Datenwissenschaft und maschinelles Lernen mit prägnanter Syntax und einer leistungsstarken Standardbibliothek. Bei der Auswahl sollte anhand der Projektanforderungen festgelegt werden.

PhPhas significantantyPactedWebDevelopmentAndendendsbeyondit.1) iTpowersMAjorPlatforms-LikewordpressandExcelsInDatabaseInteractions.2) php'SadaptabilityAllowStoscaleForLargeApplicationsfraMe-Linien-Linien-Linien-Linienkripte

Die Gründe, warum PHP für viele Websites der bevorzugte Technologie -Stack ist, umfassen die Benutzerfreundlichkeit, die starke Unterstützung der Community und die weit verbreitete Verwendung. 1) Einfach zu erlernen und zu bedienen, geeignet für Anfänger. 2) eine riesige Entwicklergemeinschaft und eine reichhaltige Ressourcen haben. 3) in WordPress, Drupal und anderen Plattformen häufig verwendet. 4) Integrieren Sie eng in Webserver, um die Entwicklung der Entwicklung zu vereinfachen.

PHP eignet sich für Webentwicklungs- und Content -Management -Systeme, und Python eignet sich für Datenwissenschafts-, maschinelles Lernen- und Automatisierungsskripte. 1.PHP hat eine gute Leistung beim Erstellen von schnellen und skalierbaren Websites und Anwendungen und wird üblicherweise in CMS wie WordPress verwendet. 2. Python hat sich in den Bereichen Datenwissenschaft und maschinelles Lernen mit reichen Bibliotheken wie Numpy und TensorFlow übertrifft.
