UNIT II Decision Making And Branching Decision Making And Looping

Slides:



Advertisements
Similar presentations
Flow of Control Chapter 3.
Advertisements

Control Statements. Define the way of flow in which the program statements should take place. Control Statements Implement decisions and repetitions.
Fundamental of C programming
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.
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.
Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
1 Control Structures (and user input). 2 Flow of Control The order statements are executed is called flow of control By default, statements in a method.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Sanjay Goel, School of Business, University at Albany, SUNY 1 MSI 692: Special Topics in Information Technology Lecture 2 Sanjay Goel University at Albany,
true (any other value but zero) false (zero) expression Statement 2
Arrays, Conditionals & Loops in Java. Arrays in Java Arrays in Java, are a way to store collections of items into a single unit. The array has some number.
Introduction to Programming G51PRG University of Nottingham Revision 2 Essam Eliwa.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
TODAY’S LECTURE Review Chapter 2 Go over exercises.
JAVA Control Statement.
L EC. 03: C ONTROL STATEMENTS Fall Java Programming.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 C++ Control Statements ~ Selection and Iteration.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
DiagrammaticRepresentation Iteration Construct False True Condition Exit from Statement (s) loop Sequence construct Selection construct Statement 1 Statement.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
C# Programming Fundamentals Control Flow Jim Warren, COMPSCI 280 S Enterprise Software Development.
CPS120: Introduction to Computer Science Decision Making in Programs.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
Expressions An expression is a series of variables, operators, and method calls (constructed according to the syntax of the language) that evaluates to.
Controlling Execution Dong Shao, Nanjing Unviersity.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
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.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
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.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
J AVA P ROGRAMMING 2 C H 03: C ONTROL STATEMENTS if, for loop (review) switch, while, do while break, continue Fall Java Programming.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Control Flow Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
Chapter : 9 Control Statements Control Flow: A control flow Statement regulates the order in which statements get executed. F Flow-order in which the computer.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
We have to discuss following:-  Statements  Statement flow control  Selection statement  Iteration statement.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
Asstt. Prof Mandeep Kaur Computer Dept. TOPIC: Control Statements in C language.
Control Flow Statements
CSI 3125, Preliminaries, page 1 Control Statements.
CPS120: Introduction to Computer Science Decision Making in Programs.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops
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’ Programming Khalid Jamal.
Statements (6 of 6) A statement causes an action to be performed by the program. It translates directly into one or more executable computer instructions.
Control Structures.
Programming Fundamentals
Expressions and Control Flow in JavaScript
Control Structures.
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
FLOW OF CONTROL.
Conditionals.
PROGRAM FLOWCHART Selection Statements.
Presentation transcript:

UNIT II Decision Making And Branching Decision Making And Looping

Decision Making And Branching

Introduction Generally a program executes it's statements from beginning to end. But not many programs execute all their statements in strict order from beginning to end. Most programs decide what to do in response to changing circumstances.

In a program statements may be executed sequentially, selectively or iteratively. When a program breaks the sequential flow and jumps to another part of the code, it is called selection or branching. When the branching is based on a particular condition, it is known as conditional branching. If branching takes place without any condition, it is known as unconditional branching. Java language supports two types of selections statements: if and switch. In addition, in certain circumstances '?:' operator can be used as an alternative to if statements.

DECISION MAKING WITH IF STATEMENT An if statement tests a particular condition; if the condition evaluates to true , a course of action is followed i.e. a statement or set of statements is executed . Otherwise (if the condition evaluates to false),the course-of-action is ignored. The if statement may be implemented in different forms depending on the complexity of conditions to be tested. Simple if statement if....else statement Nested if....else statement else if ladder

SIMPLE IF STATEMENT The syntax of the if statement is if(expression) statements; Where a statement may consist of a single statement , a compound statement, or nothing. The expression must be enclosed in parentheses. If the expression evaluates to true, the statement is executed, otherwise ignored.

Example: int a,b; if(a<=b)    a=0;

THE IF....ELSE STATEMENT This form of if allows either-or condition by providing an else clause. The syntax of the if-else statement is the following: if(expression) statement-1; else statement-2; If the expression evaluates to true i.e., a non-zero value, the statement-1 is executed, otherwise statement-2 is executed. The statement-1 and statement-2 can be a single statement, or a compound statement, or a null statement.

Example: int a,b; if(a<=b)    a=0; else    b=0;

NESTED IF....ELSE STATEMENT: A Nested if is an if that has another if in it's 'if's' body or in it's else's body. if(expression1) { if(expression2) statement-1; else statement-2; } else body of else; if(expression1) { if(expression2) { statement-1; } else { statement-2; } else { statement-3; } statement-X;

THE ELSE-IF LADDER A common programming construct in the java is the if-else-if ladder , which is often also called the if-else-if staircase because of it's appearance. Syntax of if-else-if ladder if(expression1)   statement 1; else if(expression2)   statement 2; else if(expression3)   statement 3;     : else   statement n;

The expressions are evaluated from top downward The expressions are evaluated from top downward. As soon as expression evaluates to true , the statement associated with it is executed and the rest of the ladder is bypassed. If none of the expression are true, the final else gets executed. If the final else is missing , no action takes place if all other conditions are false.

THE SWITCH STATEMENT Java provides a multiple branch selection statement known as switch. This selection statement successively tests the value of an expression against a list of integer or character constants. When a match is found, the statements associated with that constant are executed. The syntax of switch switch(expression) { case value1:    Code segment 1 case value2:    Code segment 2    ... case valuen:    Code segment n default:    default Code segment }

A switch statement is used for multiple way selection that will branch to different code segments based on the value of a variable or an expression . The optional default label is used to specify the code segment to be executed when the value of the variable or expression does not match with any of the case values. if there is no break statement as the last statement in the code segment for a certain case, the execution will continue on into the code segment for the next case clause without checking the case value.

THE ?: OPERATOR Java has an operator that can be used as an alternative to if statement. You are familiar with this operator, the conditional operator ?: This operator can be used to replace if-else statements of the general form:      if (expression 1)         expression 2;      else          expression 3; The above form of if can be alternatively written using ?: as follows:        expression 1? expression 2: expression 3

Decision Making And Looping INTRODUCTION The iteration statements allow a set of instructions to be performed repeatedly until a certain condition is fulfilled. The iteration statements are also called loops or looping statements. Java provides three kinds of loops: While loop do-while loop for loop All three constructs of java repeat a set of statements as long as a specified condition remains true. The specified condition is generally referred to as a loop control. For all three loop statements, a true condition is any nonzero value. A zero value indicates a false condition.

THE WHILE STATEMENT The most simple and general looping structure available in java is the while statement. while loop is an entry controlled Loop. The condition is checked at the beginning. The syntax of while loop is:    while(condition)    {       // loop-body     } Where the loop body may contain a single statement, a compound statement or an empty statement. The loop iterates while the condition evaluates to true. When the expression becomes false, the program control passes to the line after the loop body code.

//program for while loop class whiletest  {     public static void main(String args[])       {          int n=10;          while(n>0)          {            System.out.println("tick", +n);            n--;           }        } }

THE DO STATEMENT Unlike the while loop, the do-while is an exit-controlled loop i.e. it evaluates its test-condition at the bottom of the loop after executing it's loop-body statements. This means that a do-while loop always executes at least once. The syntax of the do-while loop is:     do     {          loop-body;      } while(condition);

Example:   Class doWhileCheck   {      Public static void main (String args[])       {          int n=10;           do             {                System.out.println("tick" +n);                 n--;             }              while(n>0);         }    }

THE FOR STATEMENT The for loop is the easiest to understand of the java loops. All its loop-control elements are gathered in one place (on the top of the loop) The Syntax of the for loop statement is: for( initialization expression(s); test condition; update expression) { loop-body; }

//program showing usage of for loop class forTest {    public static void main(String args[])  {     int i;     for( i=1; i<=10; i++)      System.out.println(i);  } }