The if Statement Format or No Condition satisfied? Yes

Slides:



Advertisements
Similar presentations
Chapter 4 Computation Bjarne Stroustrup
Advertisements

Logic Gates.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
Selection The Switch Statement 2/16/11. grade = 'P'; switch (grade){ case 'A': cout
Primitive Variable types Basic types –char (single character or ASCII integer value) –int (integer) –short (not longer than int) –long (longer than int)
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Logical and Relational Expressions Nested if statements.
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.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 2.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
Selection Structures (if & switch statements) (CS1123)
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
Basic Concepts – Conditionals Conditionals are used for making decisions. The conditionals available in C are if and if-else statements the switch statement.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
Copyright 2003 Scott/Jones Publishing Making Decisions.
CCSA 221 Programming in C CHAPTER 6 MAKING DECISIONS 1.
Conditional Expressions
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
Introduction to Programming Lecture 6: Making Decisions.
Department of Electronic & Electrical Engineering Lecture 3 IO reading and writing variables scanf printf format strings "%d %c %f" Expressions operators.
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.
C syntax (simplified) BNF. Program ::= [ ] Directives ::= [ ] ::= | |… ::=#include > ::=#define.
 Real numbers representation - Floating Point Notation  First C Program  Variables Declaration  Data Types in C ◦ char, short, int, long, float, double,
Dr. Sajib Datta Sep 3,  A new operator used in C is modulus operator: %  % only used for integers, not floating-point  Gives the integer.
Chapter 4: Control Structures I
Decision Making in C.
Chapter 3 Control Statements
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Basics programming concepts-2
Control Structures and Data Files
Chapter 4: Control Structures I
Decisions Chapter 4.
Chapter 4: Making Decisions.
A mechanism for deciding whether an action should be taken
Logic Gates.
Chapter 4 (Conditional Statements)
Data Types, Identifiers, and Expressions
Test Review Computer Science History
Engineering Problem Solving with C++, Etter/Ingber
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Chapter 4: Making Decisions.
Expressions and Control Flow in JavaScript
Chapter 4: Making Decisions.
CSE1320 INTERMEDIATE PROGRAMMING Operators+Conditionals+Loop
An Introduction to Programming with C++ Fifth Edition
Logical Statements and Selection Structures
Control Structures – Selection
Introduction to Programming
Chapter 4: Control Structures I
Exam 1 Date: Feb. 2nd, 2015 during class time (50 minutes) Coverage
Professor Jodi Neely-Ritz CGS3460
Basic Concepts – Conditionals
Logic Gates.
Conditions and Boolean Expressions
Chapter 7 Conditional Statements
If Statements.
Control Structures Lecture 6.
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
More on conditional statements
Nate Brunelle Today: Conditional Decision Statements
Herbert G. Mayer, PSU CS Status 7/19/2015
Decisions, decisions, decisions
The switch Statement When we want to compare a variable against several values to see which one it has, we can use the switch statement: switch (status)
CSCE 206 Lab Structured Programming in C
Presentation transcript:

The if Statement Format or No Condition satisfied? Yes if ( condition ) program statement; or { program statements; } Condition satisfied? No Program statement Yes

The if-else Statement Format No Condition satisfied? Yes if ( condition ) program statement 1; else program statement 2; Condition satisfied? No Program statement 1 Yes Program statement 2

The else if Statement Format Condition 1 satisfied? No if ( condition1 ) program statement 1; else if (condition2) program statement 2; Condition 1 satisfied? No Condition 2 satisfied? Program statement 1 Yes No Program statement 2 Yes

Boolean Variables Declaration Value _Bool 0 representing false 1 representing true, mostly Can use any nonzero value representing true in C programming language!

Logical Operators Operator Example Description || Why Make a decision based on multiple conditions What are they Operator Example Description || x < 0 || x > width logical OR && x >= 0 && x <= width logical AND ! !(x < 0) Logical NOT

Logical OR 1 Returns false only if both expressions are false Example: (4 > 5 || 6 <= 10) (4 <= 5 || 6 == 10) (4 >= 5 || 6 > 10) (4 < 5 || 6 != 10) A B A || B True False 1

A More Complicated Example 5: 10: 15: 20: 25: 30: 35: 40: 45: true false #include <stdio.h> int main() { int i; for (i=5; i<50; i+=5) if((i < 15) || (i >= 35)) printf("%i: true\n", i); else printf(“%i: false\n”, i); }//for return 0; }//main

Logical AND 1 Returns true only if both expressions are true Examples: (4 > 5 && 6 <= 10) (4 <= 5 && 6 == 10) (4 >= 5 && 6 > 10) (4 < 5 && 6 != 10) A B A && B True False 1

A More Complicated Example 5: 10: 15: 20: 25: 30: 35: 40: 45: false true #include <stdio.h> int main() { int i; for (i=5; i<50; i+=5) if((i > 15) && (i <= 35)) printf("%i: true\n", i); else printf(“%i: false\n”, i); }//for return 0; }//main

Logical NOT Inverts the Boolean value of an expression A !A True False Example: _Bool a = 0; if (!a) { printf(“a is a false value (0)\n”); } a = 1; if (a) { printf(“a is a true value (1)\n”); A !A True False

Conditions Relational expression with ==, !=, >, >=, <, <= The value of “relational expression” is 1 if the relation is true, and 0 if false.  Can be combined with the operators (with decreasing precedence) ! (NOT) && (AND) || (OR) Rules: && and || are evaluated left to right Short circuiting – a little advanced Example c==' ' || c=='\t' || c=='\n' x < 75 && x > 101 && x != 101 x >= 35 && x < 7 || x == 10 (x >= 35 && x < 7) || x == 10 (F && F) || T ≠ F && (F || T)

Evaluate simple expression Objective: Evaluate expression and display results with input form Number operator number Example User input: 12 + 3 Program output: 15

Program Need to check v2 == 0? #include <stdio.h> int main(void) { float v1, v2; char operator; printf("Type in your expression. \n"); scanf("%f %c %f", &v1, &operator, &v2); if ( operator == '+' ) printf("%.2f\n", v1 + v2); else if ( operator == '-' ) printf("%.2f\n", v1 - v2); else if ( operator == '*' ) printf("%.2f\n", v1 * v2); else if ( operator == '/' ) printf("%.2f\n", v1 / v2); return 0; } Need to check v2 == 0?

Corrections Change else if ( operator == '/' ) to printf("%.2f\n", v1 / v2); to { if ( v2 == 0 ) printf(“Division by zero.\n”); else }

The switch Statement evaluate expression == value 1 statement 1 Y N When to use The value of a variable successively compared against different values Format switch( expression ) { case value 1: program statement 1; break; case value 2: program statement 2; ׃ case value n: program statement n; default : program statement n+1; } == value 1 statement 1 Y N == value 2 Y statement 2 N == value n Y statement n N statement n+1

Example switch ( operator ) { case '+': printf("%.2f\n", v1 + v2); break; case '-': printf("%.2f\n", v1 - v2); case '*': printf("%.2f\n", v1 * v2); case '/': if ( v2 == 0 ) printf(“Division by zero.\n”); else printf("%.2f\n", v1 / v2); default: printf("Unknown operator!\n"); }

Another example Month = 11 November switch (month) { case 1: printf("January"); break; case 2: printf("February"); break; case 3: printf("March"); break; case 4: printf("April"); break; case 5: printf("May"); break; case 6: printf("June"); break; case 7: printf("July"); break; case 8: printf("August"); break; case 9: printf("September"); break; case 10: printf("October"); break; case 11: printf("November"); break; case 12: printf("December"); break; default: printf("Invalid month."); break; } Month = 7 July Month = 14 Invalid month

More on switch Statement case value 1: program statement 1; case value 2: program statement 2; break; == value 1 statement 1 Y N == value 2 Y statement 2 N

One last example switch (month) { case 1: printf("January\n"); case 2: printf("February\n"); case 3: printf("March\n"); case 4: printf("April\n"); case 5: printf("May\n"); case 6: printf("June\n"); default: printf("Invalid month.\n"); } Month = 11 Ivalid month Month = 4 April May June Invalid Month

The conditional operator Format: condition ? expression1 : expression2; Same as if ( condition ) { expression1; } else { expression2;

Conditional example //find the absolute value of a abs = a < 0 ? -a : a;