1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.

Slides:



Advertisements
Similar presentations
CSE 1301 Lecture 5B Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
BBS514 Structured Programming (Yapısal Programlama)1 Selective Structures.
For loops For loops are controlled by a counter variable. for( c =init_value;c
Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 4 (Conditional Statements) © CPCS
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
true (any other value but zero) false (zero) expression Statement 2
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Logical Operators and Conditional statements
C++ for Engineers and Scientists Third Edition
Programming Control Flow. Sequential Program S1 S2 S5 S4 S3 int main() { Statement1; Statement2; … StatementN; } Start End.
1 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)
Programming Variables. Named area in the computer memory, intended to contain values of a certain kind (integers, real numbers, characters etc.) They.
Spring 2005, Gülcihan Özdemir Dağ Lecture 3, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 3 Outline 3.1 Introduction.
C programming for Engineers Lcc compiler – a free C compiler available on the web. Some instructions.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 if and if-else statements. 2 Relational Operators x < y < is an operator x and y are its operands ( x < y ) is called a logical expression. Logical.
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.
1 If statement and relational operators –, >=, ==, != Finding min/max of 2 numbers Finding the min of 3 numbers Forming Complex relational expressions.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=
Flow of Control Part 1: Selection
A First C Program /* Print a Message */ #include main() { printf("This is a test!\n"); } The program is compiled and run as cc pg32.c  a.out  This is.
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
CPS120: Introduction to Computer Science Decision Making in Programs.
1 C Programming Week 2 Variables, flow control and the Debugger.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
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.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Selection-making Decisions Selection allows you to choose between two or more possible program flow --- it lets you make decisions in your program. Examples.
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.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
1 Lecture03: Control Flow 9/24/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
CPS120: Introduction to Computer Science Decision Making in Programs.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
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.
 Real numbers representation - Floating Point Notation  First C Program  Variables Declaration  Data Types in C ◦ char, short, int, long, float, double,
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Chapter 4 – C Program Control
CNG 140 C Programming (Lecture set 3)
The if…else Selection Statement
Chapter 4: Making Decisions.
Chapter 4 (Conditional Statements)
Chapter 4 - Program Control
Chapter 4: Making Decisions.
- Additional C Statements
C Programming Variables.
Week 2 Variables, flow control and the Debugger
Dale Roberts, Lecturer IUPUI
Chapter 4 - Program Control
Lecture 9: Implementing Complex Logic
Presentation transcript:

1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.

2 Flow Control Usually a program is executed line after line in order It is reasonable to expect different execution orders on different inputs Computer games Illegal input Conditional statements if switch

3 Conditional Statement: if used to execute conditionally a statement (or block of code) Syntax: if (expression) statement If expression is true, statement is executed (what is the meaning of ‘true’?) statement can be replaced by a block of statements, enclosed in curly braces

4 Example /* This program displays the absolute value of a number given by the user */ #include int main() { double num; printf("Please enter a real number: "); scanf("%lf", &num); if (num<0) { num = -num; } printf("The absolute value is %g\n", num); return 0; }

5 If-Else Statement if (expression) statement 1 else statement 2 if expression is true, statement 1 is executed. if expression is false, statement 2 is executed both statements can be (and very often are) replaced by blocks of statements (“compound statements”)

6 Example int first, second, min; if (first < second) { min = first; printf ("The first number is smaller than the second\n"); } else { min = second; printf ("The second number is smaller than the first\n"); } printf("The smaller number is equal to %d\n", min);

7 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.

8 True and False In C, every expression has a numeric value An expression is ‘true’ when it is non-zero If it is zero, it is false Therefore, in the following – if (expression) statement statement is executed if expression is non zero

9 More About Operators In C, every expression has a numeric value When using arithmetical operators (+, -, *, /) this is straight forward The value of A+B is the sum of A and B And so on…

10 More About Operators Expressions with relational operators (<, <=, etc’) have values as well (intuitively, we are used to thinking about them as ‘true’ or ‘false’) A < B equals zero if A is larger than or equal to B (false), and some non-zero value if A is smaller than B (true) The exact non-zero value varies (and is not important for that matter)

11 Relational Operators They are – A == B (Note the difference from A = B) A != B (inequality) A < B A > B A <= B A >= B The value of the expression is non-zero if it’s true, zero if it’s false

12 Example int a, b; printf("Enter two Numbers\n"); scanf("%d%d", &a, &b); if (a == b) { printf("The numbers equal %d\n", a); printf("The expression a == b is %d\n", a == b); } else { printf("The numbers are not equal\n"); printf("The expression a == b is %d\n", a == b); }

13 The Assignment Operator = The assignment operator is also an operator. Hence, expressions involving it have a numeric value This value equals to whatever appears on the right of the assignment operator For example – (x = 4) equals 4 (y = 0) equals 0

14 A Very Common Mistake Very often a programmer might confuse between the equality operator and the assignment operator - if (x==4) … if (x=4) … The second is usually a mistake, but legal in C so the compiler doesn’t call it!

15 More Examples if (0) if (1) if (3>4) if (x) equivalent to if (x == 0) if (a = 5)

16 Code Examples val.c, eqn_sign.c

17 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.

18 Logical Operators Allows to evaluate two or more expressions - !A – ‘not’ - True when A is not, and vice versa. A && B – ‘and’ - True when both A and B are true A || B – ‘or’ (inclusive or) - True when either A or B (or both) are true

19 &&, ||, != && || !

20 Example int grade; printf("Please enter your grade: "); scanf("%d", &grade); if (grade 100) printf("This is not a valid grade!\n"); else printf("This is a valid grade\n");

21 Example if (price > 100) if (price < 200) printf(“reasonable price”); if (price > 100 && price < 200) printf(“reasonable price”);

22 Operation Execution Order ! %, /, * -, +, ==, … &&, ||

23 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.

24 Else-If if statements distinguish between exactly 2 cases and execute different code in each case The else-if construction allows for a multi-way decision

25 Else-If if (expression) statement else if (expression) statement else if (expression) statement else statement

26 Example if (grade >= 90) printf ("A\n"); else if (grade >= 80) printf ("B\n"); else if (grade >= 70) printf ("C\n"); else if (grade >= 60) printf ("D\n"); else printf ("F\n");

27 Switch switch (expression) { case const-exp1: statements break; case const-exp2: statements break;. default: statements } variable – discrete const-exp – equality is tested It is ordered break – stop default – optional, when no prior condition held

28 That grade example again switch (grade/10) { case 10: case 9: printf ("A\n"); break; case 8: printf ("B\n"); break; case 7: printf ("C\n"); break; case 6: printf ("D\n"); break; default: printf ("F\n"); }

29 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.

30 Exercise Input – An English letter Output – If input is a lowercase letter – the corresponding uppercase letter If input is an uppercase letter - corresponding lowercase letter Note – Remember to check for input validity!

31 Solution switch_case.c

32 Exercise (difficult!) Write a program such that – Input – a 3-digit number Output – the same number with digits sorted Example – if input is 132, output should be 123 Note – if input is not a 3-digit number, display an error message and exit!

33 Solution Sort_digits.c

34 The ?: operator expr 1 ? expr 2 : expr 3 If expr1 is true (non-zero), expr2 is evaluated. Otherwise, expr3 is evaluated

35 The ?: operator int i, j, min; printf("Please enter two numbers: "); scanf("%d%d", &i, &j); min = (i < j) ? i : j; printf("The minimum between %d and %d is%d\n", i, j, min);

36 Example – mass conversion Write a program such that – Input – A positive number indicating mass One of the following characters – o, c, k, p, indicating measurement unit (ounce, carat, kilogram, or pound Output – The same mass expressed in grams convert_gr.c

37 Exercise Write a program that accepts a number between 1 and 100 from the user. If there is a coin of that value in cents, it should display its name. Otherwise, it should report that there is no such coin 1 = cent, 5 = nickel, 10 = dime, 25 = quarter, 100 = dollar Remember to check for input validity!

38 Solution Coins.c

39 Exercise Write a program that accepts an real number, followed by an arithmetical operator (+, -, *, /) and then a second real number The program will calculate the result of the expression and present it on the screen Example – for the input 10-8, the output will be – 10-8 = 2

40 Solution Operation.c