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.

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

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.
© 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
1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Programming Control Flow. Sequential Program S1 S2 S5 S4 S3 int main() { Statement1; Statement2; … StatementN; } Start End.
1 Lecture 5  More flow control structures  for  do  continue  break  switch  Structured programming  Common programming errors and tips  Readings:
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
1 Chapter 3 Flow of Control. 2 Review of Class on Sep 23.
12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional.
C Programming Lecture 12. The Compound Statement b A compound statement is a series of declarations and statements surrounded by braces. b A compound.
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Conditional Statement
 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.
Computer programming Lecture 4. Lecture 4: Outline Making Decisions [chap 6 – Kochan] –The if Statement –The if-else Construct –Logical Operators –Boolean.
Selection Structures (if & switch statements) (CS1123)
CMSC 104, Version 8/061L11Relational&LogicalOps.ppt Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (Switch, do-while, break) Outline 4.7The.
1 Lecture 5: Selection Structures. Outline 2  Control Structures  Conditions  Relational Operators  Logical Operators  if statements  Two-Alternatives.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Basic Concepts – Conditionals Conditionals are used for making decisions. The conditionals available in C are if and if-else statements the switch statement.
Introduction to Computing Lecture 05: Selection (continued) Introduction to Computing Lecture 05: Selection (continued) Assist.Prof.Dr. Nükhet ÖZBEK Ege.
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
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.
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.
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
CCSA 221 Programming in C CHAPTER 6 MAKING DECISIONS 1.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
CSCI 171 Presentation 3. Operators Instructs C to perform some operation Assignment = Mathematical Relational Logical.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
CMSC 104, Version 9/011 Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else Statement Nesting of.
CS115 FALL Senem KUMOVA-METİN1 CHAPTER 4 FLOW OF CONTROL-1 Operators, If and switch statements.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
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.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
Program Control: Selection Subject: T0016 – ALGORITHM AND PROGRAMMING Year: 2013.
Dr. Sajib Datta Sep 3,  A new operator used in C is modulus operator: %  % only used for integers, not floating-point  Gives the integer.
Lesson #4 Logical Operators and Selection Statements.
Lesson #4 Logical Operators and Selection Statements.
Chapter 4 – C Program Control
The if…else Selection Statement
Decisions Chapter 4.
Chapter 4: Making Decisions.
The if Statement Format or No Condition satisfied? Yes
Condition Statements.
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
CSE1320 INTERMEDIATE PROGRAMMING Operators+Conditionals+Loop
Relational & Logical Operators
Relational and Logical Operators
Relational and Logical Operators
Relational, Logical, and Equality Operators
Relational and Logical Operators
2.6 The if/else Selection Structure
Chapter 4 - Program Control
Relational and Logical Operators
Relational and Logical Operators
Flow of Control.
Relational and Logical Operators
DATA TYPES There are four basic data types associated with variables:
Presentation transcript:

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 language. this might be “written” as follows: if ( it is not raining ) I will go swimming if ( expression ) program statement Program statement expression yes no

Example 1.1 int main(void) { int degree; printf("Type in your degree: "); scanf("%i", &degree); if (degree >= 60) printf("You Passed \n"); return 0; } C:\Windows\System32\cmd.exe Type in your degree: 95 You Passed

2. if – else statement if ( expression ) program statement 1 else program statement 2 Program statement 1 expression yes no Program statement 2

Example 2.1 int main(void) { int degree; printf("Type in your degree: "); scanf("%i", &degree); if (degree >= 60) printf("You Passed \n"); else printf("You Failed \n"); return 0; } C:\Windows\System32\cmd.exe Type in your degree: 55 You Failed C:\Windows\System32\cmd.exe Type in your degree: 80 You Passed

Common Error A common error is to add a semicolon (;) at the end of the if statement, as you usually do with the most statements. int x = −3; if(x > 0); printf("x is positive\n"); C:\Windows\System32\cmd.exe x is positive the ; terminates the if statement and the program continues with the printf() call. Therefore, the output is always x is positive regardless of the value of x.

4.Nested if statements C:\Windows\System32\cmd.exe Enter a number: 11 C:\Windows\System32\cmd.exe Enter a number: 11 Output 2 int x ; printf("Enter a number: "); scanf("%i", &x); if(x < 10) if(x > 5) printf("output 1\n"); else printf(" output 2\n \n"); int x ; printf("Enter a number: "); scanf("%i", &x); if(x < 10) { if(x > 5) printf("output 1\n"); } else printf(" output 2\n \n"); In a program with nested if statements, each else statement is associated with the nearest if statement that does not contain an else

5. Else if int main(void) { int degree; printf("Type in your degree: "); scanf_s("%i", &degree); if (degree >= 90) printf("You Passed with A grade \n"); else if (degree >= 80) printf("You Passed with B grade \n"); else if (degree >= 70) printf("You Passed with C grade \n"); else if (degree >= 60) printf("You Passed with D grade \n"); else printf("You Failed \n"); return 0; } C:\Windows\System32\cmd.exe Type in your degree: 82 You Passed with B grade C:\Windows\System32\cmd.exe Type in your degree: 75 You Passed with C grade

6.Logical operators – Combining conditions int main(void) { int degree; printf("Type in your degree: "); scanf_s("%i", &degree); if (degree > 100 || degree < 0) printf("degree is not accepted \n"); else if (degree >= 90) printf("You Passed with A grade \n"); else if (degree >= 80) printf("You Passed with B grade \n"); else if (degree >= 70) printf("You Passed with C grade \n"); else if (degree >= 60) printf("You Passed with D grade \n"); else printf("You Failed \n"); return 0; } C:\Windows\System32\cmd.exe Type in your degree: 200 degree is not accepted C:\Windows\System32\cmd.exe Type in your degree: -3 degree is not accepted

6. Logical operators OperatorSymbolMeaning AND&&X && y is true if BOTH x and y are true OR||X || y is true if at least one of x and y is true NOT!!x is true if x is false Logical values as operands or in tests: true = non-zero, false= zero Logical values returned as results of expressions: true = 1, false= zero Example: ( 5 || 0 ) = 1

Precedence of operators Example for operator precedence: a > b && b > c || b > d Is equivalent to: ((a > b) && (b > c)) || (b > d) !, ++, --, (type) *, /, % +, -, >=, ==, != && || = Precedence

Testing for ranges if(x >= 5 && x <= 10) printf(“in range"); if (5 <= x <= 10) printf(“in range");

Testing for ranges if (x >= 5 && x <= 10) printf(“in range"); if (5 <= x <= 10) printf(“in range"); Syntactically correct, but semantically an error !!! Because the order of evaluation for the <= operator is left-to-right, the test expression is interpreted as follows: (5<= x) <= 10 The subexpression 5 <= x either has the value 1 (for true) or 0 (for false). Either value is less than 10, so the whole expression is always true, regardless of x !

7.Conditional operator The conditional operator ?: allows a program to perform one of two actions depending on the value of an expression. General format: condition ? expression1 : expression 2. condition is an expression that is evaluated first. If the result of the evaluation of condition is TRUE (nonzero), then expression1 is evaluated and the result of the evaluation becomes the result of the operation. If condition is FALSE (zero), then expression2 is evaluated and its result becomes the result of the operation

7.Conditional operator Example 2: int x = -2; (x > 0) ? printf("Positive \n") : printf(“Negative \n"); Example 1: maxValue = ( a > b ) ? a : b; Equivalent to: if ( a > b ) maxValue = a; else maxValue = b;

8.Switch () switch ( expression ) { case value1: program statement... break; case value2: program statement... break;... case valuen: program statement... break; default: program statement... break; } The switch statement can be used instead of if-else-if statements when we want to test the value of an expression against a series of values and handle each case differently. Remember to include the break statement at the end of every case. The switch test expression must be one with an integer value (including type char) (No float !). The case values must be integer-type constants or integer constant expressions (You can't use a variable for a case label !)

Example: Multiple choices int main(void) { float value1, value2; char op; printf("Type in your expression.\n"); scanf("%f %c %f", &value1, &op, &value2); if (op == '+') printf("%.2f\n", value1 + value2); else if (op == '-') printf("%.2f\n", value1 - value2); else if (op == '*') printf("%.2f\n", value1 * value2); else if (op == '/') printf("%.2f\n", value1 / value2); else printf("Unknown operator.\n"); return 0; } To execute this code correctly in Visual Studio, replace the following: Scanf ("%f %c %f", &value1, &op, &value2 ) with scanf_s ("%f %c %f", &value1, &op, 1, &value2 ) C:\Windows\System32\cmd.exe Type in your expression. 3 *

Example: Switch() int main(void) { float value1, value2; char op; printf("Type in your expression.\n"); scanf("%f %c %f", &value1, &op,&value2); switch (op) { case '+': printf("%.2f\n", value1 + value2); break; case '-': printf("%.2f\n", value1 - value2); break; case '*': printf("%.2f\n", value1 * value2); break; case '/': if (value2 == 0) printf("Division by zero.\n"); else printf("%.2f\n", value1 / value2); break; default: printf("Unknown operator.\n"); break; } return 0; }

Switch() switch (op) { case 'x': case '*': printf("%.2f\n", value1 * value2); break; …… } you can associate more than one case value with a particular set of program statements. printf() is executed if op is equal to an asterisk or to the lowercase letter x.