Basic Concepts – Conditionals Conditionals are used for making decisions. The conditionals available in C are if and if-else statements the switch statement.

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':
Introduction to C Programming
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.
For loops For loops are controlled by a counter variable. for( c =init_value;c
Conditionals – if - else Dr. Sajib Datta
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 4 (Conditional Statements) © CPCS
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
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
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
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.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
The switch Statement, DecimalFormat, and Introduction to Looping
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 2.
Principles of Programming - NI July Chapter 5: Structured Programming In this chapter you will learn about: Sequential structure Selection structure.
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.
CONTROL STRUCTURE The if, elseif, and else & switch Statements 1.
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.
1 2. Program Construction in Java. 2.4 Selection (decisions)
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
Chapter 5: Structured Programming
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
Copyright 2003 Scott/Jones Publishing Making Decisions.
Week 8: Decisions Bryan Burlingame 21 October 2015.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
CMSC 104, Version 9/011 More Loops Topics Counter-Controlled (Definite) Repetition Event-Controlled (Indefinite) Repetition for Loops do-while Loops Choosing.
Principles of Programming Chapter 4: Basic C Operators  In this chapter, you will learn about:  Arithmetic operators  Unary operators  Binary operators.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
CMSC 104, Version 9/011 Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else Statement Nesting of.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
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)
 2006 Pearson Education, Inc. All rights reserved if…else Double-Selection Statement if – Performs action if condition true if…else – Performs.
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
Flow Control. Comments u Comments: /* This is a comment */ –Use them! –Comments should explain: v special cases v the use of functions (parameters, return.
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
Chapter 4 (Conditional Statements)
EGR 2261 Unit 4 Control Structures I: Selection
CSE1320 INTERMEDIATE PROGRAMMING Operators+Conditionals+Loop
Relational & Logical Operators
- Additional C Statements
Relational and Logical Operators
Relational and Logical Operators
Chapter 4 - Program Control
Basic Concepts – Conditionals
Relational and Logical Operators
Program Flow.
Dale Roberts, Lecturer IUPUI
Relational and Logical Operators
Chap 7. Advanced Control Statements in Java
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"); else 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 */ if (amount == 13) if (cost == 52) printf("cost is %d\n", cost); else printf("got here\n");

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"); else printf("this never prints\n"); /* another example */ if (0) printf("zero is false\n"); else 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 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 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 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: y is larger

Conditionals Example 2 Here we have a conditional expression inside a function call: #include 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 int x = 5; --x; /* at this point x has a value of 4 */ both reduce x by one. Later we will see examples where the choice of prefix or postfix matters.