Introduction to Computing Lecture 05: Selection (continued)

Similar documents
DECISION CONTROL AND LOOPING STATEMENTS

Boolean Data-Type. Boolean Data Type (false, true) i.e. 3/6/2018. The type bool is also described as being an integer: bool bflag; bflag = true;

Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II)

Computer Science & Engineering 150A Problem Solving Using Computers

Relational & Logical Operators, if and switch Statements

CMSC 104 -Lecture 11 John Y. Park, adapted by C Grasso

Computer Programming Lecture 12 Pointers

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++

COMP-202: Foundations of Programming. Lecture 8: for Loops, Nested Loops and Arrays Jackie Cheung, Winter 2016

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

1.4 Control Structures: Selection. Department of CSE

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

Other conditional and loop constructs. Fundamentals of Computer Science Keith Vertanen

Control Structure: Selection

Programming Language. Control Structures: Selection (switch) Eng. Anis Nazer First Semester

Introduction to Computing Lecture 03: Basic input / output operations

Introduction to Programming

Topics. Chapter 5. Equality Operators

Materials covered in this lecture are: A. Completing Ch. 2 Objectives: Example of 6 steps (RCMACT) for solving a problem.

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

printf( Please enter another number: ); scanf( %d, &num2);

if Statement Numeric Rela5onal Operators The if Statement Flow of Control: Branching (Savitch, Chapter 3)

Computer Programming Lecture 13 Functions with Multiple Output Parameters

if Statement Numeric Rela7onal Operators The if Statement Flow of Control: Branching (Savitch, Chapter 3)

CHAPTER : 9 FLOW OF CONTROL

Control Structures in Java if-else and switch

CSCI312 Principles of Programming Languages!

Topic 5: Enumerated Types and Switch Statements

Structures and Pointers

Example. CS 201 Selection Structures (2) and Repetition. Nested if Statements with More Than One Variable

Introduction to Computing Lecture 09: Functions (Part II)

BBM 101 Introduc/on to Programming I Fall 2013, Lecture 4

COMP-202: Foundations of Programming. Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015

Decision Making -Branching. Class Incharge: S. Sasirekha

Flow Control. CSC215 Lecture

CS349/SE382 A1 C Programming Tutorial

BRANCHING if-else statements

Chapter 3, Selection. Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved.

CompSci 125 Lecture 11

Introduction to Computing Lecture 08: Functions (Part I)

1.3b Type Conversion

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

CHAPTER 5 FLOW OF CONTROL

Arrays III and Enumerated Types

Michele Van Dyne Museum 204B CSCI 136: Fundamentals of Computer Science II, Spring

Laboratory 4. INSTRUCTIONS (part II) I. THEORETICAL BACKGROUND

Darshan Institute of Engineering & Technology for Diploma Studies Unit 5

Maltepe University Computer Engineering Department. Algorithms and Programming. Chapter 4: Conditionals - If statement - Switch statement

BBM#101# #Introduc/on#to# Programming#I# Fall$2014,$Lecture$4$

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

C 101. Davide Vernizzi. April 29, 2011

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

Control Structure: Loop

Physics 2660: Fundamentals of Scientific Computing. Lecture 5 Instructor: Prof. Chris Neu

Administration. Conditional Statements. Agenda. Syntax. Flow of control. Lab 2 due now on floppy Lab 3 due tomorrow via FTP

Data Types and Variables in C language

Control Structures in Java if-else and switch

Enters system mode. Example The following example creates a scheduler named maintenancesched and commits the transaction:

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Armstrong State University Engineering Studies MATLAB Marina Switch-Case Statements Primer

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

M1-R4: Programing and Problem Solving using C (JAN 2019)

View a Students Schedule Through Student Services Trigger:

Simplified Operating Instructions T105-C / T108-C / T106-C / T17B-C DISPLAYS DAY OR DAY BLOCK PROG 00:0000 PROG PROG ALWAYS OFF

ECE 122. Engineering Problem Solving with Java

Computer Programming I - Unit 5 Lecture page 1 of 14

Lecture 14 CSE11 Fall 2013 For loops, Do While, Break, Continue

Problem Solving. Problem Solving Concept for Computer Science

Informatics 1 Functional Programming Lectures 13 and 14 Monday 11 and Tuesday 12 November Type Classes. Don Sannella University of Edinburgh

Decision Making and Loops

22c:111 Programming Language Concepts. Fall Types I

ECE15: Introduction to Computer Programming Using the C Language. Lecture Unit 4: Flow of Control

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

Physics 2660: Fundamentals of Scientific Computing. Lecture 3 Instructor: Prof. Chris Neu

More Programming Constructs -- Introduction

Chapter 3. Section 3.10 Type of Expressions and Automatic Conversion. CS 50 Hathairat Rattanasook

Novell Sample Paper. 1. On December 31, which ships will be sailing from the Port on a New Year's Eve.

Control Statements Loops

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

CSCI 135 Software Design and Analysis, C++ Homework 2 Due 2/21/2014

Computer Programming. Decision Making (2) Loops

COMP 208 Computers in Engineering

Lecture 3 Tao Wang 1

CS313D: ADVANCED PROGRAMMING LANGUAGE

Lecture 12. Data Types and Strings

Using Variables to Write Pattern Rules

Structured Programming. Dr. Mohamed Khedr Lecture 9

Example. Section: PS 709 Examples of Calculations of Reduced Hours of Work Last Revised: February 2017 Last Reviewed: February 2017 Next Review:

Chapter 5: Control Structures

AQA Decision 1 Algorithms. Section 1: Communicating an algorithm

COP 2000 Introduction to Computer Programming Mid-Term Exam Review

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

CSCI 131, Midterm Exam 1 Review Questions This sheet is intended to help you prepare for the first exam in this course. The following topics have

Introduction to C++ General Rules, Conventions and Styles CS 16: Solving Problems with Computers I Lecture #2

Introduction to Computing Lecture 10 Arrays (Part 1)

CS 105 Lecture 5 Logical Operators; Switch Statement. Wed, Feb 16, 2011, 5:11 pm

Information Science 1

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Advance mode: Auto

Lecture 6. Statements

Transcription:

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

Topics Type int as Boolean Nested if statements switch statement

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

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

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

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

Example: What is the output? #include <stdio.h> /* Test some Booleans. */ int main() int whatever = 0; if (whatever) printf( Is that true, Homer?\n ); printf( No, Marge.\n ); return 0;

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

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

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

Example: What is the output? #include <stdio.h> /* Test some Booleans. */ Behavior is undefined int main() float whatever = 0.003; if (whatever) printf( Is that true, Homer?\n ); printf( No, Marge.\n ); return 0;

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

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

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

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

Notes on Cascaded if Q: 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 if (number >= 5) if(number >= 10) if (number < 20) printf( S1 ); printf( S2 ); printf( S3); printf( S4 ); printf( S5 );

Common Mistake if(age>2) /* if greater than 2 */ if(age<18) /* and less than 18 */ printf ( Child ); /* it's a child */ 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 */ printf( Infant );

CONDITIONAL OPERATOR Conditional Operator (?:) is ternary operator (demands 3 operands), and is used in certain situations, replacing if condition phrases. 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.

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

Multiple Choice: switch and break If choosing more than 2 alternatives you have use if if 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 An equivalent statement using if 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 if statements if (month==3 month==4 month==5) printf( Spring ); if (month==6 month==7 month==8) printf( Summer ); if (month==9 month==10 month==11) printf( Autumn ); if (month==12 month==1 month==2) printf( Winter ); printf( month must be between 1 and 12 );

switch and if When should you use a switch and when should you use the if 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 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 < 1000 && integer > 2)

Summary Type int as Boolean Nested if statements switch statement