Java/C2/if-else/English

From Script | Spoken-Tutorial
Jump to: navigation, search

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