The if StatementtMyn1 The if Statement The basic if statement allows your program to execute a single statement, or a block of statements enclosed between.

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
Control Structures Corresponds with Chapters 3 and 4.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
CSCI S-1 Section 5. Deadlines for Problem Set 3 Part A – Friday, July 10, 17:00 EST Parts B – Tuesday, July 14, 17:00 EST Getting the code examples from.
1 Repetition structures Overview while statement for statement do while statement.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
CSCI1402: Lecture 1 Week 6 Dr. David A. Elizondo Centre for Computational Intelligence School of Computing Office: Gateway 6.61
Making Choices (Chap 3) If you wish to defrost, press the defrost button; otherwise press the full power button. Let the dough rise in a warm place until.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Classes, methods, and conditional statements We’re past the basics. These are the roots.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops III Lecture 19, Wed Mar
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Chapter 7: User-Defined Methods
CS305j Introduction to ComputingNested For Loops 1 Topic 6 Nested for Loops "Complexity has and will maintain a strong fascination for many people. It.
1 Repetition structures Overview while statement for statement do while statement.
CSCI1402: Lecture 2 Week 6 Dr. David A. Elizondo Centre for Computational Intelligence School of Computing Office: Gateway 6.61
Nested conditional statements. Previously discussed Conditional statements discussed so far: Syntax of the if-statement: if-statement if-else-statement.
Block Scope By Greg Butler Purpose The purpose of this presentation is to familiarize the student with the concept of variable scope, as it relates to.
Introduction to Programming G51PRG University of Nottingham Revision 2 Essam Eliwa.
JAVA Control Statement.
L EC. 03: C ONTROL STATEMENTS Fall Java Programming.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Introduction to Java Thanks to Dan Lunney (SHS). Java Basics File names The “main” method Output to screen Escape Sequence – Special Characters format()
CSC204 – Programming I Lecture 4 August 28, 2002.
Control Structures if else do while continue break switch case return for.
The switch StatementtMyn1 The switch Statement Sometimes there can be a multiple-choice situation, in which you need to execute a particular set of statements.
CPS120 Introduction to Computer Science Iteration (Looping)
Chapter 5 Loops.
Style Guidelines. Why do we need style?  Good programming style helps promote the readability, clarity and comprehensibility of your code.
The scope of local variables. Murphy's Law The famous Murphy's Law says: Anything that can possibly go wrong, does. (Wikipedia page on Murphy's Law:
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Logical OperatorstMyn1 Logical Operators Using logical operators, we can combine a series of comparisons into a single expression. if(letter>=‘A’ && letter
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
August 6, 2009 Data Types, Variables, and Arrays.
Sahar Mosleh California State University San MarcosPage 1 Program Control Statement.
J AVA P ROGRAMMING 2 C H 03: C ONTROL STATEMENTS if, for loop (review) switch, while, do while break, continue Fall Java Programming.
CPS120 Introduction to Computer Science Iteration (Looping)
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
The for loop.
CSCI S-1 Section 4. Deadlines for Homework 2 Problems 1-8 in Parts C and D – Friday, July 3, 17:00 EST Parts E and F – Tuesday, July 7, 17:00 EST.
Loops and Logic. Making Decisions Conditional operator Switch Statement Variable scope Loops Assertions.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING While Loop.
Input Characters from the Keyboard tMyn1 Input Characters from the Keyboard We have been using console output, but not console (keyboard) input. The main.
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
The if-else StatementtMyn1 The if-else Statement It might be that we want to execute a particular statement or block of statements only when the condition.
Lecture 4b Repeating With Loops
Java Programming: From Problem Analysis to Program Design, 3e Chapter 7 User-Defined Methods.
The Lifetime of a Variable
Lecture 5: Some more Java!
Decision statements. - They can use logic to arrive at desired results
חלק ה שימוש במציין שלם לערך תווי
In this class, we will cover:
Tonga Institute of Higher Education
class PrintOnetoTen { public static void main(String args[]) {
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Scope of variables class scopeofvars {
while while (condition) { statements }
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
In this class, we will cover:
There many situations comes in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations.
Suggested self-checks:
Local variables and how to recognize them
statement. Another decision statement, , creates branches for multi-
Presentation transcript:

The if StatementtMyn1 The if Statement The basic if statement allows your program to execute a single statement, or a block of statements enclosed between braces, if a given condition is true. This is illustrated in the Figure 1.

The if StatementtMyn2 condition is true? yes no Statement or Block of Statements Next Statement if(condition) statement; next statement; or if(condition) { statement; … } next statement; Figure 1. The statement or block of statements that follows the if is only executed if condition is true.

The if StatementtMyn3 A simple example of an if statement to test the value of a variable called letter: if(letter==‘A’) System.out.println(“The first capital!“); System.out.println(“This is not part of the if structure!!“); The condition to be tested appears ALWAYS in parentheses!!

The if StatementtMyn4 package ifstatement; public class Main { public static void main(String[] args) throws java.io.IOException { char ch, answer='K'; System.out.println("I am thinking of a letter between A and Z."); System.out.print("Can you guess it: "); ch=(char)System.in.read(); if(ch==answer) System.out.println("***RIGHT***"); }

The if StatementtMyn5 run: I am thinking of a letter between A and Z. Can you guess it: K ***RIGHT*** BUILD SUCCESSFUL (total time: 8 seconds)

The if StatementtMyn6 The statement that is to be executed when the condition in an if statement is true can itself be an if statement. This arrangement is called a nested if. The condition of the inner if is only tested if the condition for the outer if is true. An if that is nested inside another can also contain a nested if.

The if StatementtMyn7 if(letter>=‘A’) { if(letter<=‘Z’) { System.out.println(“You entered an upper case letter”); … } If this is NOT true, we will never visit here!!