PROGRAM FLOWCHART Selection Statements.

Slides:



Advertisements
Similar presentations
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
Advertisements

1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
8-May-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Introduction to Control Statements Presented by: Parminder Singh BCA 5 th Sem. [ Batch] PCTE, Ludhiana 5/12/ Control Statements.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Introduction to Computers and Programming Lecture 6 Professor: Evan Korth New York University.
Additional control structures. The if-else statement The if-else statement chooses which of two statements to execute The if-else statement has the form:
16-Jun-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
C++ for Engineers and Scientists Third Edition
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.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
UNIT II Decision Making And Branching Decision Making And Looping
Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.
Flow of Control Java Programming Mrs. C. Furman January 5, 2009.
Conditional Statements While writing a program, there may be a situation when you need to adopt one path out of the given two paths. So you need to make.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Java Programming, Second Edition Chapter Five Input and Selection.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 7 Decision Making : selection statements.
Java Programming: From The ground Up  Chapter 4 Selection and Decision: if Statements.
Visual C# 2005 Decision Structures. Visual C# Objectives Understand decision making Learn how to make decisions using the if statement Learn how.
Jaeki Song ISQS6337 JAVA Lecture 04 Control Structure - Selection, and Repetition -
Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
Decision making statements. Decision making statements are used to skip or to execute a group of statements based on the result of some condition. The.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
1-Dec-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 The switch Statement The switch statement provides another way.
Java Programming Fifth Edition Chapter 5 Making Decisions.
Chapter 5: Making Decisions. Objectives Plan decision-making logic Make decisions with the if and if…else structures Use multiple statements in if and.
Control Flow Statements
CSI 3125, Preliminaries, page 1 Control Statements.
Decision Statements, Short- Circuit Evaluation, Errors.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING Switch Statement.
Chapter 6 Conditionals. Learning Java through Alice © Daly and Wrigley Objectives List relational operators. List logical operators. Use the hierarchy.
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.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Introduction to Control Statements IT108 George Mason University.
Control Structures- Decisions. Smart Computers Computer programs can be written to make computers seem smart Making computers smart is based on decision.
Branching statements.
Java Programming Fifth Edition
Decision Making in C.
Selection (also known as Branching) Jumail Bin Taliba by
Chapter 6 Conditionals.
Selections Java.
The switch Statement, and Introduction to Looping
Java Programming: From The ground Up
Introduction to programming in java
Flow of Control.
JavaScript: Control Statements.
Control Structures.
IF if (condition) { Process… }
Outline Altering flow of control Boolean expressions
Chapter 3:Decision Structures
Control Structures: Selection Statement
Control Structure Chapter 3.
Control Structures: Selection Statement
Selection Statements August 22, 2019 ICS102: The course.
Chapter 6 Conditionals.
Presentation transcript:

PROGRAM FLOWCHART Selection Statements

Selection Statements Selection statements allows our program to choose different paths of execution based on the outcome of an expression or the state of the variable. The expression or the value upon which a decision is made is always a boolean value(true or false). 2 kinds of selection statements if or if…else switch

The if structure A single if statement is sometimes called a single-alternate if because we only perform an action based on one alternative. syntax: if( condition ) statement; or if(condition ){ statement1; statement2; }

Example: int grade = 68; if( grade > 60 ) System.out.println("Congratulations!"); ------------------------------------------------------------------------------------ int grade = 68; if( grade > 60 ){ System.out.println("Congratulations!"); System.out.println("You passed!"); }

The if…else structure The if…else structure is a dual-alternate if, which requires two options for the next course of action. It provides the mechanism to perform one action when a boolean expression evaluates as true and performs another action when the boolean expression evaluates as false

syntax: if( boolean_expression ){ statement1; statement2; . . . } else{ statement3; statement4;

Example: int grade = 68; if( grade > 60 ) System.out.println("Congratulations!"); else System.out.println("Sorry you failed"); ---------------------------------------------------------------------------------------------- int grade = 68; if( grade > 60 ){ System.out.println("Congratulations!"); System.out.println("You passed!"); } else{ System.out.println("Sorry you failed");

Nested if…else Structure Example: | | | syntax: if( condition 1 ) statement1; else if( condition 2 ) statement2; else statement3; int grade = 68; if( grade > 90 ){ System.out.println("Very good!"); } else if( grade > 60 ){ else{ System.out.println("Sorry you failed");

Common Errors The condition inside the if-statement does not evaluate to a boolean value. For example, //WRONG int number = 0; if( number ){ //some statements here } The variable number does not hold a boolean value. 2. Writing elseif instead of else if.

3. Using = instead of == for comparison. For example, //WRONG int number = 0; if( number = 0 ){ //some statements here } This should be written as, //CORRECT if( number == 0 ){

Example: public class Grade { public static void main( String[] args ) { double grade = 92.0; if( grade >= 90 ){ System.out.println( "Excellent!" ); } else if( (grade < 90) && (grade >= 80)){ System.out.println("Good job!" ); else if( (grade < 80) && (grade >= 60)){ System.out.println("Study harder!" ); else{ System.out.println("Sorry, you failed.");

The switch structure The switch statement is useful when we need to test a single variable against a series of exact integer or character values. keyword switch starts the structures and is followed immediately by a test expression enclosed in parenthesis keyword case is followed by one of the possible values for the test expression and a colon keyword break optionally terminates values of the test expression and a colon keyword default optionally used to prior to any action that should occur if the test variable does not match any case

syntax: switch( switch_expression ){ case case_selector1: statement1;// statement2;//block 1 break; case case_selector2: statement2;//block 2 : default: statement2;//block n }

Example: public class Grade { public static void main( String[] args ) { int grade = 92; switch(grade){ case 100: System.out.println( "Excellent!" ); break; case 90: System.out.println("Good job!" ); case 80: System.out.println("Study harder!" ); default: System.out.println("Sorry, you failed."); }