1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.

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

1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Week 4 Selections This week shows how to use selection statements for more flexible programs. It also describes the various integral types that are available.
Control Flow C and Data Structures Baojian Hua
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
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.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
C++ for Engineers and Scientists Third Edition
Programming Control Flow. Sequential Program S1 S2 S5 S4 S3 int main() { Statement1; Statement2; … StatementN; } Start End.
Selection. Computer Programming 2 Objectives Examine if statement in more detail Study use of switch statement to implement multialternative selections.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Week 3 – Selection Structures UniMAP SemPGT C PROGRAMMING1.
Conditional Statement
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
1 4.8The do/while Repetition Structure The do/while repetition structure –Similar to the while structure –Condition for repetition tested after the body.
UniMAP Sem II-09/10EKT120: Computer Programming1 Week 3 – Selection Structures.
Control Statements Spring Semester 2013Programming and Data Structure1.
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.
CMSC 104, Version 8/061L11Relational&LogicalOps.ppt Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else.
1 If statement and relational operators –, >=, ==, != Finding min/max of 2 numbers Finding the min of 3 numbers Forming Complex relational expressions.
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.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
IIT Kanpur C Course Lecture 3 Aug 31, Rishi Kumar, Final year BT-MT, CSE.
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
Week 4 Program Control Structure
1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.
Control Statements: Part1  if, if…else, switch 1.
Dr. Sajib Datta Jan 23,  A precedence for each operator ◦ Multiplication and division have a higher precedence than addition and subtraction.
Making Decisions in c. 1.if statement Imagine that you could translate a statement such as “If it is not raining, then I will go swimming” into the C.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
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.
Branching statements.
Chapter 4 – C Program Control
‘C’ Programming Khalid Jamal.
The if…else Selection Statement
Selection (also known as Branching) Jumail Bin Taliba by
Decisions Chapter 4.
Week 3 C Program Structures (Selection Structures)
CHAPTER 4 Selection CSEG1003 Introduction to Computing
Week 3 – Selection Structures
DKT121: Fundamental of Computer Programming
Programming Fundamentals
Flow of Control.
Lecture 2: Logical Problems with Choices
- Additional C Statements
CS1100 Computational Engineering
Chapter 5 Decision Making and Branching
Structured Program
Chapter 4 - Program Control
Relational, Logical, and Equality Operators
Chapter 4: Control Structures I (Selection)
Week 3 – Program Control Structure
Lecture 9: Implementing Complex Logic
Presentation transcript:

1 Conditional Statement

2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition Also called Branching How do we specify conditions?  Using expressions non-zero value means condition is true value 0 means condition is false  Usually logical expressions, but can be any expression The value of the expression will be used

3 Branching: if Statement if (expression) statement; if (expression) { Block of statements; }

4 Branching: if Statement if (expression) statement; if (expression) { Block of statements; } The condition to be tested is any expression enclosed in parentheses. The expression is evaluated, and if its value is non-zero, the statement is executed.

5 true false marks >= 40 print “Passed” print “Good luck”

6 true false marks >= 40 print “Passed” print “Good luck” A decision can be made on any expression. zero - false nonzero - true

7 true false marks >= 40 print “Passed” print “Good luck” A decision can be made on any expression. zero - false nonzero - true if (marks >= 40) { printf(“Passed \n”); printf(“Good luck\n”); } printf (“End\n”) ;

8 Branching: if-else Statement if (expression) { Block of statements; } else { Block of statements; } if (expression) { Block of statements; } else if (expression) { Block of statements; } else { Block of statements; }

9 Grade Computation void main() { int marks; scanf(“%d”, &marks); if (marks >= 80) printf (”A”) ; else if (marks >= 70 && marks <80) printf (”B”) ; else if (marks >= 60 && marks <70) printf (”C”) ; else printf (”Failed”) ; }

10 Find the larger of two numbers START STOP READ X, Y OUTPUT Y ISX>Y? OUTPUT X STOP YESNO

11 Find the larger of two numbers START STOP READ X, Y OUTPUT Y ISX>Y? OUTPUT X STOP YESNO void main () { int x, y; scanf (“%d%d”, &x, &y) ; if (x > y) printf (“%d\n”, x); else printf (“%d\n”, y); }

12 Largest of three numbersSTART READ X, Y, Z IS Max > Z? IS X > Y? Max = X Max = Y OUTPUT Max OUTPUT Z STOPSTOP YES YES NO NO

13 START READ X, Y, Z IS Max > Z? IS X > Y? Max = X Max = Y OUTPUT Max OUTPUT Z STOPSTOP YES YES NO NO

14 START READ X, Y, Z IS Max > Z? IS X > Y? Max = X Max = Y OUTPUT Max OUTPUT Z STOPSTOP YES YES NO NO void main () { int x, y, z, max; scanf (“%d%d%d”,&x,&y,&z); if (x > y) max = x; else max = y; if (max > z) printf (“%d”, max) ; else printf (“%d”,z); }

15 Another version void main() { int a,b,c; scanf (“%d%d%d”, &a, &b, &c); if ((a >= b) && (a >= c)) printf (“\n The largest number is: %d”, a); if ((b >= a) && (b >= c)) printf (“\n The largest number is: %d”, b); if ((c >= a) && (c >= b)) printf (“\n The largest number is: %d”, c); }

16 Confusing Equality (==) and Assignment (=) Operators Dangerous error  Does not ordinarily cause syntax errors  Any expression that produces a value can be used in control structures  Nonzero values are true, zero values are false Example: if ( payCode = 4 ) printf( "You get a bonus!\n" ); WRONG! Will always print the line

17 Nesting of if-else Structures It is possible to nest if-else statements, one within another if (exp1) { if (exp2) stmta else stmtb } if (exp1) { if (exp2) stmta } else stmtb

18 Nesting of if-else Structures It is possible to nest if-else statements, one within another All “if” statements may not be having the “else” part  Confusion?? Rule to be remembered:  An “else” clause is associated with the closest preceding unmatched “if”

19 Dangling else problem if (exp1) if (exp2) stmta else stmtb if (exp1) { if (exp2) stmta else stmtb } OR if (exp1) { if (exp2) stmta } else stmtb ? Which one is the correct interpretation? Give braces explicitly in your programs to match the else with the correct if to remove any ambiguity

20 Dangling else problem if (exp1) if (exp2) stmta else stmtb if (exp1) { if (exp2) stmta else stmtb } OR if (exp1) { if (exp2) stmta } else stmtb ? Which one is the correct interpretation? Give braces explicitly in your programs to match the else with the correct if to remove any ambiguity

21 More Examples if e1 s1 else if e2 s2 if e1 s1 else if e2 s2 else s3 if e1 if e2 s1 else s2 else s3 ?

22 Answers if e1 s1 else if e2 s2else { if e2 s2 } if e1 s1if e1 s1 else if e2 s2else { if e2 s2 else s3else s3 } if e1 if e2 s1 if e1 { if e2 s1 else s2 else s2 } else s3

23 The switch Statement An alternative to writing lots of if-else in some special cases This causes a particular group of statements to be chosen from several available groups based on equality tests only Uses switch statement and case labels

24 Syntax switch (expression) { case const-expr-1: S-1 case const-expr-2: S-2 : case const-expr-m: S-m default: S } expression is any integer-valued expression const-expr-1, const-expr-2,…are any constant of integer-valued expressions  Values must be distinct S-1, S-2, …,S-m, S are statements/compound statements Default is optional, and can come anywhere (not necessarily at the end as shown)

25 Behavior of switch expression is first evaluated It is then compared with const-expr-1, const- expr-2,…for equality in order If it matches any one, all statements from that point till the end of the switch are executed (including statements for default, if present)  Use break statements if you do not want this (see example) Statements corresponding to default, if present, are executed if no other expression matches

26 Example int x; scanf(“%d”, &x); switch (x) { case 1: printf(“One\n”); case 2: printf(“Two\n”); default: printf(“Not one or two\n”); }; If x = 1 is entered, this will print One Two Not one or two Not what we want switch-1.c

27 Correct Program int x; scanf(“%d”, &x); switch (x) { case 1: printf(“One\n”); break; case 2: printf(“Two\n”); break; default: printf(“Not one or two\n”); }; If x = 1 is entered, this will print One switch-2.c

28 The break Statement Used to exit from a switch or terminate from a loop With respect to “switch”, the “break” statement causes a transfer of control out of the entire “switch” statement, to the first statement following the “switch” statement Can be used with other statements also …(will show later)

29 The Conditional Operator ?: This makes use of an expression that is either non- 0 or 0. An appropriate value is selected, depending on the value of the expression Example: instead of writing if (balance > 5000) interest = balance * 0.2; else interest = balance * 0.1; We can just write interest = (balance > 5000) ? balance * 0.2 : balance * 0.1;

30 More Examples if (((a >10) && (b < 5)) x = a + b; else x = 0; x = ((a > 10) && (b < 5)) ? a + b : 0 if (marks >= 60) printf(“Passed \n”); else printf(“Failed \n”); (marks >= 60) ? printf(“Passed \n”) : printf(“Failed \n”);