Control Structures.

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
true (any other value but zero) false (zero) expression Statement 2
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
ECE122 L8: More Conditional Statements February 7, 2007 ECE 122 Engineering Problem Solving with Java Lecture 8 More Conditional Statements.
Logical Operators and Conditional statements
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Switch Statements Comparing Exact Values. 2 The Switch Statement The switch statement provides another way to decide which statement to execute next The.
UNIT II Decision Making And Branching Decision Making And Looping
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.
CPS120: Introduction to Computer Science Decision Making in Programs.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
CSCI 1100/1202 January 28, The switch Statement The switch statement provides another means to decide which statement to execute next The switch.
1 Data Comparisons and Switch Data Comparisons Switch Reading for this class: L&L 5.3,
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Switch Statements Comparing Exact Values. The Switch Statement: Syntax The switch statement provides another way to decide which statement to execute.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
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.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Control statements Mostafa Abdallah
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Control Flow Statements
CSI 3125, Preliminaries, page 1 Control Statements.
Decision Statements, Short- Circuit Evaluation, Errors.
CPS120: Introduction to Computer Science Decision Making in Programs.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Switch Statements Comparing Exact Values
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
‘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.
C-Language Lecture By B.S.S.Tejesh, S.Neeraja
Chapter 4: Making Decisions.
DKT121: Fundamental of Computer Programming
Programming Fundamentals
The order in which statements are executed is called the flow of control. Most of the time, a running program starts at the first programming statement,
Chapter 4: Making Decisions.
The switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression,
FLOW OF CONTROL.
IF if (condition) { Process… }
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Visual Basic – Decision Statements
Conditionals.
CprE 185: Intro to Problem Solving (using C)
Program Flow.
PROGRAM FLOWCHART Selection Statements.
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
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.
Comparing Data & the ‘switch’ Statement
Comparing Data & the ‘switch’ Statement
CprE 185: Intro to Problem Solving (using C)
Department of Computer Science
CSC215 Lecture Control Flow.
CprE 185: Intro to Problem Solving (using C)
Presentation transcript:

Control Structures

Control Structure Control structure is divided into three parts: Selection statement Iteration statement Jumps in statement

Selection Statement if statement switch statement Selection statement is also called as Decision making statements because it provides the decision making capabilities to the statements. In selection statement, there are two types: if statement switch statement These two statements allows you to control the flow of a program with their conditions.

Decision Making with if Statements The “if statement” is also called as conditional branch statement. It is used to control program execution through two paths. It allows the computer to evaluate the expression first and then depending on whether the value of the expression is ‘true’ or ‘false’, it transfers the control to particular statement. Entry Test Condition ? false If (test expression) true

if Statement The if-statement may be implemented in different forms depending on the complexity of condition to be tested. Simple if Statement If…else Statement Nested if...else Statement else if ladder

Simple if Statement The general form of a simple if statement is… The ‘Statement-block’ may be a single statement or a group of statements. If the test expression is true, the statement-block will be executed; Otherwise the statement-block will be skipped and the execution will jump to the statement-x. if (test expression) { statement-block; } statement-x;

The If…Else Statement The if…else statement is an extension of the simple if statement. The ‘Statement-block’ may be a single statement or a group of statements. If the test expression is true, the true-statement-block will be executed; otherwise the false-statement-block will be executed and the execution will jump to the statement-a. If (condition) { true - Statement block; } else false - Statement block; Statement-a;

Nesting of If…Else Statement When the series of decisions are involved, we may have to use more than one if…else statement in nested form. If (condition1) { If (condition2) Statement block1; } else Statement block2; Statement block3; Statement 4;

If-Else-if Ladder if(condition-1) {//if condition-1 is true } else if (condition-2) {//if condition-2 is true } else if (condition-3) {//if condition-n is true } . . else if (condition-n) {//if condition-n is true } else { //if none of the conditions are true.} Statements which will be executed always

The switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression, then attempts to match the result to one of several possible cases Each case contains one value (a constant) and a list of statements The flow of control transfers to statement associated with the first case value that matches

The switch Statement The general syntax of a switch statement is: and case are reserved words switch ( expression ) { case value1 : statement-list1 case value2 : statement-list2 case value3 : statement-list3 case ... } If expression matches value2, control jumps to here

The switch Statement Often a break statement is used as the last statement in each case's statement list A break statement causes control to transfer to the end of the switch statement If a break statement is not used, the flow of control will continue into the next case Sometimes this may be appropriate, but often we want to execute only the statements associated with one case

The switch Statement switch (option) { case 'A': aCount++; break; An example of a switch statement: switch (option) { case 'A': aCount++; break; case 'B': bCount++; case 'C': cCount++; }

The switch Statement A switch statement can have an optional default case The default case has no associated value and simply uses the reserved word default If the default case is present, control will transfer to it if no other case value matches If there is no default case, and no other value matches, control falls through to the statement after the switch