Skip to main content

Conditional Structure (if,if else,if else if ,nested if)

 Conditional Structure

There are kinds of conditional structure in java are as follows: 

➢ If 

➢ If else 

➢ If else if 

➢ Nested if 

If Statement: 

It is a programming conditional structure that if the statement of if is true, a block of code is executed. 

Syntax: 

if(condition) 

//code 

Example to understand the if statement in java 

class if { 

public static void main(String[] args) { 

int marks=92; 

if(marks>=80) 

System.out.println("Excellent"); 

Output: 

Excellent 

If -else: 

It is a programming conditional structure that if the statement of if is true, a block of code is executed otherwise the else statement is executed. 

Syntax : 

if (condition) 

else { 

}

 Example to understand the if else statement in java 

class Ifelse{ 

public static void main(String[] args) { 

int A=6; 

int area=A*A; 

if(area>=25) 

System.out.println("Area: "+area); 

else 

System.out.println("Invalid Area!"); 

Output: 

Area: 36 

 If - else -if: 

It is a programming conditional structure that if the statement of if is true, a block of code is executed otherwise the else if statement is executed and so on.If all if statements are false then the else statement is to be executed.Similarly this conditional structure executes multiple statements. 

Syntax: 

if(condition){ 

//block 1 

else If(condition) 

//block 2 

else if(condition) 

//block 3 

else{ 

//block 4 

}

Example to understand the if else if statement in java 

import java.util.*; 

public class ifelseif { 

public static void main(String[] args) { 

int age; 

Scanner obj=new Scanner(System.in); 

System.out.print("Enter age: "); 

age=obj.nextInt(); 

if(age>=10&&age<=20) 

System.out.println("Successful!"); 

else if(age>=25&&age<=30) 

System.out.println("Successful!"); 

else if(age>=40&&age<=50) 

System.out.println("Successful!"); 

else 

System.out.println("Invalid age!"); 

Output: 

Enter age: 34 

Invalid age! 

Nested if: 

A statement within another statement called nested if statement. Inner statement is not checked without outer if statement when outer if loop is true then inner statement is to be executed. 

Syntax: 

if(condition){ 

if(condition){ 

else 

}

else{ 

Example to understand the nested if statement in java 

import java.util.*; 

public class nested { 

public static void main(String[] args) { 

int marks; 

Scanner obj=new Scanner(System.in); 

System.out.println("Enter your marks: "); 

marks=obj.nextInt(); 

if(marks>=75) 

System.out.println("You are eligible for Army"); 

int height; 

Scanner sc=new Scanner(System.in); 

System.out.println("Enter your height: "); 

height=sc.nextInt(); 

if(height>=4.5) 

System.out.println("Congratulations!You have passed! 

else 

System.out.println("Sorry!Best of luck for next time"); 

else 

System.out.println("You cannot apply for army"); 

Output: 

Enter your marks: 

98 

You are eligible for Army 

Enter your height: 

Congratulations!You have passed!


Comments

Popular posts from this blog

Applications of Java Programming Language

  There are 8 applications of java are as follows:  ★ Desktop Applications:    Antivirus Software , Foxit Reader etc are desktop applications.  ★ Web Applications:    A type of application that is used on server sites and runs on mobile and computers. In web development, EJB technology is used for web software components and for developing business type applications.  ★ Enterprise Applications:    A type of application that is used like a database .  For Example: Banking apps, Business apps etc.  Database: A type of application that is used to store the record and installed on device (mobile,computer).  ★ Mobile app:    Java programming language is used in all types of mobiles like android,IOS and apple . Basically, it is used for  android.  ★ Embedded System:    A combination of computer and processor that is used in electronic devices like washing machines. These types of devices are coding ...

Kurulus Osman Season 2 Episode 4 (p1) Urdu subtitles ...

 

Text Widgets

  A widget displaying the string text with a single style is called text widget. import 'package:flutter/cupertino.dart' ; import "package:flutter/material.dart" ; void main () { runApp( MyApp ()) ; } class MyApp extends StatelessWidget { @override Widget build (BuildContext context) { // TODO: implement build return MaterialApp ( home: Scaffold ( backgroundColor: Colors. teal , body: SafeArea ( child: Column ( children: <Widget>[ CircleAvatar ( radius: 50 , backgroundImage: NetworkImage ( 'https://dsearch.com/images?q=avatar%20user%20png' ) , ) , Text( 'Ahmad Mukhtiar', style: TextStyle( fontSize: 40.0, color: Colors. white , fontWeight: FontWeight. bold , ) , ), ] , ...