Conditional Statements Lecture - Koya University PDF
Document Details

Uploaded by MemorablePine5822
Koya University
ReBaz NaJeeb R
Tags
Summary
This lecture from Koya University's Software Engineering department covers conditional statements in Java programming. It includes topics such as if statements, if-else statements, nested if statements, switch statements, and the ternary operator, along with examples and lab tasks.
Full Transcript
KOYA UNIVERSITY FACULTY OF ENGINEERING SOFTWARE ENGINEERING DEPARTMENT. PROGRAMMING [KOU20312] R e b a z. N a j e e b @ k o y a u n i v e r s i t y. o r g Lecture 4 1...
KOYA UNIVERSITY FACULTY OF ENGINEERING SOFTWARE ENGINEERING DEPARTMENT. PROGRAMMING [KOU20312] R e b a z. N a j e e b @ k o y a u n i v e r s i t y. o r g Lecture 4 1 Programming # 1 L e c t u r e d b y : R e B a z N a J e e b R. Today’s class outline conditional statements introduction If and if else statement If else if ladder Nested if Shorthanded if Switch Programming # 2 L e c t u r e d b y : R e B a z N a J e e b R. Conditional statements Pass? Get you Get me lunch lunch Conditional Statements ► In real life, we make decisions based on conditions. For example: 1. if it is raining, take an umbrella. 2. if the temperature is below 10°C, wear a jacket; otherwise, wear a t-shirt. 3. if your exam score is above 90, you get an A; if it is above 80, you get a B, and so on. ► Similarly, in programming, conditional statements allow a program to decide what to do based on given conditions. Condition block ► Conditional statements are fundamental building blocks in programming that let your code decide which set of instructions to execute based on whether a given condition is true or false. ► An if statement is a construct that enables a program to specify alternative paths of execution. Types of conditional statements if Statement (Single Condition) if-else Statement (Two Conditions) if-else if-else Statement (Multiple Conditions) Nested if Statements (if inside another if) switch Statement (Alternative to if-else if) The Ternary Operator (? :) Short-Circuit Evaluation in && and || if-else statement ❍ if statement is used to test the condition. It checks boolean condition: true or false. ❍ There are various types of if statement in java: ❍ if statement ❍if-else statement ❍ if-else-if ladder ❍ nested if statement one condition: if statement ❍ The Java if statement tests the condition. It executes the if block if condition is true. ❍ Syntax: ❍ If (you pass) { I will buy you a gift. } IF example public class IfExample { public static void main(String[] args) { int age=20; if(age>=18){ System.out.print("You are qualified for driving license. "); } } } if example 2 public class IfExample2 { public static void main(String[] args) { int x = 20; int y = 18; if (x > y) { System.out.println("x is greater than y"); } } } Java Programming Language # 10 L e c t u r e d b y : R e B a z N a J e e b R. two-conditions: if – else statement ❍if-else statement also tests the condition. It executes the if block if condition is true otherwise else block is executed. If (you pass) { I will buy you a gift. } else { you buy me a gift. } if-else example public class IfElseExample { public static void main(String[] args) { int number=13; if(number%2==0){ System.out.println("even number"); } else { System.out.println("odd number"); } } } Programming # 12 L e c t u r e d b y : R e B a z N a J e e b R. multi-conditions: if-else-if ladder statement ❍ The if-else-if ladder statement executes one condition from multiple statements. Multibranch example if else-if statement public class IfElseIfExample { public static void main(String[] args) { int marks=89; if(marks=50 && marks=60 && marks=70 && marks=80 && marks=90 && marks=18){ if(weight>50){ System.out.println("You are eligible to donate blood"); } } } } Programming # 17 L e c t u r e d b y : R e B a z N a J e e b R. Nested-if example 2 public class JavaNestedIfExample2 { public static void main(String[] args) { int age=25; int weight=48; if(age>=18){ if(weight>50){ System.out.println("You are eligible to donate blood"); } else{ System.out.println("You are not eligible to donate blood"); } } else{ System.out.println("Age must be greater than 18"); } } } Programming # 18 L e c t u r e d b y : R e B a z N a J e e b R. Switch statement ❍ A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case. Switch example public class SwitchExample { public static void main(String args[]) { char grade = 'C'; switch(grade) { case 'A' : System.out.println("Excellent!"); break; case 'B' : Switch is more like if-else-if ladder case 'C' : System.out.println("Well done"); break; case 'D' : System.out.println("You passed"); case 'F' : System.out.println("Better try again"); break; default : System.out.println("Invalid grade"); } System.out.println("Your grade is " + grade); } } Switch example 2 public class SwitchMonthExample { public static void main(String[] args) { int month=7; String monthString=""; switch(month){ case 1: monthString="1 - January"; break; case 2: monthString="2 - February"; break; case 3: monthString="3 - March"; break; default:System.out.println("Invalid Month!"); } System.out.println(monthString); } } shorthand if – else (ternary operator) ❍ The public class ShortHandIf { public static void main(String[] args) { int time = 20; String result = (time < 18) ? "Good day." : "Good evening."; System.out.println(result); } } Programming # 22 L e c t u r e d b y : R e B a z N a J e e b R. Zodiac example Zodiac solution Switch example 3 Short-Circuit Evaluation in && and || ► && (AND) and || (OR) operators stop evaluation if the result is already determined. Optional { } for one line block ► When there is no curly braces to determine if block , the if statement takes only one line as a if block statement. Follow the flow Common error Simplify your code Lab task 2 Lab task 3 Lab task 4 Lab task 5 Lab task 6 Lab task 7 Lab task 8 Lab task 9 Lab task 10 Socrates: Understanding a question is half an answer.