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

From Script | Spoken-Tutorial
Jump to: navigation, search
(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
* The types of conditional statements and
+
*   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:
  
  
It is also called '''branching''' or '''decision making statement. '''
+
  
 
|-
 
|-
Line 115: Line 115:
  
 
// program continues here''' '''
 
// program continues here''' '''
| style="border:0.035cm solid #000000;padding:0.097cm;"| Let us look at the syntax for writing '''if statement'''.
+
| 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 parentheses '''age<nowiki> < 21</nowiki>''' curly brackets.''
+
'''''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,''' then “'''The person is minor'''” will be displayed.
+
Here, if '''age '''is less than '''21,''' than “'''The person is minor'''” will be displayed.
  
  
Let us '''save''' and''' run '''this file.
+
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, then the statement or block of code is executed..
+
If the condition is True,   the statement or block of code is executed..
  
  
Else it executes another statement or another block of code.
+
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&nbsp;'''can be used in a program.
 
| style="border:0.035cm solid #000000;padding:0.097cm;"| We will now see how the '''If…else statement&nbsp;'''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'''.
  
Now, 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
  
So inside the '''Main''' method type
 
  
 +
then '''if '''''within brackets'' '''age''' ''greater than'' '''21''',
  
'''if '''''within brackets'' '''age''' ''greater than'' '''21''',
+
  ''within curly brackets'' type '''System '''''dot''''' out '''''dot''''' println '''''within brackets '''''The person is Major.'''
  
then ''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''', then “'''The person is Minor'''” will be displayed.
+
Here, if the '''age''' ''is less than'' '''21''',   “' ''The person is Minor'''” will be displayed.
  
  
Line 331: Line 333:
  
  
Now, '''save '''and''' run''' the program.
+
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 follows.
+
| 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 will display the output as “'''The person is Major'''”.
+
Therefore the program display the output as “'''The person is Major'''”.
  
  
Line 372: Line 373:
  
  
It is also called as a''' branching '''or '''decision making statement.'''
+
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.
  
  
This statement initially checks for '''condition 1.'''
+
If statement initially checks for '''condition 1.'''
  
  
If''' condition 1 '''is true, it executes the '''statement 1 '''or '''block code 1'''.
+
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 2 '''or '''block code 2'''.
+
If '''condition 2''' is true, it executes '''statement   '''or '''block code 2'''.
  
  
Line 489: Line 490:
  
  
In the''' Main''' method, type '''int''' ''space''''' testScore''' ''equal to''''' 70''' ''semicolon.''
+
So  inside  the''' Main''' method, type '''int''' ''space''''' testScore''' ''equal to''''' 70''' ''semicolon.''
  
  
Line 495: Line 496:
  
  
Then 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'''''.'''
+
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 follows.
+
| 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;"| We get the output as follows.
+
| 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'' '''75.'''
+
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 type '''System '''''dot '''''out''' ''dot '''''println''' ''within brackets and double quotes''''' O grade''' ''semicolon''.
+
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''' then the program will display "'''O Grade'''".
+
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 '''75.'''
+
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 braces.
+
* 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:
  • About conditional statements
  • types of conditional statements and
  • How to use conditional statements in Java programs


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
  • Arithmetic, Relational and Logical operators in Java

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.


In such cases you can use conditional statements.


A conditional statement helps to control the flow of execution of a program.



Slide 6


Types of Conditional Statements

In Java we have the following conditional statements:
  • If statement 
  • If...Else statement 
  • If...Else if statement 
  • Nested If statement
  • Switch statement

In this tutorial, we will learn about If, If...Else and If...Else If statements in detail.

Slide 7


If Statement


If statement is used to execute a block of statements based on a condition.


It is called a single conditional statement.



Slide 8


Syntax for If statement 


if (condition)

{

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.


If the condition is false, the block is skipped and it is not executed.



Example of If statement 


(Minimize the slide and in the Eclipse tool, type the program according to the explanation)


class Person

{

public static void main(String args[]) {

int age=20;

if(age<21) {

System.out.println("The person is Minor");

}

}


}


Highlight when you explain


Highlight {}

now Let us look at an example to understand how the If Statement can be used.


So Let us switch to eclipse.


We will write a program to identify whether a person is Minor.


I have already created a class Person.


Now, inside the main method let us declare a variable ‘age’ of type int.

So type int age is equal to 20 semi-colom.


Now, we will write an If statement as follows:


if within bracket age < 21 open curly brackets. Press enter


Here, we are checking if age is less than 21.


Whatever is inside the brackets belongs to the if block.


Inside the brackets type


System dot out dot println within brackets and double quotes The person is Minor semi-colon.


Here, if age is less than 21, than “The person is minor” will be displayed.


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


(Restore the slide)


If…Else statement 


Now, we will learn about if...else statement.


If...Else statement is used to execute alternative statements.


These are based on a single condition.



Slide 10


Syntax for If…Else statement 


if ( <condition> )

<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.


If the condition is True, the statement or block of code is executed..


Else it executes another statement or block of code.

Example of If…Else statement


(Please include the example in Eclipse. No need to show it on slide. Show how to run the program in Eclipse and then point to the output at the console.) 


Type the code in Eclipse as per the explanation.


class Person

{

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");

}

}

}


Highlight as you explain.

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

else within curly brackets type

System dot out dot println within bracketsin double quotes The person is Minor semi-colon.


Here, if the age' is less than 21, “' The person is Minor” will be displayed.


Else the “The person is Major” will be displayed.


Let ussave it and run the program.



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.


Therefore the program display the output as “The person is Major”.



Slide 11

(Restore the slide)


If…Else If statement 


If…Else If statement is used to execute various set of statements.


These are based on the two given conditions.


You can also add more conditions based on your requirement.


It is also called as branching or decision making statement.



Slide 12


Syntax for If…Else If statement 


if ( <condition 1> )

<Statement_or_block 1>


else if (<condition 2>)

<Statement of block 2>

else

<Statement_or_block 3>

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 or block code 1.


Else it again checks for condition 2.


If condition 2 is true, it executes statement or block code 2.


Else it executes statement 3 or block code 3.


In this way, we can extend the code by If…Else blocks.


These blocks can have multiple conditions.


The corresponding code will get executed until it finds the true condition.


If all the conditions are false, it will execute the final Else section.

Example of If…Else If statement 


(Minimize the slide and type the code according to the audio narration)


class Student

{

public static void main(String args[])

{

int testScore=70;


if(testScore<35)

{

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.


So switch to Eclipse.


I have already created a class named Student.


Let us write a program to identify grade of a student.


This is done based on the score percentage.


So inside the Main method, type int space testScore equal to 70 semicolon.


The input variable named ‘testScore’ is used to get the score percentage.


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.


If the testScore is less than 35, then the program displays "C Grade".


Next line type else


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.

If the testScore is between 35 and 60 then the program displays "B Grade".


Next line type else within brackets type System dot out dot println within brackets and double quotes A grade semicolon.


Finally, if both the conditions are False, the program displays “A Grade".


Now, let us save and run this code.


.

Highlight the output and explain We get the output as A Grade

In this program, the student’s testScore is 70 .


So the output will be displayed as “A Grade”.

Change testScore=55; Now Let us change the testScore to 55.


Now, save and run this program.



Highlight the output as you explain.


In this case, the output will be displayed as “B Grade”.



else if((testScore>=60)&&(testScore<=75))

{

System.out.println("O grade");

}

Highlight as you explain.

We can also increase the number of conditions.


Let us add one more condition after the “B grade” output section.


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 press enter System dot out dot println within brackets and double quotes O grade semicolon.


Here if the testScore is between 60 and 75 the program will display "O Grade".


Now, change the testScore of the student to 70.


Now, save and run the file.



Highlight the output. We get the output as follows.


The program will display the output as “O grade”.


It is not “A grade” as it displayed before.


The program will display “A grade” for the testScore greater than 70.

Slide 13


Points to Remember

While coding conditional structures:
  • Always remember to add a semicolon while terminating a statement.
  • But don’t add semi-colons after the condition.
  • Add the block of code within curly brackets
  • Curly braces are optional if the block contains a single statement.


Slide 14

Summary

We have come to the end of this tutorial.

In this tutorial, we:

  • Explained conditional statements
  • Listed the types of conditional statements
  • Used conditional statements: if, if...else and if...else if in Java programs.


Slide 15

Assignment


Now take an assignment on writing java programs using conditional statements: if, if...else and if...else if statements.


  • Write a java program to compare two values using if statement.
  • Write a java program to check whether the given number is even or odd.

Hint : use if...else statement.

  • Write a java program to find the biggest number among the three numbers.

Hint : use if...else if statement.



Slide 16

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 Spoken Tutorial project.


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

Slide 17

Spoken Tutorial Workshops

The Spoken Tutorial Project Team

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

For more details, please write to contact@spoken-tutorial.org

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


Slide 18

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

http://spoken-tutorial.org/NMEICT-Intro

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

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

Thanks for joining


This tutorial has been contributed by TalentSprint.

Thanks for joining



Contributors and Content Editors

Arya, Sneha