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.

Slides:



Advertisements
Similar presentations
Control Statements. Define the way of flow in which the program statements should take place. Control Statements Implement decisions and repetitions.
Advertisements

Decision Making EE2372 Software Design I Dr. Gerardo Rosiles.
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.
Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
1 Lecture 8:Control Structures I (Selection) (cont.) Introduction to Computer Science Spring 2006.
true (any other value but zero) false (zero) expression Statement 2
1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Control Statements.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
UNIT II Decision Making And Branching Decision Making And Looping
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 2.
UniMAP Sem II-09/10EKT120: Computer Programming1 Week 3 – Selection Structures.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
1 Lecture 5: Selection Structures. Outline 2  Control Structures  Conditions  Relational Operators  Logical Operators  if statements  Two-Alternatives.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Control Statements (Decision Making)
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
C Programming Lecture 7 : Control Structures. Control Structures Conditional statement : if, switch Determine a block of statements to execute depending.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Week 4 Program Control Structure
IT CS 200: C ONDITION Lect. Napat Amphaiphan. T HE ABILITY TO CONTROL THE FLOW OF YOUR PROGRAM, LETTING IT MAKE DECISIONS ON WHAT CODE TO EXECUTE 2.
Control statements Mostafa Abdallah
Asstt. Prof Mandeep Kaur Computer Dept. TOPIC: Control Statements in C language.
Control Flow Statements
Decision Statements, Short- Circuit Evaluation, Errors.
Decision Making and Branching
CS115 FALL Senem KUMOVA-METİN1 CHAPTER 4 FLOW OF CONTROL-1 Operators, If and switch statements.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Dr. Sajib Datta Jan 23,  A precedence for each operator ◦ Multiplication and division have a higher precedence than addition and subtraction.
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)
 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.
Dr. Sajib Datta Sep 3,  A new operator used in C is modulus operator: %  % only used for integers, not floating-point  Gives the integer.
BY ILTAF MEHDI(MCS,MCSE, CCNA) 1. INSTRUCTOR: ILTAF MEHDI (MCS, MCSE, CCNA, Web Developer) BY ILTAF MEHDI(MCS,MCSE, CCNA) 2 Programming Fundamentals Chapter.
THE DECISIONS CONTROL STRUCTURE! CHAPTER 2. Transfer of control statement: o The statement of computer program are executed one after the other in the.
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Decisions Chapter 4.
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.
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Condition Statements.
DKT121: Fundamental of Computer Programming
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
JavaScript: Control Statements.
CSE1320 INTERMEDIATE PROGRAMMING Operators+Conditionals+Loop
Control Structures.
IF if (condition) { Process… }
Chapter 4 - Program Control
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Week 3 – Program Control Structure
PROGRAM FLOWCHART Selection Statements.
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.
CprE 185: Intro to Problem Solving (using C)
CSC215 Lecture Control Flow.
Presentation transcript:

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 decision making statements are, – simple if statement – if…else statement – nested if – switch statement

Simple if statement The if statement is a powerful decision making statement and is used to control the flow of execution of statements. It is basically two ways decision statement is of the form if (test condition) It allows a computer to evaluate the expression first and depending upon whether the value of condition is true or false. It will transfer the control to a particular statement

Syntax The general form of simple if statement is if (condition) { statement-block } Example: if(x>40) { printf(“x is greater”); }

Flow chart for if statement

Rules The brackets around the test condition are must. The test condition must be relational or logical expression. Statement block is called body of the if statement and it contains one or more statements. The opening and closing brackets {} are must if the statement block contains more than one statement. else optional

If else statement If else statement is used to execute one group of statements, if the test condition is true or other group if the test condition is false.

Syntax if (condition) { true block statement } else { false block statement }

Example #include main() { int a,b; printf(“enter the value of a:”); scanf(“%d”,&a); printf(“enter the value of b:”); scanf(“%d”,&b); if(a>40) { printf(“ a is greater”); } else { printf(“b is greater”); }

Flow chart for if else

Rules The brackets around the test condition are must. The test condition must be relational or logical expression. Statement block is called body of the if statement. It can contains one or more statements. The opening and closing brackets {} are must if the statement block contains more than one statement. else optional. No statements other than the statements in the statement blocks are allowed between if…else.

Nested if If within if is called nested if and it is used when we have multiple conditions to check and when any if condition contains another if statement then that is called ‘nested if’. If the external condition is true, then the internal if condition or condition are executed and if the condition is false then the else portion is executed of the external if statement

Syntax if (condition) { } if (condition) { ……. } else if (condition) { ……. } else if (condition) { ……. } else { ……. } } else { ……. }

Example if (num < 0) { result = abs(num); printf("%d", result); } else { if (num == 0) printf("the number is zero"); else { result = num * num; printf("%d", result); } }

Flow chart for nested if

Rules The baby IF may be in the TRUE part or the FALSE part of the mother IF. Or, both the TRUE part and the FALSE part of the mother IF may contain baby IFs. Each ELSE belongs to the NEAREST IF, whether mother or baby. Therefore, we need to study carefully what each else means. Be very careful opening and closing the braces of each IF. Do you really want to finish this IF here? What happens if this IF is finished and some other statement comes after the brace: We need to trace what we are doing very carefully.

Switch statement C has a built-in multiway decision statement known as a switch. The switch statement tests the value of a given variable against a list of case values and when a match is found a block of statements associated with that case is executed. The break statement at the end of each block signals the end of a particular case and cause exit from switch statement transferring the control to the statement-x The default is optional case. It will be executed if the value of the expression does not match with any of the case values.

Syntax switch (expression) { case value-1; block-1 break; case value-2; block-2 break; ……….. default: default-block break; } statement-x;

Example switch(x) { case 'A': Print("CASE A"); break; case 'B': case 'C': Print("CASE B or C"); break; default: Print("NOT A, B or C"); break; }

Flow chart for switch statement

Rules The expression should be placed in parentheses. The value of the expression should be an integer. The body of the switch statement should be enclosed within {} brackets. Case label should terminate with a colon. Break statement is a must and causes immediate exit from switch statement. If it is not included the statements below the matched case will be executed.

End Thank you