Self study.

Slides:



Advertisements
Similar presentations
CS110 Programming Language I Lab 10: Arrays I Computer Science Department Spring 2014.
Advertisements

5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter4: Control Statements Part I.
1 BUILDING JAVA PROGRAMS CHAPTER 4 CONDITIONAL EXECUTION.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Introduction to Computer Programming Decisions If/Else Booleans.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
If statements Chapter 3. Selection Want to be able to do a statement sometimes, but not others if it is raining, wear a raincoat. Start first with how.
Writing algorithms using the while-statement. Previously discussed Syntax of while-statement:
Conditionals (Cont’d). 2 Nested if/else question Formula for body mass index (BMI): Write a program that produces output like the following: This program.
Flow of Control Java Programming Mrs. C. Furman January 5, 2009.
Conditional If Week 3. Lecture outcomes Boolean operators – == (equal ) – OR (||) – AND (&&) If statements User input vs command line arguments.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Chapter 4: Control Structures I
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Boolean expressions, part 2: Logical operators. Previously discussed Recall that there are 2 types of operators that return a boolean result (true or.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
CHAPTER 8 CONTROL STRUCTURES Prepared by: Lec. Ghader R. Kurdi.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
Chapter 4: Control Structures SELECTION STATEMENTS.
The if-else statement. The if-else statement in Java The if-else statement is the second conditional statement in Java The if-else statement selects one.
COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson
CHAPTER#3 PART1 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc.
OOP (pre) Basic Programming. Writing to Screen print will display the string to the screen, the following print statement will be appended to the previous.
COMP Flow of Control: Branching 1 Yi Hong May 19, 2015.
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Chapter#3 Part1 Structured Program Development in C++
Catie Welsh February 2,  Program 1 Due Today by 11:59pm today  Program 2 Assigned Today  Lab 2 Due Friday by 1:00pm 2.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
SELF STUDY. IF STATEMENTS SELECTION STRUCTURE if selection statement if … else selection statement switch selection statement.
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
COMP 110 Branching Statements and Boolean Expressions Luv Kohli September 8, 2008 MWF 2-2:50 pm Sitterson
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101.
Computer Programming Lab 9. Exercise 1 Source Code package excercise1; import java.util.Scanner; public class Excercise1 { public static void main(String[]
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
CS0007: Introduction to Computer Programming
Decision making If.. else statement.
Chapter 4: Control Structures I
Chapter 4: Control Structures I
Introduction to programming in java
Chapter 5: Control Structures II
TK1114 Computer Programming
The switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression,
SELECTION STATEMENTS (1)
Control Statement Examples
Chapter 4: Control Structures I
Chapter 6 More Conditionals and Loops
מבוא למדעי המחשב, סמסטר א', תשע"א תרגול מס' 2
SELECTION STATEMENTS (2)
AP Java Review If else.
Decision making If statement.
Building Java Programs
If Statements.
Control Structure Chapter 3.
Factoring if/else code
Lecture Notes – Week 2 Lecture-2
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
AP Java Review If else.
Structured Program Development in C++
Building Java Programs
Building Java Programs
Factoring if/else code
Control Structure.
Selection Statements August 22, 2019 ICS102: The course.
Presentation transcript:

Self study

If statements

Selection Structure if selection statement if…else selection statement switch selection statement

Example ... if ( grade >= 60 ) System.out.print("Passed”); Write a program that reads a student’s grade and prints “passed” if the grade is greater than or equal 60. Flowchart: Code: ... if ( grade >= 60 ) System.out.print("Passed”);

One-Way Selection Example: char grade=‘’ if ( score >= 90 ) grade = ‘A’;

Example if-statement: find absolute value import java.util.Scanner; public class If1 { public static void main(String[] args) Scanner in = new Scanner(System.in); int a; System.out.print("Enter an integer value: "); a = in.nextInt(); // Read in an integer from keyboard if ( a < 0 ) // If-statement a = -a; // Negate a ONLY if a < 0 System.out.print("It's absolute value = "); System.out.println(a); }

Two-Way Selection Syntax: if (expression) statement1; else statement2; else statement must be paired with an if.

Two-Way Selection Example: boolean positive, negative; if (number >= 0 ) positive = true; else //number < 0 negative =true;

Multiple Selection: Nested if Example4_23 : if ( GPA >= 2.0 ) if (GPA >= 3.9) System.out.println(“Dean Honor list”); else System.out.println(“GPA below graduation requirement”); If GPA = 3.8 what will be printed? GPA below graduation requirement

Multiple Selection: Nested if Example4_23 : (rewritten) if ( GPA >= 2.0 ) { if (GPA >= 3.9) System.out.println(“Dean Honor list”); } else System.out.println(“GPA below graduation requirement”); Now, if GPA = 3.8 what will be printed?

public class Test { public static void main(String args[]){ int x = 30; if( x == 10 ){ System.out.print("Value of X is 10");} else if( x == 20 ){ System.out.print("Value of X is 20");} else if( x == 30 ){ System.out.print("Value of X is 30");} else{ System.out.print("This is else statement"); }

Exercise What is the output for the following code lines if hours=35 and hours=60: a) if (hours <= 40) System.out.println(“No overtime” ); System.out.println(“ You must work over 40 hours for overtime\n”); System.out.println(“Continue” ); B) if (hours <= 40) { cout << “No overtime” << endl; cout << "You must work over 40 hours for overtime.\n"; } cout << “Continue” << endl;