IP_Lecture_04_Latex PDF
Document Details
Uploaded by InventiveBougainvillea
Sri Lanka Institute of Information Technology (SLIT)
Dr.Junius Anjana
Tags
Related
- IT1120 - Introduction to Programming Lecture Notes PDF
- Introduction to Computer Programming (CS109) Lecture Notes PDF
- IP_Lecture_08_Latex PDF - SLIIT University
- Cours JAVA : Notions de Base, 2021/2022 PDF
- Lecture 2 (Chapter 2) Introduction to Java Programming PDF
- Programming Paradigm and Intro to JAVA Lecture
Summary
This document is a set of programming lectures about introductory programming. It contains topics about sequence selection statements in Java programming. It also contains a section on IF/ELSE and switch statements. The lectures come from SLIIT University.
Full Transcript
Sri Lanka Institute of Information Technology Faculty of Computing IT1120 - Introduction to Programming Dr.Junius Anjana Year 01 and Semester 01 Year 01 and Semester 01 1 / 26 Lecture 4 Sequence...
Sri Lanka Institute of Information Technology Faculty of Computing IT1120 - Introduction to Programming Dr.Junius Anjana Year 01 and Semester 01 Year 01 and Semester 01 1 / 26 Lecture 4 Sequence , Selection Statements Year 01 and Semester 01 2 / 26 Selection Statements Obviously in the real world we make choices Year 01 and Semester 01 3 / 26 Selection In solving a problem, we can make different choices depending on certain conditions - i.e. we make decisions The same can be done in programming as a part of decision making Note: though selection is a separate construct to sequence, the two are combined in the overall solution and does not replace each other Year 01 and Semester 01 4 / 26 Selection There may be alternative steps that could be taken subject to a particular condition Year 01 and Semester 01 5 / 26 Types of Selection Statements IF statement This is the basic selection statement to check a condition when making a decision If the condition is true, the code block following the IF statement gets executed If the condition is false, the code block is skipped Exercise Input two integers from the keyboard. Display whether the two integers are equal. Write a pseudocode to the above scenario. Draw the flow chart to the above scenario. Write a Java program to the above scenario. Year 01 and Semester 01 6 / 26 Write a pseudocode to the above scenario Year 01 and Semester 01 7 / 26 Draw the flow chart to the above scenario Year 01 and Semester 01 8 / 26 Syntax of IF statement To include several statements in the body of an if, enclose the set of statements in braces { } A left brace { begins the body of each if statement A corresponding right brace } ends each if statement body Any number of statements can be placed in the body of an if statement A set of statements contained within a pair of braces is called a compound or a block Year 01 and Semester 01 9 / 26 Write a Java program to the above scenario Year 01 and Semester 01 10 / 26 IF ELSE statement This builds upon the IF statement. It checks a condition and then provides two options: If the condition is true, one block of code runs If the condition is false, a different block of code runs Year 01 and Semester 01 11 / 26 Syntax of IF ELSE statement Year 01 and Semester 01 12 / 26 Exercise Input two integers from the keyboard. Display whether the two integers are equal or not. Write a pseudocode to the above scenario Draw the flow chart to the above scenario Write a Java program to the above scenario Year 01 and Semester 01 13 / 26 Write a pseudocode to the above scenario Year 01 and Semester 01 14 / 26 Draw the flow chart to the above scenario Year 01 and Semester 01 15 / 26 Write a java program to the above scenario Year 01 and Semester 01 16 / 26 ELSE IF statement Multiple ELSE IF statements are used to check multiple cases or conditions. Exercise In an ice cream shop, the customer can input the ice cream flavour type (C, S, V) that he/she needs to purchase. Display the flavour of the ice cream that the customer purchases. Write a pseudocode to the above scenario. Draw the flow chart to the above scenario. Convert the above pseudocode to a Java program. Year 01 and Semester 01 17 / 26 Write a pseudocode to the above scenario Year 01 and Semester 01 18 / 26 Draw the flow chart to the above scenario Year 01 and Semester 01 19 / 26 Write a java program to the above scenario Year 01 and Semester 01 20 / 26 Switch statement It compares a value (usually an integer or character) to a set of constant expressions (cases). If there’s a match, the code associated with that case is executed. A break statement is typically used to exit the switch after the matching case is executed. Exercise In an ice cream shop, the customer can input the ice cream flavour type (C, S, V) that he/she needs to purchase. Display the flavour of the ice cream that the customer purchases. Write a pseudocode to the above scenario. Convert the above pseudocode to a Java program. Year 01 and Semester 01 21 / 26 Write a pseudocode to the above scenario Year 01 and Semester 01 22 / 26 Year 01 and Semester 01 23 / 26 Write a Java program to the above scenario Year 01 and Semester 01 24 / 26 Ternary Operator ( ? : ) The ternary operator, also known as the conditional operator, is a short way to write if-else statement in a single line of code in Java. It’s a compact way to express a simple condition and choose a value based on whether the condition is true or false. The below images show you how conditional operator and if-else statement are used to write same code segment. Year 01 and Semester 01 25 / 26 Thank You! Year 01 and Semester 01 26 / 26