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

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(3 intermediate revisions by one other user not shown)
Line 8: Line 8:
  
 
|-
 
|-
| 00:07
+
| 00:07
| In this tutorial, we will learn:
+
| In this tutorial, we will learn:
  
 
|-
 
|-
| 00:09
+
| 00:09
| * About '''conditional statements'''
+
| About '''conditional statements'''
 +
 
 
|-
 
|-
| 00:11  
+
| 00:11  
| * Types of '''conditional statements''' and
+
| Types of '''conditional statements''' and
 +
 
 
|-
 
|-
| 00:13  
+
| 00:13  
| * How to use '''conditional statements''' in Java programs.
+
| How to use '''conditional statements''' in Java programs.
  
 
|-
 
|-
Line 35: Line 37:
  
 
|-
 
|-
| 00:31
+
| 00:31
| * '''Arithmetic''', '''Relational''' and '''Logical''' '''operators''' in Java.
+
| '''Arithmetic''', '''Relational''' and '''Logical''' '''operators''' in Java.
  
 
|-
 
|-
Line 47: Line 49:
  
 
|-
 
|-
| 00:48
+
| 00:48
 
| In such cases you can use '''conditional statements'''.
 
| In such cases you can use '''conditional statements'''.
  
 
|-
 
|-
| 00:52
+
| 00:52
 
| A conditional statement helps to control the '''flow''' of '''execution''' of a program.  
 
| A conditional statement helps to control the '''flow''' of '''execution''' of a program.  
  
 
|-
 
|-
| 00:57
+
| 00:57
| In Java, we have the following conditional statements:
+
| In Java, we have the following conditional statements:
  
 
|-
 
|-
| 01:01
+
| 01:01
| * '''If''' statement
+
| '''If''' statement '''If...Else''' statement
 +
 
 
|-
 
|-
| 01:02
+
| 01:03
| * '''If...Else''' statement
+
| '''If...Else if''' statement
|-
+
 
01:03
+
| * '''If...Else if''' statement
+
|-
+
|  01:05
+
| * '''Nested If''' statement
+
 
|-
 
|-
| 01:06
+
| 01:05
| * '''Switch''' statement
+
|'''Nested If''' statement '''Switch''' statement
  
 
|-
 
|-
| 01:08
+
| 01:08
 
| In this tutorial, we will learn about '''If, If...Else and If...Else If''' statements in detail.
 
| In this tutorial, we will learn about '''If, If...Else and If...Else If''' statements in detail.
  
 
|-
 
|-
 
| 01:15
 
| 01:15
| | '''if statement''': '''if''' statement is used to '''execute''' a block of statements based on a condition.  
+
| '''if statement''': '''if''' statement is used to '''execute''' a block of statements based on a condition.  
  
 
|-
 
|-
| 01:22
+
| 01:22
 
| It is called a '''single conditional statement.'''
 
| It is called a '''single conditional statement.'''
  
 
|-
 
|-
| 01:26
+
| 01:26
|   Syntax for '''If''' statement ;
+
| Syntax for '''If''' statement ;
  
 
|-
 
|-
| 01:28
+
| 01:28
 
| In the '''if''' statement, if the condition is true, the block is executed.
 
| In the '''if''' statement, if the condition is true, the block is executed.
  
 
|-
 
|-
| 01:34
+
| 01:34
 
| If the condition is false, the block is skipped and it is  not executed.
 
| If the condition is false, the block is skipped and it is  not executed.
  
 
|-
 
|-
| 01:40
+
| 01:40
 
| Now,  let us look at an example to understand how the '''if''' statement can be used.
 
| Now,  let us look at an example to understand how the '''if''' statement can be used.
  
 
|-
 
|-
| 01:45
+
| 01:45
 
| So, let us switch to '''eclipse.'''
 
| So, let us switch to '''eclipse.'''
  
 
|-
 
|-
| 01:48
+
| 01:48
 
| We will write a program to identify whether a person is '''Minor'''.
 
| We will write a program to identify whether a person is '''Minor'''.
  
 
|-
 
|-
| 01:53
+
| 01:53
 
| I have already created a '''class''' '''Person.'''
 
| I have already created a '''class''' '''Person.'''
  
 
|-
 
|-
| 01:56
+
| 01:56
 
| 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.'''
  
 
|-
 
|-
| 02:02
+
| 02:02
 
| So, type inside the '''main method''': '''int''' space '''age ''' equal to 20 semicolon.
 
| So, type inside the '''main method''': '''int''' space '''age ''' equal to 20 semicolon.
 +
 
|-
 
|-
| 02:14
+
| 02:14
 
|Now, we will write an '''If''' statement as follows:
 
|Now, we will write an '''If''' statement as follows:
  
 
|-
 
|-
| 02:18
+
| 02:18
 
|Next line, '''if ''' within  brackets '''age < 21'''  open curly brackets. Press Enter.
 
|Next line, '''if ''' within  brackets '''age < 21'''  open curly brackets. Press Enter.
  
 
|-
 
|-
| 02:30
+
| 02:30
 
| Here, we are checking if '''age''' is less than '''21'''.
 
| Here, we are checking if '''age''' is less than '''21'''.
  
Line 174: Line 173:
  
 
|-
 
|-
| 03:31
+
| 03:31
 
| These are based on a single condition.
 
| These are based on a single condition.
  
 
|-
 
|-
|   03:34
+
| 03:34
| Let us look at the syntax for writing '''If…Else statement.'''
+
| Let us look at the syntax for writing '''If…Else statement.'''
  
 
|-
 
|-
| 03:38
+
| 03:38
 
| If the condition is '''true''',  the statement or block of code is executed.
 
| If the condition is '''true''',  the statement or block of code is executed.
  
 
|-
 
|-
| 03:44
+
| 03:44
 
| Else, it executes another statement or  block of code.
 
| Else, it executes another statement or  block of code.
  
Line 250: Line 249:
  
 
|-
 
|-
|   05:22
+
| 05:22
 
|  '''If…Else If''' statement: '''If…Else If''' statement is used to execute various set of statements.
 
|  '''If…Else If''' statement: '''If…Else If''' statement is used to execute various set of statements.
  
 
|-
 
|-
| 05:29
+
| 05:29
 
| These are based on the two given conditions.
 
| These are based on the two given conditions.
  
Line 262: Line 261:
  
 
|-
 
|-
| 05:38
+
| 05:38
 
| It is also called as  ''' branching ''' or decision making statement.
 
| It is also called as  ''' branching ''' or decision making statement.
  
 
|-
 
|-
|   05:43
+
| 05:43
 
| Now, Let us look at the syntax for writing  the '''If…Else If '''statement.
 
| Now, Let us look at the syntax for writing  the '''If…Else If '''statement.
  
 
|-
 
|-
| 05:48
+
| 05:48
 
| '''If''' statement initially checks for '''condition 1.'''
 
| '''If''' statement initially checks for '''condition 1.'''
  
 
|-
 
|-
| 05:53
+
| 05:53
 
| If '''condition 1''' is true, it executes the '''statement-or-block 1 code'''.
 
| If '''condition 1''' is true, it executes the '''statement-or-block 1 code'''.
  
Line 306: Line 305:
  
 
|-
 
|-
|   06:30
+
| 06:30
|   We will see how the '''If…Else If statement''' can be used in a program.
+
| We will see how the '''If…Else If statement''' can be used in a program.
  
 
|-
 
|-
| 06:35
+
| 06:35
 
| So, switch to '''Eclipse'''.
 
| So, switch to '''Eclipse'''.
  
 
|-
 
|-
| 06:37
+
| 06:37
 
| I have already created a '''class''' named '''Student'''.
 
| I have already created a '''class''' named '''Student'''.
  
Line 375: Line 374:
 
|-
 
|-
 
| 08:51
 
| 08:51
| We get the output as '''A Grade'''.
+
| We get the output as '''A Grade'''.
  
 
|-
 
|-
| 08:55
+
| 08:55
 
| In this program, the student’s 'testScore' is 70.
 
| In this program, the student’s 'testScore' is 70.
  
 
|-
 
|-
| 09:00
+
| 09:00
 
| So the output will be displayed as “A Grade”.
 
| So the output will be displayed as “A Grade”.
  
 
|-
 
|-
|   09:02
+
| 09:02
 
| Now  Let us change the '''testScore''' to ''' 55.'''  
 
| Now  Let us change the '''testScore''' to ''' 55.'''  
  
 
|-
 
|-
| 09:07
+
| 09:07
 
| Now, '''save''' and '''run''' this program.
 
| Now, '''save''' and '''run''' this program.
  
 
|-
 
|-
| 09:10
+
| 09:10
 
| In this case, the output will be displayed as “B Grade”.
 
| In this case, the output will be displayed as “B Grade”.
  
 
|-
 
|-
| 09:16
+
| 09:16
| We can also increase the number of conditions.
+
| We can also increase the number of conditions.
  
 
|-
 
|-
| 09:19
+
| 09:19
 
| Let us add one more condition after the “B grade” output section.
 
| Let us add one more condition after the “B grade” output section.
  
 
|-
 
|-
| 09:23
+
| 09:23
 
| So type here,
 
| So type here,
else, Next line,
+
else, next line, '''if''' within brackets '''testScore''' greater than or equal to '''60''' '''AND''' (&&) '''testScore''' less than or equal to '''70.'''
'''if''' within brackets '''testScore''' greater than or equal to '''60''' '''AND''' (&&) '''testScore''' less than or equal to '''70.'''
+
  
 
|-
 
|-
Line 416: Line 414:
  
 
|-
 
|-
| 10:01
+
| 10:01
 
| Here, if the 'testScore' is between 60 and 70 the program will display "O Grade".
 
| Here, if the 'testScore' is between 60 and 70 the program will display "O Grade".
  
 
|-
 
|-
| 10:07
+
| 10:07
 
| Now, change the '''testScore''' of the student to '''70'''.  
 
| Now, change the '''testScore''' of the student to '''70'''.  
  
 
|-
 
|-
| 10:12
+
| 10:12
 
| Now, '''save '''and''' run''' the file.
 
| Now, '''save '''and''' run''' the file.
  
 
|-
 
|-
 
| 10:15
 
| 10:15
| | We get the output as follows.
+
| We get the output as follows.
  
 
|-
 
|-
| 10:17
+
| 10:17
 
| The program will display the output as “O grade”.
 
| The program will display the output as “O grade”.
  
Line 445: Line 443:
 
|-
 
|-
 
|  10:28
 
|  10:28
|   While coding '''conditional structures''':
+
| While coding '''conditional structures''':
  
 
|-
 
|-
| 10:30
+
| 10:30
| * Always remember to add a semicolon while terminating a statement.
+
| Always remember to add a semicolon while terminating a statement.
  
 
|-
 
|-
| 10:35
+
| 10:35
| * But don’t add semicolons after the condition.
+
| But don’t add semicolons after the condition.
  
 
|-
 
|-
| 10:40
+
| 10:40
| * Add the block of code within curly brackets.
+
| Add the block of code within curly brackets.
 +
 
 
|-
 
|-
| 10:43
+
| 10:43
| * Curly braces are optional if the block contains a single statement.
+
| Curly braces are optional if the block contains a single statement.
 
   
 
   
 
|-
 
|-
Line 467: Line 466:
  
 
|-
 
|-
| 10:51
+
| 10:51
 
| In this tutorial,   
 
| In this tutorial,   
  
 
|-
 
|-
| 10:53
+
| 10:53
 
| We have  Explained: * '''conditional statements'''
 
| We have  Explained: * '''conditional statements'''
 +
 
|-
 
|-
| 10:56
+
| 10:56
| * Listed the types of conditional statements
+
| Listed the types of conditional statements
 +
 
 
|-
 
|-
| 10:59
+
| 10:59
| * Used conditional statements- '''if, if...else''' and '''if...else if''' in Java programs.
+
| Used conditional statements- '''if, if...else''' and '''if...else if''' in Java programs.
  
 
|-
 
|-
Line 485: Line 486:
  
 
|-
 
|-
| 11:12
+
| 11:12
| * Write a java program to compare two values using '''if '''statement.
+
| Write a java program to compare two values using '''if '''statement.
|-
+
|  11:17
+
| * Write a java program to check whether the given number is even or odd.
+
  
Hint : use '''if...else '''statement.
+
|-
 +
| 11:17
 +
| Write a java program to check whether the given number is even or odd.Hint : use '''if...else '''statement.
  
 
|-
 
|-
| 11:23
+
| 11:23
| * Write a java program to find the biggest number among the three numbers.
+
| Write a java program to find the biggest number among the three numbers.Hint : use '''if...else if '''statement.
 
+
Hint : use '''if...else if '''statement.
+
  
 
|-
 
|-
| 11:29
+
| 11:29
| To know more about the Spoken Tutorial project,  
+
| To know more about the Spoken Tutorial project,  
  
 
|-
 
|-
| 11:32
+
| 11:32
 
| watch the video available at the following link.
 
| watch the video available at the following link.
  
Line 516: Line 514:
  
 
|-
 
|-
|   11:42
+
| 11:42
| The Spoken Tutorial Project Team:
+
| The Spoken Tutorial Project team:
  
 
|-
 
|-
| 11:44
+
| 11:44
| * Conducts workshops using Spoken tutorials.  
+
| Conducts workshops using Spoken tutorials.  
 +
 
 
|-
 
|-
| 11:47
+
| 11:47
| * gives certificates for those who pass an online test. For more details, please write to '''contact AT spoken HYPHEN tutorial DOT org.'''
+
| gives certificates for those who pass an online test. For more details, please write to '''contact AT spoken HYPHEN tutorial DOT org.'''
  
 
|-
 
|-
| 11:56
+
| 11:56
| Spoken Tutorial project is a part of the Talk to a Teacher project.  
+
|Spoken Tutorial project is a part of the Talk to a Teacher project.  
 +
 
 
|-
 
|-
| 12:00
+
| 12:00
| It is supported by the National Mission on Education through ICT, MHRD, Government of India.  
+
| It is supported by the National Mission on Education through ICT, MHRD, Government of India.  
 +
 
 
|-
 
|-
 
|  12:06
 
|  12:06
Line 537: Line 538:
  
 
|-
 
|-
|   12:15  
+
| 12:15  
| This script has been contributed by TalentSprint. This is Arya Ratish from IIT Bombay, signing off
+
| This script has been contributed by TalentSprint. This is Arya Ratish from IIT Bombay, signing off.
 
Thanks for joining
 
Thanks for joining
 
|}
 
|}

Latest revision as of 15:16, 9 March 2017

Time Narration
00:02 Welcome to the spoken tutorial on If else constructs in java.
00:07 In this tutorial, we will learn:
00:09 About conditional statements
00:11 Types of conditional statements and
00:13 How to use conditional statements in Java programs.
00:18 For this tutorial, we are using:

Ubuntu v 11.10

JDK 1.6 and

Eclipse 3.7.0.

00:27 To follow this tutorial, you should have knowledge of using:
00:31 Arithmetic, Relational and Logical operators in Java.
00:35 If not, for relevant tutorials, please visit our website which is as shown.
00:42 Conditional statements- You may have to perform different actions for different decisions in your code.
00:48 In such cases you can use conditional statements.
00:52 A conditional statement helps to control the flow of execution of a program.
00:57 In Java, we have the following conditional statements:
01:01 If statement If...Else statement
01:03 If...Else if statement
01:05 Nested If statement Switch statement
01:08 In this tutorial, we will learn about If, If...Else and If...Else If statements in detail.
01:15 if statement: if statement is used to execute a block of statements based on a condition.
01:22 It is called a single conditional statement.
01:26 Syntax for If statement ;
01:28 In the if statement, if the condition is true, the block is executed.
01:34 If the condition is false, the block is skipped and it is not executed.
01:40 Now, let us look at an example to understand how the if statement can be used.
01:45 So, let us switch to eclipse.
01:48 We will write a program to identify whether a person is Minor.
01:53 I have already created a class Person.
01:56 Now, inside the main method, let us declare a variable ‘age’ of type int.
02:02 So, type inside the main method: int space age equal to 20 semicolon.
02:14 Now, we will write an If statement as follows:
02:18 Next line, if within brackets age < 21 open curly brackets. Press Enter.
02:30 Here, we are checking if age is less than 21.
02:34 Whatever is inside the brackets belongs to the if block.
02:38 So, inside the brackets type:
02:41 System dot out dot println within brackets and double quotes The person is Minor semicolon.
02:56 Here, if age is less than 21, then “The person is minor” will be displayed.
03:03 So save and run the file.
03:08 We get the output as follows: "The person is minor".
03:14 In this case, the person's age is 20 which is less than 21.
03:20 So, we got the output as “The person is minor”.
03:24 Now, we will learn about if...else statement.
03:27 If...Else statement is used to execute alternative statements.
03:31 These are based on a single condition.
03:34 Let us look at the syntax for writing If…Else statement.
03:38 If the condition is true, the statement or block of code is executed.
03:44 Else, it executes another statement or block of code.
03:49 We will now see how the If…else statement can be used in a program.
03:54 So, let us switch to the eclipse.
03:57 We will now write a program to identify whether the person is minor or major.
04:03 So, inside the main method, type: int age equal to 25
04:12 then, if within brackets age greater than 21
04:19 within curly brackets, type: System dot out dot println within brackets The person is Major.
04:28 Then type, next line,
04:32 else within curly brackets type:
04:38 System dot out dot println within brackets in double quotes The person is Minor semicolon.
04:51 Here, if age is less than 21, “The person is Minor” will be displayed.
04:58 Else, the “The person is Major” will be displayed.
05:02 So, now let us save and run the program.
05:07 We get the output as: "The person is Major".
05:11 Here, the person's age is 25, which is greater than 21.
05:17 Therefore, the program displayed the output as “The person is Major”.
05:22 If…Else If statement: If…Else If statement is used to execute various set of statements.
05:29 These are based on the two given conditions.
05:33 You can also add more conditions based on your requirement.
05:38 It is also called as branching or decision making statement.
05:43 Now, Let us look at the syntax for writing the If…Else If statement.
05:48 If statement initially checks for condition 1.
05:53 If condition 1 is true, it executes the statement-or-block 1 code.
05:59 Else, it again checks for condition 2.
06:02 If condition 2 is true, it executes statement-or-block 2.
06:09 Else, it executes statement 3 or block code 3.
06:13 In this way, we can extend the code by If…Else blocks.
06:17 These blocks can have multiple conditions.
06:20 The corresponding code will get executed, until it finds the true condition.
06:25 If all the conditions are false, it will execute the final Else section.
06:30 We will see how the If…Else If statement can be used in a program.
06:35 So, switch to Eclipse.
06:37 I have already created a class named Student.
06:40 Let us write a program to identify grade of a student.
06:44 This is done based on the score percentage.
06:47 So, inside the main method, type: int space testScore equal to 70 semicolon.
06:58 The input variable named testScore is used to get the score percentage.
07:05 Next line, type: if within brackets testScore less than 35 then within curly brackets type: System dot out dot println within brackets and double quotes C grade semicolon.
07:28 If the 'testScore' is less than 35, then the program displays "C Grade".
07:34 Next line, type else.
07:37 Next line, if within brackets testScore greater than or equal to 35 AND (&&) testScore less than or equal to 60. Put this whole condition within brackets open curly brackets, press Enter.
08:03 Type: System dot out dot println within brackets B grade semicolon
08:13 Here, the program will check for the second condition in the Else If section.
08:18 If the 'testScore' is between 35 and 60 then the program displays "B Grade".
08:24 Next line, type: else within brackets type: System dot out dot println within brackets and double quotes A grade semicolon.
08:42 So Finally, if both the conditions are false, the program displays “A Grade".
08:48 Now, let us save and run this code.
08:51 We get the output as A Grade.
08:55 In this program, the student’s 'testScore' is 70.
09:00 So the output will be displayed as “A Grade”.
09:02 Now Let us change the testScore to 55.
09:07 Now, save and run this program.
09:10 In this case, the output will be displayed as “B Grade”.
09:16 We can also increase the number of conditions.
09:19 Let us add one more condition after the “B grade” output section.
09:23 So type here,

else, next line, if within brackets testScore greater than or equal to 60 AND (&&) testScore less than or equal to 70.

09:47 Open curly brackets press Enter System dot out dot println within brackets and double quotes O grade semicolon.
10:01 Here, if the 'testScore' is between 60 and 70 the program will display "O Grade".
10:07 Now, change the testScore of the student to 70.
10:12 Now, save and run the file.
10:15 We get the output as follows.
10:17 The program will display the output as “O grade”.
10:20 It is not “A grade” as it displayed before.
10:23 The program will display “A grade” for the testScore greater than 70.
10:28 While coding conditional structures:
10:30 Always remember to add a semicolon while terminating a statement.
10:35 But don’t add semicolons after the condition.
10:40 Add the block of code within curly brackets.
10:43 Curly braces are optional if the block contains a single statement.
10:49 We have come to the end of this tutorial.
10:51 In this tutorial,
10:53 We have Explained: * conditional statements
10:56 Listed the types of conditional statements
10:59 Used conditional statements- if, if...else and if...else if in Java programs.
11:04 Now take an assignment on writing java programs using conditional statements- if, if...else and if...else if statements.
11:12 Write a java program to compare two values using if statement.
11:17 Write a java program to check whether the given number is even or odd.Hint : use if...else statement.
11:23 Write a java program to find the biggest number among the three numbers.Hint : use if...else if statement.
11:29 To know more about the Spoken Tutorial project,
11:32 watch the video available at the following link.
11:35 It summarizes the Spoken Tutorial project.
11:38 If you do not have good bandwidth, you can download and watch it.
11:42 The Spoken Tutorial Project team:
11:44 Conducts workshops using Spoken tutorials.
11:47 gives certificates for those who pass an online test. For more details, please write to contact AT spoken HYPHEN tutorial DOT org.
11:56 Spoken Tutorial project is a part of the Talk to a Teacher project.
12:00 It is supported by the National Mission on Education through ICT, MHRD, Government of India.
12:06 More information on this mission is available at spoken HYPHEN tutorial DOT org SLASH NMEICT HYPHEN Intro.
12:15 This script has been contributed by TalentSprint. This is Arya Ratish from IIT Bombay, signing off.

Thanks for joining

Contributors and Content Editors

Gaurav, PoojaMoolya, Priyacst, Sandhya.np14, Sneha