1 TDBA66, VT-03, Lecture - Ch. 4 Control structures Conditions (expr1 op expr2 op expr3..) Choise (if statements) Repetition (loops) see Chap. 5.

Slides:



Advertisements
Similar presentations
Decisions If statements in C.
Advertisements

CSE 1301 Lecture 5B Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
1 TDBA66, VT-03, Lecture - Ch. 5 Repetition Loops are common in programming (computers can do boring things over and over again) Type of loopWhen usedC-structures.
1 CS 201 Selection Structures (1) Debzani Deb. 2 Error in slide: scanf Function scanf(“%lf”, &miles); function name function arguments format string variable.
CS 117 Spring 2002 Review for Exam 2 March 6, 2002 open book, 1 page of notes.
Introduction to Computers and Programming Class 6 Introduction to C Professor Avi Rosenfeld.
Chapter 4 Making Decisions
C++ for Engineers and Scientists Third Edition
Copyright © 2012 Pearson Education, Inc. Chapter 3 Control Structures: Selection.
1 ICS103 Programming in C Lecture 6: Selection Structures.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
CPS 125: Digital Computation and Programming Selection Structures: if and switch Statements.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
CPS120: Introduction to Computer Science Decision Making in Programs.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Selection Structures: if and switch Statements Problem Solving,
Chapter 4 Selection Structures: Making Decisions.
A. Abhari CPS1251 Topic 4: Control structures Control Structures Overview Conditions if Statement Nested if Statements switch Statement Common Programming.
1 Lecture 5: Selection Structures. Outline 2  Control Structures  Conditions  Relational Operators  Logical Operators  if statements  Two-Alternatives.
Flow of Control Part 1: Selection
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
CSC 107 – Programming For Science. The Week’s Goal.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Northeastern University 1 \nnew line \rcarriage return \thorizontal tab \vvertical tab \bbackspace \\backslash \”double quotation mark \’single quotation.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Selection Structures: if and switch Statements. 2 Selection Statements –In this chapter we study statements that allow alternatives to straight sequential.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
CSC Programming for Science Lecture 10: Boolean Expressions & More If ­ Else Statements.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Decision Statements, Short- Circuit Evaluation, Errors.
CPS120: Introduction to Computer Science Decision Making in Programs.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
Control Statements: Part1  if, if…else, switch 1.
Chapter 4 Selection Structures: if and switch Statements Lecture Notes Prepared By: Blaise W. Liffick, PhD Department of Computer Science Millersville.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Chapter 4 : Selection Structures: if and switch statement By Suraya Alias.
Decision making If.. else statement.
Lecture 3 Selection Statements
Chapter 3 Selection Statements
CNG 140 C Programming (Lecture set 3)
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
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.
EGR 2261 Unit 4 Control Structures I: Selection
Engineering Problem Solving with C++, Etter/Ingber
CHAPTER 5: SELECTION STRUCTURES: if and switch STATEMENTS
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Bools & Ifs.
Control Structures – Selection
Lecture 2: Logical Problems with Choices
Flow of Control.
Flow of Control.
3 Control Statements:.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Computer Science Core Concepts
Chapter 4: Control Structures I (Selection)
Flow of Control.
CSC215 Lecture Control Flow.
ICS103: Programming in C 4: Selection Structures
Presentation transcript:

1 TDBA66, VT-03, Lecture - Ch. 4 Control structures Conditions (expr1 op expr2 op expr3..) Choise (if statements) Repetition (loops) see Chap. 5

2 TDBA66, VT-03, Lecture - Ch. 4 Operators (other than aritmetic) Relational operators > = <= (greater than, less than etc.) Equality operators == != (equal, not equal) Logical operators ! && || (not, and, or (inclusive or)) Note –Order of priority (Appendix C) E.g. Relational higher than logical –Symbols: <= must be written like this (no space between < and =)

3 TDBA66, VT-03, Lecture - Ch. 4 Relational operators binary and results in 0 or 1. Integer representation of false and true respectively Ex.1 (a != b) Ex. 2 (a >=0) && (a<100) Note : 3 < j < 5 is not true if j equals 7 (logically), but in C the evaluation of 3 < j < 5 is like (3 < j) < 5 i.e. (3 < j) which is true and the result is 1, in the next step the expression 1 < 5 is evaluated which is true and the result is 1 Correct condition in C is (3<j) && (j<5) but 3<j && j<5 is also correct in C due to priority rules

4 TDBA66, VT-03, Lecture - Ch. 4 Equality operators == and != (equal to and not equal to) Binary and results in the integer 0 or 1 Ex.1 (expr1 == expr2) (Normally expr1 and expr2 of same data type) Important to realize the difference between = and == Syntactically correct if (a = 0) if (a == 0) …but (a = 0) is an assignment with the value 0, (false), while (a == 0) is a comparison that is true if a equals 0.

5 TDBA66, VT-03, Lecture - Ch. 4 Logical operators ! not (almost) && and || or Note : not not s ->  not (not s)  -> s but !!5  !(!5)  -> !(0)  1 Everything ≠ 0 is true

6 TDBA66, VT-03, Lecture - Ch. 4 Short-circut evaluation (lazy evaluation) A condition is evaluated until its result is known Ex. 1 expr1 && expr2 If value of expr1 is false the value of expr2 is not evaluated Ex. 2 expr1 || expr2 If value of expr1 is true the value of expr2 does not matter

7 TDBA66, VT-03, Lecture - Ch. 4 The compound statement Many statements surrounded by { } No semicolon after } Can contain declarations and statements If declarations are present it is called a block The compound statement can be placed anywhere a statement is legal. Normally it is used where C allows one statement but you would like to execute more than one statement Often in if-statements and loops

8 TDBA66, VT-03, Lecture - Ch. 4 if -statements expr is very often a logical expression Figure 4.5 if Statement to Order x and y such that x <= y if (x > y) { /* Switch x and y */ temp = x; /* Store old x in temp */ x = y; /* Store old y in x */ y = temp; } /* Note indentation */ if (expr) statement; if (expr) statement1; else statement2;

9 TDBA66, VT-03, Lecture - Ch. 4 The statements in if-statements might be if-statements (nested if- statements) Ex.1 see page 133 in text book if (x > 0) num_pos = num_pos + 1; else if (x < 0) num_neg++; else /* x equals 0 */ num_zero += 1;

10 TDBA66, VT-03, Lecture - Ch. 4 The dangling-else-problem else is always associated to the nearest previous if-statement that misses an else Ex.1 if (a <= b) if (a == b) printf(”a is equal to b\n”); else /* bad indentation does not fool the compiler */ printf(”a is less than b\n”); Note: no printing if a > b See also page 140 in the text book

11 TDBA66, VT-03, Lecture - Ch. 4 Figure 4.6 Multiple-Alternative if to Calculate Tax /* * Computes the tax due based on a tax table. * Returns the tax due for 0.0 <= salary <= 150,000.00; * returns -1.0 if salary is outside the table range. */ if (salary < 0.0) tax = -1.0; else if (salary < )/* first range */ tax = 0.15 * salary; else if (salary < )/* second range*/ tax = (salary ) * ; else if (salary < )/* third range*/ tax = (salary ) * ; else if (salary < )/* fourth range*/ tax = (salary ) * ; else if (salary <= )/* fifth range*/ tax = (salary ) * ; else tax = -1.0;

12 TDBA66, VT-03, Lecture - Ch. 4 switch -statement Better than nested if-statements in certain cases Controlling expr must be of ”integral-type” (int or char) The values must be unique The statements might be empty switch (controlling expr) { case value1 : statements; case value2 : statements; case value3 : statements; …… default : statements; /* can be excluded */ }

13 TDBA66, VT-03, Lecture - Ch. 4...switch The controlling expr is evaluated If the value of the controlling expr is equal to one of the values in the case labels the execution continues sequentially from there If the controlling value is not found among the case labels execution is continued in the default-alternative (if that is present) Normally the last statement in all case alternatives is break

14 TDBA66, VT-03, Lecture - Ch. 4 Figure 4.10 Example of a Switch-statement with Type char Case Labels switch (sclass) { case ’B’: case ’b’:printf("Battleship\n"); break; case ’C’: case ’c’:printf("Cruiser\n"); break; Case ’D’: case ’d’: printf("Destroyer\n"); break; case ’F’: case ’f’: printf("Frigate\n"); break; default: printf("Unknown ship class %c\n", sclass); }

15 TDBA66, VT-03, Lecture - Ch. 4 Solve programming project 1 in Ch. 4 (page 158) In short: write a program that reads one character and prints different messages depending on the read charcter Read characterInterpretationPrint NNegativeMessage 1 PPositiveMessage 2 BBothMessages 1 and 2 ZNeitherMessage 3 Use simple functions: one to write Message1 and the other one to write Message 2

16 TDBA66, VT-03, Lecture - Ch. 4 peppar:~/c/Ckod>./proj4_1 Type one of the characters P, N, B and Z: P My analysis of sample with code P is Gram-postive: Perform standard tests 1 and 5. Record results in notebook #2 peppar:~/c/Ckod> !!./proj4_1 Type one of the characters P, N, B and Z: n My analysis of sample with code n is Gram-negative: Perform standard tests 2, 3 and 4. Record results in notebook #3 Example runs of proj4_1.c