Introduction to Computing Lecture 05: Selection (continued) Introduction to Computing Lecture 05: Selection (continued) Assist.Prof.Dr. Nükhet ÖZBEK Ege.

Slides:



Advertisements
Similar presentations
Programming In C++ Spring Semester 2013 Lecture 4 Programming In C++, Lecture 3 By Umer Rana.
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
true (any other value but zero) false (zero) expression Statement 2
1 CSE1301 Computer Programming Lecture 7 Booleans Linda M c Iver.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
C++ for Engineers and Scientists Third Edition
1 Revision of IO Streams printf scanf. 2 CSE1301 Computer Programming Lecture 8 Booleans.
1 ICS103 Programming in C Lecture 6: Selection Structures.
1 Lecture 5  More flow control structures  for  do  continue  break  switch  Structured programming  Common programming errors and tips  Readings:
Spring 2005, Gülcihan Özdemir Dağ Lecture 3, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 3 Outline 3.1 Introduction.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
CPS 125: Digital Computation and Programming Selection Structures: if and switch Statements.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
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.
1 Lecture 5: Selection Structures. Outline 2  Control Structures  Conditions  Relational Operators  Logical Operators  if statements  Two-Alternatives.
Chapter 4 C Program Control. Objectives In this chapter, you will learn: –To be able to use the for and do … while repetition statements. –To understand.
Flow of Control Part 1: Selection
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
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 #7 CONTROL STRUCTURE & FLOW CHARTS
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Choices and Decisions if statement if-else statement Relational.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
CCSA 221 Programming in C CHAPTER 6 MAKING DECISIONS 1.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.
BOOLEAN OPERATIONS AND CONDITIONALS CHAPTER 20 1.
CMSC 104, Version 9/011 Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else Statement Nesting of.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Control Statements: Part1  if, if…else, switch 1.
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++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
 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.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Lecture #8 SWITCH STATEMENT By Shahid Naseem (Lecturer)
 2006 Pearson Education, Inc. All rights reserved if…else Double-Selection Statement if – Performs action if condition true if…else – Performs.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Branching statements.
Chapter 4 – C Program Control
The if…else Selection Statement
Decisions Chapter 4.
EGR 2261 Unit 4 Control Structures I: Selection
CSC113: Computer Programming (Theory = 03, Lab = 01)
Lecture 2: Logical Problems with Choices
Writing a program with conditionals
Chapter 4 - Program Control
Control Structures: Selection Statement
Chapter 4: Control Structures I (Selection)
Introduction to Computing Lecture 04: Booleans & Selection
Control Structures: Selection Statement
Branching statements Kingdom of Saudi Arabia
Chap 7. Advanced Control Statements in Java
CSCE 206 Lab Structured Programming in C
Lecture 9: Implementing Complex Logic
Presentation transcript:

Introduction to Computing Lecture 05: Selection (continued) Introduction to Computing Lecture 05: Selection (continued) Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics Engineering

Topics Type int as Boolean Type int as Boolean Nested if statements Nested if statements switch statement switch statement

Recall : Syntax for if statement if (condition) statement;OR { statement 1 ; … statement n ; }

Recall : Syntax for if & else statement if (condition) statement T ; else statement F ; OR if (condition) { statement T1 ; … statement Tn ; }else{ statement F1 ; … statement Fn ; }

Recall : Syntax for cascaded if statements if (condition 1 ) statement 1 ; else if (condition 2 ) statement 2 ;... else if (condition n ) statement n ; else statement e ;

Type int as Boolean In C, integers can be used as Booleans In C, integers can be used as Booleans Integer value 0 is false Integer value 0 is false Any non-zero integer value is true Any non-zero integer value is true

#include #include /* Test some Booleans. */ int main() { int whatever = 0; int whatever = 0; if (whatever) if (whatever) { printf(“Is that true, Homer?\n”); printf(“Is that true, Homer?\n”); } else else { printf(“No, Marge.\n”); printf(“No, Marge.\n”); } return 0; return 0;} Example: What is the output?

#include #include /* Test some Booleans. */ int main() { int whatever = 1; int whatever = 1; if (whatever) if (whatever) { printf(“Is that true, Homer?\n”); printf(“Is that true, Homer?\n”); } else else { printf(“No, Marge.\n”); printf(“No, Marge.\n”); } return 0; return 0;} Example: What is the output?

#include #include /* Test some Booleans. */ int main() { int whatever = -100; int whatever = -100; if (whatever) if (whatever) { printf(“Is that true, Homer?\n”); printf(“Is that true, Homer?\n”); } else else { printf(“No, Marge.\n”); printf(“No, Marge.\n”); } return 0; return 0;} Example: What is the output?

#include #include /* Test some Booleans. */ int main() { int whatever = 0.003; int whatever = 0.003; if (whatever) if (whatever) { printf(“Is that true, Homer?\n”); printf(“Is that true, Homer?\n”); } else else { printf(“No, Marge.\n”); printf(“No, Marge.\n”); } return 0; return 0;} Example: What is the output?

#include #include /* Test some Booleans. */ int main() { float whatever = 0.003; float whatever = 0.003; if (whatever) if (whatever) { printf(“Is that true, Homer?\n”); printf(“Is that true, Homer?\n”); } else else { printf(“No, Marge.\n”); printf(“No, Marge.\n”); } return 0; return 0;} Behavior is “undefined”

Nested if statements We can nest if statements (one if statement inside another) to code decisions with multiple alternatives We can nest if statements (one if statement inside another) to code decisions with multiple alternatives

Example: Nested if if (num > 0) printf(“positive”);else if (num < 0) printf(“negative”); else /* num is zero */ printf(“zero”);

Example: Nested if if(age<2) printf(“Infant”); elseif(age<18) printf(“Child”); else /* age >= 18 */ printf(“Adult”);

/* Print a message if all criteria are met */ if (marital_status == ‘S’) if (gender == ‘M’) if (age >= 18 && age = 18 && age <= 26) printf(“All criteria are met.”); IS EQUIVALENT TO if (marital_status == ‘S’ && gender == ‘M’&& age >= 18 && age = 18 && age <= 26) printf(“All criteria are met.”); Example: Nested if

Notes on Cascaded if if (number >= 5) { if(number >= 10) if(number >= 10){ if (number < 20) printf(“S1”);elseprintf(“S2”);}elseprintf(“S3);printf(“S4”);}elseprintf(“S5”); What is the output if: number is equal to 3 number is equal to 5 number is equal to 10 number is equal to 35 Q:

Common Mistake if(age>2) /* if greater than 2 */ if(age<18) /* and less than 18 */ printf (“Child”); /* it's a child */ else printf( “Infant”); /* ERROR: inappropriate response */ if(age>2) /* if greater than 2 */ { if(age<18) /* and less than 18 */ printf (“Child”); /* it's a child */ } else printf( “Infant”);

CONDITIONAL OPERATOR Conditional Operator (?:) is ternary operator (demands 3 operands), and is used in certain situations, replacing if- else condition phrases. Conditional Operator (?:) is ternary operator (demands 3 operands), and is used in certain situations, replacing if- else condition phrases. Conditional operator’s shape is: Conditional operator’s shape is: –Condition_phrase ? phrase1 : phrase2; If conditional_phrase is true, phrase1 is executed and whole phrase is assigned with value of this phrase1. If the conditional phrase is false, then phrase2 is executed, and whole phrase is assigned with phrase2 value. If conditional_phrase is true, phrase1 is executed and whole phrase is assigned with value of this phrase1. If the conditional phrase is false, then phrase2 is executed, and whole phrase is assigned with phrase2 value.

Example - CONDITIONAL OPERATOR Example - CONDITIONAL OPERATOR int a, b, c;... c = a > b ? a : b; IS THE SAME AS if(a > b) c = a; else c = b;

Multiple Choice: switch and break If choosing more than 2 alternatives you have use if…else if…else If choosing more than 2 alternatives you have use if…else if…else Another alternative is the switch statement Another alternative is the switch statement

switch example switch(day){ case 1: printf(“Monday”); break; case 2: printf(“Tuesday”); break; case 3: printf(“Wednesday”); break; case 4: printf(“Thursday”); break; case 5: printf(“Friday”); break; case 6: printf(“Saturday”); break; case 7: printf(“Sunday”); break; case default: printf(“Day number should be between 1 and 7”); }

Syntax for switch statement switch(controlling expression) { label set 1 : statements 1 ; break; label set 2 : statements 2 ; break;... label set n : statements n ; break;default: statements d ; }

Program flow in switch statements

switch example switch(class){ case ‘B’: case ‘b’: printf(“Battleship”); break; case ‘C’: case ‘c’: printf(“Cruiser”); break; case ‘D’: case ‘d’: printf(“Destroyer”); break; case ‘F’: case ‘f’: printf(“Frigate”); break; default: printf(“unknown ship class %c”,class); }

Notes on switch statement Controlling expression may be of type int or char, but not float or double Controlling expression may be of type int or char, but not float or double An equivalent statement using if…else if statements can be written for each switch statement, but not vice versa An equivalent statement using if…else if statements can be written for each switch statement, but not vice versa

switch example switch(month){ case 3: case 4: case 5: printf(“Spring”); break; case 6: case 7: case 8: printf(“Summer”); break; case 9: case 10: case 11: printf(“Autumn”); break; case 12: case 1: case 2: printf(“Winter”); break; default: printf(“month must be between 1 and 12”); }

Same example using if…else if statements if (month==3 || month==4 || month==5) printf(“Spring”); else if (month==6 || month==7 || month==8) printf(“Summer”); else if (month==9 || month==10 || month==11) printf(“Autumn”); else if (month==12 || month==1 || month==2) printf(“Winter”);else printf(“month must be between 1 and 12”); }

switch and if else When should you use a switch and when should you use the if else construction? When should you use a switch and when should you use the if else construction? –Often you don't have a choice. You can't use a switch if your choice is based on evaluating a float or a double variable or expression You can't use a switch if your choice is based on evaluating a float or a double variable or expression Nor can you conveniently use a switch if a variable must fall into a certain range. It is simple to write the following: Nor can you conveniently use a switch if a variable must fall into a certain range. It is simple to write the following: –if (integer 2)

Summary Type int as Boolean Type int as Boolean Nested if statements Nested if statements switch statement switch statement