Difference between revisions of "Java/C2/if-else/English"
(Created page with ''''Author : '''TalentSprint '''ScriptTitle: '''If Else Construct '''Keywords: '''if, else, conditional statements, if, if else, if else if, Eclipse, video tutorial {| style=…') |
|||
(One intermediate revision by the same user not shown) | |||
Line 24: | Line 24: | ||
* About conditional statements | * About conditional statements | ||
− | * | + | * types of conditional statements and |
* How to use conditional statements in Java programs | * How to use conditional statements in Java programs | ||
Line 35: | Line 35: | ||
| 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 | + | JDK 1.6 and |
− | Eclipse 3.7 | + | Eclipse 3.7.0 |
|- | |- | ||
Line 95: | Line 95: | ||
− | + | ||
|- | |- | ||
Line 115: | Line 115: | ||
// program continues here''' ''' | // program continues here''' ''' | ||
− | | style="border:0.035cm solid #000000;padding:0.097cm;"| | + | | style="border:0.035cm solid #000000;padding:0.097cm;"| ''Syntax for If statement ''' |
+ | |||
Line 121: | Line 122: | ||
− | If the condition is '''false, '''the block is skipped and not executed. | + | If the condition is '''false, '''the block is skipped and it is not executed. |
Line 157: | Line 158: | ||
Highlight '''{}''' | Highlight '''{}''' | ||
− | | style="border:0.035cm solid #000000;padding:0.097cm;"| Let us look at an example to understand how the '''If Statement''' can be used. | + | | style="border:0.035cm solid #000000;padding:0.097cm;"| now Let us look at an example to understand how the '''If Statement''' can be used. |
− | Let us switch to '''eclipse.''' | + | So Let us switch to '''eclipse.''' |
Line 171: | Line 172: | ||
Now, inside the main method let us declare a variable '''‘age’''' of type '''int.''' | Now, inside the main method let us declare a variable '''‘age’''' of type '''int.''' | ||
− | So type '''''int age '''is equal to'' 20. | + | So type '''''int age '''is equal to'' 20 semi-colom. |
Line 177: | Line 178: | ||
− | '''''if '''within | + | '''''if '''within bracket '''age<nowiki> < 21</nowiki>''' open curly brackets.'' Press enter |
Line 189: | Line 190: | ||
− | '''System '''''dot '''''out '''''dot '''''println '''within brackets and double quotes''' The person is Minor.''' | + | '''System '''''dot '''''out '''''dot '''''println '''within brackets and double quotes''' The person is Minor semi-colon.''' |
− | Here, if '''age '''is less than '''21,''' | + | Here, if '''age '''is less than '''21,''' than “'''The person is minor'''” will be displayed. |
− | + | So '''save''' and''' run '''the file. | |
|- | |- | ||
| style="border:0.035cm solid #000000;padding:0.176cm;"| Highlight the output. | | style="border:0.035cm solid #000000;padding:0.176cm;"| Highlight the output. | ||
− | | style="border:0.035cm solid #000000;padding:0.176cm;"| We get the output as follows. | + | | style="border:0.035cm solid #000000;padding:0.176cm;"| We get the output as follows. The person is minor |
+ | |||
In this case, the person's age is''' 20''', which is less than '''21. ''' | In this case, the person's age is''' 20''', which is less than '''21. ''' | ||
− | |||
So, we got the output as “'''The person is minor'''”. | So, we got the output as “'''The person is minor'''”. | ||
Line 253: | Line 254: | ||
− | If the condition is True, | + | If the condition is True, the statement or block of code is executed.. |
− | Else it executes another statement or | + | Else it executes another statement or block of code. |
|- | |- | ||
Line 305: | Line 306: | ||
Highlight as you explain. | Highlight as you explain. | ||
| style="border:0.035cm solid #000000;padding:0.097cm;"| We will now see how the '''If…else statement '''can be used in a program. | | style="border:0.035cm solid #000000;padding:0.097cm;"| We will now see how the '''If…else statement '''can be used in a program. | ||
+ | |||
+ | So let us switch to the eclipse. | ||
+ | we will write a program to identify whether the person is '''Minor or Major'''. | ||
− | |||
+ | So inside the '''Main''' method type; int age is equal to 25 | ||
− | |||
+ | then '''if '''''within brackets'' '''age''' ''greater than'' '''21''', | ||
− | ''' | + | ''within curly brackets'' type '''System '''''dot''''' out '''''dot''''' println '''''within brackets '''''The person is Major.''' |
− | |||
− | + | Then type,next line | |
− | Then type | + | |
'''else '''''within curly brackets'' type | '''else '''''within curly brackets'' type | ||
− | '''System''' ''dot '''''out''' ''dot'' '''println '''''within brackets'' '''The person is Minor.''' | + | '''System''' ''dot '''''out''' ''dot'' '''println '''''within brackets''in double quotes '''The person is Minor semi-colon.''' |
− | Here, if the '''age''' ''is less than'' '''21''', | + | Here, if the '''age''' ''is less than'' '''21''', “' ''The person is Minor'''” will be displayed. |
Line 331: | Line 333: | ||
− | + | Let us'''save it '''and''' run''' the program. | |
Line 338: | Line 340: | ||
|- | |- | ||
| style="border:0.035cm solid #000000;padding:0.176cm;"| Highlight the output and explain. | | style="border:0.035cm solid #000000;padding:0.176cm;"| Highlight the output and explain. | ||
− | | style="border:0.035cm solid #000000;padding:0.176cm;"| We get the output as | + | | style="border:0.035cm solid #000000;padding:0.176cm;"| We get the output as the person is major |
− | + | ||
Here, the person's '''age''' is '''25,''' which is greater than '''21'''. | Here, the person's '''age''' is '''25,''' which is greater than '''21'''. | ||
− | Therefore the program | + | Therefore the program display the output as “'''The person is Major'''”. |
Line 372: | Line 373: | ||
− | It is also called as | + | It is also called as ''' branching '''or '''decision making statement.''' |
Line 396: | Line 397: | ||
<nowiki><</nowiki>Statement_or_block 3> | <nowiki><</nowiki>Statement_or_block 3> | ||
− | | style="border:0.035cm solid #000000;padding:0.097cm;"| Let us look at the syntax for writing the '''If…Else If '''statement. | + | | style="border:0.035cm solid #000000;padding:0.097cm;"|Now Let us look at the syntax for writing the '''If…Else If '''statement. |
− | + | If statement initially checks for '''condition 1.''' | |
− | If''' condition 1 '''is true, it executes the '''statement | + | If''' condition 1 '''is true, it executes the '''statement '''or '''block code 1'''. |
Line 408: | Line 409: | ||
− | If '''condition 2''' is true, it executes '''statement | + | If '''condition 2''' is true, it executes '''statement '''or '''block code 2'''. |
Line 489: | Line 490: | ||
− | + | So inside the''' Main''' method, type '''int''' ''space''''' testScore''' ''equal to''''' 70''' ''semicolon.'' | |
Line 495: | Line 496: | ||
− | + | Next line type''' if''' ''within brackets'' '''testScore''' ''is less than'' '''35, '''''within curly brackets '''''System '''dot''' out '''''dot println within brackets and double quotes''''' C grade '''''semicolon'''''.''' | |
Line 504: | Line 505: | ||
− | Then next line type '''if'' '''within brackets''''' testScore '''''greater than or equal to'' '''35 '''and '''testScore '''''less than or equal to '''''60.''' | + | Then next line type '''if'' '''within brackets''''' testScore '''''greater than or equal to'' '''35 '''and '''testScore '''''less than or equal to '''''60.'''Put the soul condition within brackets open curly brackets press enter. |
− | + | ||
+ | Type '''System dot println''' within brackets ''' B grade''' semi-colomn | ||
+ | |||
Here, the program will check for the second condition in the '''Else If''' section. | Here, the program will check for the second condition in the '''Else If''' section. | ||
− | |||
If the '''testScore''' is between '''35 '''and '''60''' then the program displays "'''B Grade'''". | If the '''testScore''' is between '''35 '''and '''60''' then the program displays "'''B Grade'''". | ||
Line 526: | Line 527: | ||
|- | |- | ||
| style="border:0.035cm solid #000000;padding:0.176cm;"| Highlight the output and explain | | style="border:0.035cm solid #000000;padding:0.176cm;"| Highlight the output and explain | ||
− | | style="border:0.035cm solid #000000;padding:0.176cm;"| We get the output as | + | | style="border:0.035cm solid #000000;padding:0.176cm;"| We get the output as '''A Grade''' |
− | + | ||
In this program, the student’s '''testScore''' is '''70''' . | In this program, the student’s '''testScore''' is '''70''' . | ||
Line 536: | Line 536: | ||
|- | |- | ||
| style="border:0.035cm solid #000000;padding:0.176cm;"| Change testScore<nowiki>=55;</nowiki> | | style="border:0.035cm solid #000000;padding:0.176cm;"| Change testScore<nowiki>=55;</nowiki> | ||
− | | style="border:0.035cm solid #000000;padding:0.176cm;"| Let us change the''' testScore''' to''' 55.''' | + | | style="border:0.035cm solid #000000;padding:0.176cm;"|Now Let us change the''' testScore''' to''' 55.''' |
Line 546: | Line 546: | ||
|- | |- | ||
| style="border:0.035cm solid #000000;padding:0.176cm;"| Highlight the output as you explain. | | style="border:0.035cm solid #000000;padding:0.176cm;"| Highlight the output as you explain. | ||
− | | style="border:0.035cm solid #000000;padding:0.176cm;"| | + | | style="border:0.035cm solid #000000;padding:0.176cm;"| |
Line 570: | Line 570: | ||
− | So type here, '''if''' ''within brackets''''' testScore''' ''greater than or equal to'' '''60 '''and''' testScore''' ''less than or equal to'' ''' | + | So type here, |
+ | Else, Next line | ||
+ | '''if''' ''within brackets''''' testScore''' ''greater than or equal to'' '''60 '''and''' testScore''' ''less than or equal to'' '''70.''' | ||
− | Then within curly brackets | + | Then within curly brackets press enter '''System '''''dot '''''out''' ''dot '''''println''' ''within brackets and double quotes''''' O grade''' ''semicolon''. |
− | Here if the '''testScore''' is between '''60''' and '''75''' | + | Here if the '''testScore''' is between '''60''' and '''75''' the program will display "'''O Grade'''". |
Line 598: | Line 600: | ||
− | The program will display “'''A grade'''” for the '''testScore''' greater than ''' | + | The program will display “'''A grade'''” for the '''testScore''' greater than '''70.''' |
|- | |- | ||
Line 611: | Line 613: | ||
* But don’t add semi-colons after the condition. | * But don’t add semi-colons after the condition. | ||
− | * Add the block of code within curly | + | * Add the block of code within curly brackets |
* Curly braces are optional if the block contains a single statement. | * Curly braces are optional if the block contains a single statement. | ||
Latest revision as of 10:58, 9 May 2013
Author : TalentSprint
ScriptTitle: If Else Construct
Keywords: if, else, conditional statements, if, if else, if else if, Eclipse, video tutorial
Visual cue | Narration |
Slide 1
Welcome |
Welcome to the spoken tutorial on If else constructs in java. |
Slide 2
Learning Objectives |
In this tutorial we will learn:
|
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 have knowledge of using
If not, for relevant tutorials please visit our website which is as shown. |
Slide 5
Conditional Statements |
You may have to perform different actions for different decisions in your code.
|
Slide 6
|
In Java we have the following conditional statements:
In this tutorial, we will learn about If, If...Else and If...Else If statements in detail. |
Slide 7
|
If statement is used to execute a block of statements based on a condition.
|
Slide 8
{ code_block; } // end of if construct // program continues here |
Syntax for If statement '
In the if statement, if the condition is true, the block is executed.
|
Example of If statement
{ public static void main(String args[]) { int age=20; if(age<21) { System.out.println("The person is Minor"); } }
|
now Let us look at an example to understand how the If Statement can be used.
So type int age is equal to 20 semi-colom.
So save and run the file. |
Highlight the output. | We get the output as follows. The person is minor
In this case, the person's age is 20, which is less than 21. So, we got the output as “The person is minor”.
|
Slide 9
|
Now, we will learn about if...else statement.
|
Slide 10
<statement_or_block> else <statement_or_block> // end of if construct // program continues here |
Let us look at the syntax for writing the If…Else statement.
|
Example of If…Else statement
{ int age=25;
public static void main(String args[]) { if(age>21) { System.out.println("The person is Major"); } else { System.out.println("The person is Minor"); } } }
|
We will now see how the If…else statement can be used in a program.
So let us switch to the eclipse. we will write a program to identify whether the person is Minor or Major.
then if within brackets age greater than 21, within curly brackets type System dot out dot println within brackets The person is Major.
else within curly brackets type System dot out dot println within bracketsin double quotes The person is Minor semi-colon.
|
Highlight the output and explain. | We get the output as the person is major
Here, the person's age is 25, which is greater than 21.
|
Slide 11
(Restore the slide)
|
If…Else If statement is used to execute various set of statements.
|
Slide 12
<Statement_or_block 1>
<Statement of block 2> else <Statement_or_block 3> |
Now Let us look at the syntax for writing the If…Else If statement.
|
Example of If…Else If statement
{ public static void main(String args[]) { int testScore=70;
{ System.out.println("C grade "); } else if((testScore>=35)&&(testScore<=60)) { System.out.println("B grade"); } else { System.out.println("A grade"); } }
|
We will see how the If…Else If statement can be used in a program.
Next line type if within brackets testScore is less than 35, within curly brackets System dot out dot println within brackets and double quotes C grade semicolon.
Type System dot println within brackets B grade semi-colomn Here, the program will check for the second condition in the Else If section. If the testScore is between 35 and 60 then the program displays "B Grade".
|
Highlight the output and explain | We get the output as A Grade
In this program, the student’s testScore is 70 .
|
Change testScore=55; | Now Let us change the testScore to 55.
|
Highlight the output as you explain. |
|
else if((testScore>=60)&&(testScore<=75))
{ System.out.println("O grade"); } Highlight as you explain. |
We can also increase the number of conditions.
if within brackets testScore greater than or equal to 60 and testScore less than or equal to 70.
|
Highlight the output. | We get the output as follows.
|
Slide 13
|
While coding conditional structures:
|
Slide 14
Summary |
We have come to the end of this tutorial.
In this tutorial, we:
|
Slide 15
Assignment
|
Now take an assignment on writing java programs using conditional statements: if, if...else and if...else if statements.
Hint : use if...else statement.
Hint : use if...else if statement.
|
Slide 16
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,
It summarizes the Spoken Tutorial project.
|
Slide 17
Spoken Tutorial Workshops The Spoken Tutorial Project Team
For more details, please write to contact@spoken-tutorial.org |
The Spoken Tutorial Project Team
|
Slide 18
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 19
About the contributor
Thanks for joining
|
This tutorial has been contributed by TalentSprint.
Thanks for joining
|