Java/C2/Nested-if/English

From Script | Spoken-Tutorial
Revision as of 11:57, 17 June 2014 by PoojaMoolya (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Author : TalentSprint

Script Title : Nested-If and Ternary Operator

Keywords : If else conditional statements, Ternary operator, nested-if, video tutorial


Visual Cue Description
Slide 1

Welcome

Welcome to the spoken tutorial on Nested-If and Ternary Operator in java.
Slide 2

Learning Objectives

By the end of this tutorial you should be able to:
  • Explain Nested-If Statements and Ternary operators.
  • Use them in a Java program.


Slide 3

Tools Used


For this tutorial we are using:

Ubuntu v 11.10,

JDK 1.6,and

Eclipse 3.7.0

Slide 4

Pre-requisites

To follow this tutorial, you should know,

- about the usage of relational and logical operators.

- if...else control flow statements.


If not, for relevant tutorial please visit our website which is as shown.

Slide 5

Nested-If statements

An if statement within another if statement is called a nested-if statement.
Slide 6

Syntax for Nested-If Statement


Point to the line of code as you explain.


if (condition1) {

if (condition2) {

Statement or block 1

}

else {

Statement or block 2

}

}

else {

Statement or block 3

}

Now let us see the syntax for writing the Nested-If statement.

In this case, if condition 1 is true, then the program checks for condition 2.


Condition 2 is given using another If statement.


If condition 2 is true, then the program executes Statement or block 1.

Else, it executes Statement or block 2.


If condition 1 is false, then the program will not check condition2.

Instead it will directly jump to its else statement i.e. block 3.

nOw Let us try and example to understand that better
Point to the code

{ Minimize slides and open Eclise. It should contain the following }


public class NestedIfDemo {

public static void main(String args[]) {

}

}

We have the eclipse IDE and the skeleton required for the rest of the code.

We have created a class NesedIfDemo and added the main method to it.

Type

int n = -5;


Highlight int n = -5;

We shall check if the given number is a even number or an odd number.


 we will also handle negative numbers using a nested-if.


So inside the main method type

int n = minus 5;


We have created a variable n to store the negative number.

Now we shall write the if conditions.

Type

if (n < 0) {

System.out.println(“Negative number”);

}

Next line Type


if (n < 0)

open curly bracket. Press enter

System.out.println Within brackets and double quotes (“Negative number”);


We first see if the number is a negative number.

If yes then we will not check for even and odd.

if the number is not a negative, we then check for even and odd.
Type after the if statement.

else{

}

Next line Type

else {

} Press enter


Now if the execution has come to the else part.

It means that the number is non negative.


So we check for odd or even inside this else part.

inside else, type

if (n % 2 == 0) {

System.out.println(“Even number”);

}

else {

System.out.println(“Odd number”);

}

Type

if (n modules 2 double equal to 0) { Press enter

System.out.println(“Even number”);

}

else {

System.out.println(“Odd number”);

}


So we make sure that negative numbers are not considered for odd or even check.

Now let us see the code in action.
Save and Run Save and run the file.As we can see, we get the output as“negative”.


now Let us try a positive number

Change n = -5 to n = 5 Change n = -5 to n = 5


Save and Run the file

Point to the output As we can see, the output is odd number as expected. Let us try an even number
Change n = 5 to n = 10 Change n = 5 to n = 10.

Now Save and run the file

Point to the output. As we can see, the output is “even” number as expected.
Highlight the code This process of including an if statement inside another, is called nested-if.


There is no limit to the amount of nesting.


But it is a good practice to not go beyond 3 levels of nesting.

Remove everything inside the Main method Now we shall look at the ternary operator.


First let me clean up the Main method.

We shall write a program that divides a number by 2.


It is a very trivial program but the issue comes in dividing odd numbers.


When 7 is divided by 2, we get 3.


But what if we want the result to be rounded off.


Which means, when 7 is divided by 2, we get 4 and not 3


In simple terms, we need the next number.

Let us see how to write such a program.
Type

int n, nHalf;

Type

int n, nHalf;


We will store the number in n and the half number in nHalf

Type


n = 5;


if (n % 2 == 0) {

nHalf = n / 2;

}

else {

nHalf = (n + 1) / 2;

}

NExt line Type

n = 5;


if (n % 2 == 0) { Press enter

nHalf = n / 2;

}

else {

nHalf = (n + 1) / 2;

}


We check if the number is even or odd and do the division accordingly.

Type


System.out.println(nHalf);

Now Let us add a print statement to see the program in action.


So  Type


System.out.println(nHalf);

Save and Run.


Point to the output.

Save and Run the file


As we can see, our objective is met. We get the output as 3 and not 2

Point to the code. But if we notice, all we are doing is, setting the value of a variable depending on a condition.


There is more syntax than logic in our program.


This is when ternary operator makes code simpler.

{Switch to Slides}

Slide 7

Ternary Operator.

Ternary Operator conditional operator providing results similar to nested-if.


It Provides a short syntax and is denoted by a question mark.


It Takes three operands at a time.
Slide 8

Point to the syntax


Syntax of Ternary Operator


Syntax:

Result = Expression ? Operand 1 : Operand 2

Let us learn about the syntax of Ternary Operator.


The expression is the condition that has to be checked.


Operand 1 is the value of the variable Result if the condition is true.


Operand 2 is the value if the condition is false.


Now Let us use it in our program.
{Switch to eclipse}

Remove the if-else statement

First let us remove the if-else statement.
Type


nHalf = n % 2 == 0 ? n / 2 : (n + 1) / 2

Type


nHalf = n % 2 == 0 ? n / 2 : (n + 1) / 2 semi-colon


This statement reads,


if n is even, nHalf is n by 2.

Otherwise, it is n plus 1 by 2.

Save and Run. Let us now see it in action.


Save and Runthe file. Press Ctrl S and Ctrl F11 keys

Point to the output. As we can see, the output is as expected.


This way, ternary operator reduces clutter in the code and improves readability.

Slide 9

Summary

We have come to the end of this tutorial.

In this tutorial we have learnt:

  • About Nested-If Statements and Ternary Operator
  • Usage of Nested-If Statements and Ternary Operator in a Java program


Slide 10

Assignment

Now take an assignment on

Nested-If and Ternary operator. Write java program for the following.

  • Check whether a number is even and also a multiple of 11 using nested-if.
  • Identify the largest number among the two given numbers using Ternary operator.


Slide 11

About the Spoken Tutorial Project

  • It summarizes the Spoken Tutorial project

If you do not have good bandwidth, you can download and watch it

To know more about the Spoken Tutorial project, watch the video available at the following link. It summarizes the project.If you do not have good bandwidth, you can download and watch it.
Slide 12

Spoken Tutorial Workshops

The Spoken Tutorial Project Team

  • Conducts workshops using spoken tutorials
  • Gives certificates for those who pass an online test


The Spoken Tutorial Project Team. Conducts workshops using spoken tutorials and gives certificates for those who pass an online test. For more details, please write to contact AT spoken HYPHEN tutorial DOT org.
Slide 13

Acknowledgement

  • Spoken Tutorial Project is a part of the Talk to a Teacher project
  • It is supported by the National Mission on Education through ICT, MHRD, Government of India
  • More information on this Mission is available at


Spoken Tutorial Project is a part of the Talk to a Teacher project and is supported by the National Mission on Education through ICT, MHRD, Government of India. More information on this Mission is available at spoken HYPHEN tutorial DOT org SLASH NMEICT HYPHEN Intro
Slide 14

About the contributor

  • This tutorial has been contributed by TalentSprint
  • www.talentsprint.com

Thanks for joining

This tutorial has been contributed by TalentSprint.

Thanks for joining.



Contributors and Content Editors

Arya, PoojaMoolya, Sneha