Difference between revisions of "Java/C2/Nested-if/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with ''''Author : '''TalentSprint '''Script Title : '''Nested-If and Ternary Operator '''Keywords : '''If else conditional statements, Ternary operator, nested-if, video tutorial …')
 
Line 37: Line 37:
 
| style="border:0.035cm solid #000000;padding:0.097cm;"| For this tutorial we are using:
 
| style="border:0.035cm solid #000000;padding:0.097cm;"| For this tutorial we are using:
  
'''Ubuntu 11.10,'''
+
'''Ubuntu v 11.10,'''
  
 
'''JDK 1.6,'''and
 
'''JDK 1.6,'''and
Line 49: Line 49:
 
| style="border:0.035cm solid #000000;padding:0.097cm;"| To follow this tutorial, you should know,
 
| style="border:0.035cm solid #000000;padding:0.097cm;"| To follow this tutorial, you should know,
  
- the usage of relational and logical operators.
+
- about the usage of relational and logical operators.
  
 
- '''if...else''' control flow statements.
 
- '''if...else''' control flow statements.
Line 92: Line 92:
  
 
}
 
}
| style="border:0.035cm solid #000000;padding:0.097cm;"| Let us look at the syntax for writing the '''Nested-If statement'''.
+
| style="border:0.035cm solid #000000;padding:0.097cm;"|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.
 
In this case, if condition 1 is true, then the program checks for condition 2.
Line 107: Line 107:
 
If condition 1 is false, then the program will not check condition2.
 
If condition 1 is false, then the program will not check condition2.
  
Instead will directly jump to its else statement i.e. block 3.
+
Instead it will directly jump to its else statement i.e. block 3.
  
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.176cm;"|  
 
| style="border:0.035cm solid #000000;padding:0.176cm;"|  
| style="border:0.035cm solid #000000;padding:0.176cm;"| Let us try and example to understand that better
+
| style="border:0.035cm solid #000000;padding:0.176cm;"| nOw Let us try and example to understand that better
  
 
|-
 
|-
Line 140: Line 140:
  
  
But we will also handle negative numbers using a nested-if.
+
  we will also handle negative numbers using a nested-if.
  
  
So type  
+
So inside the main method  type  
  
 
'''int n = minus 5;'''
 
'''int n = minus 5;'''
  
  
We have created a variable to store the number.
+
We have created a variable to store the negative number.
  
 
Now we shall write the if conditions.
 
Now we shall write the if conditions.
Line 160: Line 160:
  
 
'''}'''
 
'''}'''
| style="border:0.035cm solid #000000;padding:0.176cm;"| Type
+
| style="border:0.035cm solid #000000;padding:0.176cm;"|Next line Type
  
  
 
'''if (n<nowiki> < 0) </nowiki>'''
 
'''if (n<nowiki> < 0) </nowiki>'''
  
'''''open curly bracket'''''
+
'''''open curly bracket'''''. Press enter
  
'''System.out.println(“Negative number”);'''
+
'''System.out.println Within brackets and double quotes (“Negative number”);'''
 
+
'''''close curly bracket'''''
+
  
 +
 
  
 
We first see if the number is a negative number.
 
We first see if the number is a negative number.
Line 178: Line 177:
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.176cm;"|  
 
| style="border:0.035cm solid #000000;padding:0.176cm;"|  
| style="border:0.035cm solid #000000;padding:0.176cm;"| Then if the number is not a negative, we then check for even and odd.
+
| style="border:0.035cm solid #000000;padding:0.176cm;"| if the number is not a negative, we then check for even and odd.
  
 
|-
 
|-
Line 186: Line 185:
  
 
'''}'''
 
'''}'''
| style="border:0.035cm solid #000000;padding:0.176cm;"| Type
+
| style="border:0.035cm solid #000000;padding:0.176cm;"| Next line Type
  
 
'''else {'''
 
'''else {'''
  
'''}'''
+
'''}''' Press enter
  
  
Line 216: Line 215:
 
| style="border:0.035cm solid #000000;padding:0.176cm;"| Type
 
| style="border:0.035cm solid #000000;padding:0.176cm;"| Type
  
'''if (n ''percent'' 2 equalto equalto 0) {'''
+
'''if (n ''modules'' 2 double equal to 0) {''' Press enter
  
 
'''System.out.println(“Even number”);'''
 
'''System.out.println(“Even number”);'''
Line 237: Line 236:
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.176cm;"| '''Save''' and '''Run'''
 
| style="border:0.035cm solid #000000;padding:0.176cm;"| '''Save''' and '''Run'''
| style="border:0.035cm solid #000000;padding:0.176cm;"| As we can see, we get the output “negative”.
+
| style="border:0.035cm solid #000000;padding:0.176cm;"| Save and run the file.As we can see, we get the output as“negative”.
  
  
Let us try a positive number
+
now Let us try a positive number
  
 
|-
 
|-
Line 247: Line 246:
  
  
'''Save''' and''' Run'''.
+
'''Save''' and''' Run''' the file
  
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.176cm;"| Point to the output
 
| style="border:0.035cm solid #000000;padding:0.176cm;"| Point to the output
| style="border:0.035cm solid #000000;padding:0.176cm;"| As we can see, the output is odd as expected. Let us try an even number
+
| style="border:0.035cm solid #000000;padding:0.176cm;"| As we can see, the output is odd number as expected. Let us try an even number
  
 
|-
 
|-
Line 257: Line 256:
 
| style="border:0.035cm solid #000000;padding:0.176cm;"| Change n = 5 to n = 10.
 
| style="border:0.035cm solid #000000;padding:0.176cm;"| Change n = 5 to n = 10.
  
Save and run.
+
Now Save and run the file
  
 
|-
 
|-
 
| style="border:0.035cm solid #000000;padding:0.176cm;"| Point to the output.
 
| style="border:0.035cm solid #000000;padding:0.176cm;"| Point to the output.
| style="border:0.035cm solid #000000;padding:0.176cm;"| As we can see, the output is “even” as expected.
+
| style="border:0.035cm solid #000000;padding:0.176cm;"| As we can see, the output is “even” number as expected.
  
 
|-
 
|-
Line 294: Line 293:
  
  
Which means, when 7 is divided by 2, we get must 4 and not 3
+
Which means, when 7 is divided by 2, we get   4 and not 3
  
  
Line 332: Line 331:
  
 
'''}'''
 
'''}'''
| style="border:0.035cm solid #000000;padding:0.176cm;"| Type
+
| style="border:0.035cm solid #000000;padding:0.176cm;"|NExt line Type
  
 
'''n = 5;'''
 
'''n = 5;'''
  
  
'''if (n % 2 == 0) {'''
+
'''if (n % 2 == 0) {''' Press enter
  
 
'''nHalf = n / 2;'''
 
'''nHalf = n / 2;'''
Line 357: Line 356:
  
 
'''System.out.println(nHalf);'''
 
'''System.out.println(nHalf);'''
| style="border:0.035cm solid #000000;padding:0.176cm;"| Let us add a print statement to see the program in action.
+
| style="border:0.035cm solid #000000;padding:0.176cm;"|Now  Let us add a print statement to see the program in action.
  
  
Type
+
So  Type
  
  
Line 370: Line 369:
  
 
Point to the output.
 
Point to the output.
| style="border:0.035cm solid #000000;padding:0.176cm;"| '''Save''' and '''Run'''.
+
| style="border:0.035cm solid #000000;padding:0.176cm;"| '''Save''' and '''Run''' the file
  
  
As we can see, our objective is met.
+
As we can see, our objective is met. We get the output as 3 and not 2
  
 
|-
 
|-
Line 391: Line 390:
  
 
Ternary Operator.
 
Ternary Operator.
| style="border:0.035cm solid #000000;padding:0.176cm;"| A conditional operator providing results similar to nested-if.
+
| style="border:0.035cm solid #000000;padding:0.176cm;"| Ternary Operator conditional operator providing results similar to nested-if.
  
  
Provides a short syntax and is denoted by a question mark.
+
It Provides a short syntax and is denoted by a question mark.
  
  
Takes three operands at a time.
+
It Takes three operands at a time.
  
 
|-
 
|-
Line 423: Line 422:
  
  
Let us use it in our program.
+
Now Let us use it in our program.
  
 
|-
 
|-
Line 439: Line 438:
  
  
'''nHalf = n % 2 == 0 ? n / 2 : (n + 1) / 2'''
+
'''nHalf = n % 2 == 0 ? n / 2 : (n + 1) / 2''' semi-colon
  
  
Line 454: Line 453:
  
  
'''Save''' and '''Run'''.
+
'''Save''' and '''Run'''the file. Press Ctrl S and Ctrl F11  keys
  
 
|-
 
|-

Revision as of 10:39, 9 May 2013

Author : TalentSprint

Script Title : Nested-If and Ternary Operator

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


Visual Clue 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