Difference between revisions of "Java/C2/Nested-if/English"
(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;"| | + | | 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: | ||
− | + | 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 n 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”);''' |
− | + | ||
− | + | ||
+ | |||
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;"| | + | | 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 '' | + | '''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 | + | | 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 | + | 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;"| | + | | 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:
|
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.
|
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
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.
Else, it executes Statement or block 2.
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 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;
|
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.
int n = minus 5;
Now we shall write the if conditions. |
Type
if (n < 0) { System.out.println(“Negative number”); } |
Next line Type
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
It means that the number is non negative.
|
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”); }
|
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”.
|
Change n = -5 to n = 5 | Change n = -5 to n = 5
|
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.
|
Remove everything inside the Main method | Now we shall look at the ternary operator.
|
We shall write a program that divides a number by 2.
| |
Let us see how to write such a program. | |
Type
int n, nHalf; |
Type
int n, nHalf;
|
Type
nHalf = n / 2; } else { nHalf = (n + 1) / 2; } |
NExt line Type
n = 5;
nHalf = n / 2; } else { nHalf = (n + 1) / 2; }
|
Type
|
Now Let us add a print statement to see the program in action.
So Type
|
Save and Run.
|
Save and Run the file
|
Point to the code. | But if we notice, all we are doing is, setting the value of a variable depending on a condition.
|
{Switch to Slides}
Slide 7 Ternary Operator. |
Ternary Operator conditional operator providing results similar to nested-if.
It Takes three operands at a time. |
Slide 8
Point to the syntax
Result = Expression ? Operand 1 : Operand 2 |
Let us learn about the syntax of Ternary Operator.
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
|
Type
Otherwise, it is n plus 1 by 2. |
Save and Run. | Let us now see it in action.
|
Point to the output. | As we can see, the output is as expected.
|
Slide 9
Summary |
We have come to the end of this tutorial.
In this tutorial we have learnt:
|
Slide 10
Assignment |
Now take an assignment on
Nested-If and Ternary operator. Write java program for the following.
|
Slide 11
About 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
|
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 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
Thanks for joining |
This tutorial has been contributed by TalentSprint.
Thanks for joining.
|