Basic Concepts – Conditionals

Slides:



Advertisements
Similar presentations
INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.
Advertisements

Switch code for Lab 4.2 switch (input) { /* input is a variable that we will test. */ case 'M': printf("The prefix is equal to 1E6.\n"); break; case 'k':
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.
Conditionals – if - else Dr. Sajib Datta
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
true (any other value but zero) false (zero) expression Statement 2
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CS 117 Spring 2002 Review for Exam 2 March 6, 2002 open book, 1 page of notes.
Programming Control Flow. Sequential Program S1 S2 S5 S4 S3 int main() { Statement1; Statement2; … StatementN; } Start End.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 2.
CMSC 104, Version 8/061L11Relational&LogicalOps.ppt Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
CSCI 1100/1202 January 28, The switch Statement The switch statement provides another means to decide which statement to execute next The switch.
Basic Concepts – Conditionals Conditionals are used for making decisions. The conditionals available in C are if and if-else statements the switch statement.
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.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
CMSC 104, Version 9/011 Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else Statement Nesting of.
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.
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
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 – C Program Control
Decision Making in C.
UMBC CMSC 104 – Section 01, Fall 2016
The switch Statement, and Introduction to Looping
C Program Controls + Flow Structure
Chapter 4 (Conditional Statements)
EGR 2261 Unit 4 Control Structures I: Selection
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
CSE1320 INTERMEDIATE PROGRAMMING Operators+Conditionals+Loop
Relational & Logical Operators
- Additional C Statements
Relational and Logical Operators
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
Relational and Logical Operators
Chapter 4 - Program Control
Exam 1 Date: Feb. 2nd, 2015 during class time (50 minutes) Coverage
Conditions and Boolean Expressions
If Statements.
UMBC CMSC 104 – Section 01, Fall 2016
Relational, Logical, and Equality Operators
Relational and Logical Operators
Program Flow.
More Loops Topics Counter-Controlled (Definite) Repetition
Dale Roberts, Lecturer IUPUI
More Loops Topics Counter-Controlled (Definite) Repetition
Chapter 4 - Program Control
Relational and Logical Operators
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Relational Operators Logical Operators for Loops.
More Loops Topics Counter-Controlled (Definite) Repetition
Comparing Data & the ‘switch’ Statement
Relational and Logical Operators
More Loops Topics Counter-Controlled (Definite) Repetition
Comparing Data & the ‘switch’ Statement
CprE 185: Intro to Problem Solving (using C)
Chap 7. Advanced Control Statements in Java
Relational and Logical Operators
EECE.2160 ECE Application Programming
More Loops Topics Counter-Controlled (Definite) Repetition
ICS103: Programming in C 5: Repetition and Loop Statements
More Loops Topics Counter-Controlled (Definite) Repetition
Presentation transcript:

Basic Concepts – Conditionals Conditionals are used for making decisions. The conditionals available in C are • if and if-else statements • the switch statement • the conditional expression operator, ?:

Operators – Relational The following relational operators are available in C: == equality != not equal < lesser than > greater than <= less than or equal to >= greater than or equal to

Conditionals – if The basic format of the if statement is if (condition_is_true) do_something; Examples if (x >= 2) y = 10; if (y == 10) printf("y is now 10");

Conditionals – if What if we wish to have a second conditional statement in response to the first condition being true? We can nest the if statements. Example if (count > 23) if(time < 45) z = 56; This assigns a value of 56 to z only if count > 23 and time < 45.

Conditionals – if cont. Question: What if we want to do more than one thing in response to a condition being true? Answer: Create a block of statements. Example if (x >= 2) { y = 10; printf("y is now 10"); }

Conditionals – if-else Sometimes we wish to do one thing if a condition is true but another if the condition is false. For this we can use an if-else statement: if (condition_is_true) do_something; else do_something_else; Example if (x >= 2) printf("x is greater than or equal to 2"); printf("x is less than 2");

Conditionals – if-else What if we have nested if statements and an else statement; which if does the else belong to? /* indented to trick you */ if (amount == 13) if (cost == 52) printf("cost is %d\n", cost); else printf("got here\n"); /* indented to show correct logic */

Conditionals Notes Keep in mind that any condition that evaluates to a nonzero value is considered true. if (8) printf("non-zero values are true\n"); else printf("this never prints\n"); /* another example */ if (-3.4) printf("-3.4 is considered true\n"); if (0) printf("zero is false\n"); printf("this is always false\n");

Conditionals Notes cont. WARNING: Don’t use = when you really mean == = is used for assigning values Example: a = 5; == is used for determining if two values are equal Example: if (a == 5)

Conditionals – switch An if-else statement is used for binary decisions–those with two choices. Sometimes there are more than two choices. switch (expression) { case label1: do_this case label2: do_this · case labeln: do_this default: do_this }

Conditionals – switch cont. Notes on the use of the switch statement • The labels must be integers (or at least evaluate to an integer). • The default line is optional. • Once the matching label is found, that statement and each following statement will be executed (unless we use a break).

switch Example Example: #include <stdio.h> int main( void ) { int a = 3; int b = 5; switch(b - a) case 5: printf("countdown from 5\n"); case 4: printf("countdown from 4\n"); case 3: printf("countdown from 3\n"); case 2: printf("countdown from 2\n"); case 1: printf("countdown from 1\n"); }

switch Example cont. Output countdown from 2 countdown from 1

switch and break On many occasions we may only wish to do a single thing in response to what the switch evaluates to. For this, we can use a break statement. Example: #include <stdio.h> int main(void) { int cost = 18; switch(cost) case 23: printf("cost is 23\n"); break; case 18: printf("cost is 18\n"); break; case 75: printf("cost is 75\n"); break; }

switch and break contd. Output cost is 18

Conditionals – ?: Instead of an if-else statement, we can use the conditional expression operator: (condition_is_true) ? do_if_true : do_if_false; Example (w < 14) ? (x = 10) : (y = 19); NOTE: This is discussed in chapter 9 (page 515) of the textbook.

Conditionals – ?: cont. A difference between this and an if-else statement is that the conditional expression can be used like other expressions: Example answer = ((w > 14) ? 28 : 16); This is equivalent to if (w > 14) answer = 28; else answer = 16;

Conditionals Example 1 Both types of conditionals are used equivalently here #include <stdio.h> int main(void) { int x = 3, y = 10; printf("x is %d, y is %d\n\n", x, y); if (x > y) printf("x is larger\n\n"); else printf("y is larger\n\n"); /* equivalent structure */ printf("Let’s try with the conditional expression:\n"); (x > y) ? printf("x is larger\n") : printf("y is larger\n") ; }

Conditionals Example 1 cont. The output is x is 3, y is 10 y is larger Let’s try with the conditional expression:

Conditionals Example 2 Here we have a conditional expression inside a function call: #include <stdio.h> int main(void) { int x = 5; printf("x is %s\n", (x < 100) ? "small" : "large"); } Produces x is small

Increment/Decrement Operators The following operators are available in C for incrementing and decrementing variables by a value of one: ++ increment -- decrement Example: We could use a = a + 1; or a++;

Increment/Decrement Operators These operators can be placed before (prefix) or after (postfix) a variable: int x = 5; x--; /* at this point x has a value of 4 */ Or --x; both reduce x by one. Later we will see examples where the choice of prefix or postfix matters.