Control Structure: Selection

Size: px
Start display at page:

Download "Control Structure: Selection"

Transcription

1 Control Structure: Selection Knowledge: Understand various concepts of selection control structure Skill: Be able to develop a program containing selection control structure

2 Selection Structure Single Selection Multiple Selection Double Selection

3 Single Selection Pseudocode Structure step a if <condition is true> start step m step n end_if step x

4 Single Selection Step a condition true Step m false Step n Step x

5 Exercise Develop an algorithm for the following problem: A system to check/verify the eligibility of a person to vote during the election is going to be developed. The input for the system is the person s age. If the age is greater than 17 years old, then display You are eligible to vote and Your age is <age> years old messages. If the age is less than 17 years old, display Your age is <age> years old message only.

6 Exercise - Answer start Assume the age is 12 years old age 12 > 17? FALSE! age > 17 true You are eligible to vote false Your age is 12 years Your age is <age> old years old end

7 Something to ponder What if.. the age is 37 years old? What about 20 years old?

8 Let s recap Pseudocode Structure Single Selection step a if <condition is true> start step m step n end_if step x Double selection has similar structure except that

9 Double Selection Pseudocode Structure step a if <condition is true> start step m step n end_if else start step x step y end_else step z additional steps at the bottom

10 Double Selection Step a condition false Step x Step y true Step m Step n Step z

11 Exercise Develop an algorithm for the following problem: A system to check/verify the eligibility of a person to vote during the election is going to be developed. The input for the system is the person s age. If the age is greater than 20 years old, then display You are eligible to vote and Your age is <age> years old messages. If the age is less than 20 years old, display You are not eligible to vote and Your age is <age> years old messages.

12 Exercise - Answer Verify this flowchart! start age false age > 17 You are not eligible to vote true You are eligible to vote Your age is <age> years old end

13 Let s recap Pseudocode Structure Pseudocode Structure step a step if <condition a is true> start step m Double Selection Single Selection if <condition is true> start step n end_if step m else start step n end_if step x step y step end_else x step z Guess how does multiple selection look like?

14 Multiple Selection Pseudocode Structure (Multi-way if) step a if <condition_1 is true> start step m end_if if <condition_2 is true> start step n end_if step z

15 Multiple Selection Step a Condition1 false Condition2 true true Step m Step n false Step z

16 Multiple Selection Pseudocode Structure (Cascaded if) step a if <condition_1 is true> start step m end_if if <condition_2 is true> start step n end_if else start step x end_else step z

17 Multiple Selection Step a Condition1 false Condition2 false Step x true true Step m Step n Step z

18 Exercise Develop a flowchart for the following problem. Given a mark, determine its grade based on the table below: 86 <= mark <= 100 grade = A 71 <=mark <= 85 grade = B 56 <=mark <= 70 grade = C 41 <=mark <= 55 grade = D 0 <=mark <= 40 grade = E others error message

19 if Statements if if else - if if - else

20 if Statement The structure is similar to single selection (flowchart) Syntax: if (expression) statement; or if (expression) { statement1; statement2; } Don t forget the brackets!! Don t forget the curly brackets!!

21 if Statement The similarity between single selection structure and if statement: Single Selection: if <condition is true> start end_if step 1 step 2 step k if Statement: if (<condition>) { } statement 1 statement 2 statement k

22 if Statement Example: int num1, num2, min; printf( Key-in 2 numbers: ); scanf( %d %d, &num1, &num2); min = num1; if (num1 > num2) min = num2; printf( \nsmallest: %d\n, min); 20 > 15? 20 num1? 15 num2? min? Key-in 2 numbers: _ _ Smallest: 15 _

23 if Statement Example: int mark; printf( Mark: ); scanf( %d, &mark); if (mark > 85) { } Mark: _ 92 printf( Category: Excellent\n ); printf( Congratulations! ); _ Category: Excellent Congratulations! 92 > 85? mark 92?

24 if Statement } Example: void main() { int mark; printf( Mark: ); scanf( %d, &mark); if (mark >= 40) printf( Pass\n ); What will the output be if the mark is 65? printf( Your mark is %d, mark);

25 if Statement Example: } void main() { int mark; printf( Mark: ); scanf( %d, &mark); if (mark >= 40) What will the output be if the mark is 35? printf( Pass\n ); printf( Your mark is %d, mark);

26 if - else Statement The structure is similar to double selection (flowchart) Syntax: if (expression) statement; else statement; or if (expression) { statement1; statement2; } else statement3;

27 if - else Statement or if (expression) { statement1; statement2; } else { statement3; statement4; }

28 if else Statement The similarity between double selection structure and if - else statement: Double Selection: if <condition is true> start step 1 step k end_if else start step 1 step n end_else if Statement: if <condition> { statement 1 statement k } else { statement 1 statement n }

29 if - else Statement Example: if (num1 < num2) min = num1; else min = num2; printf( Smallest: %d\n, min); 10 < 15? num1 10 num min? _ Smallest: 10 _

30 if - else Statement Example: if (num1 < num2) min = num1; else min = num2; printf( Smallest: %d\n, min); 20 < 15? num1 20 num min? _ Smallest: 15 _

31 if - else Statement Example: num1 700 if (num1 < num2) { 700 < 125? min = num1; max = num2; num2 125 } else { min 125?? min = num2; max = num1; max 700?? } printf( Min = %d, Max = %d\n, min, max); _ Min = 125, Max = 700 _

32 if else Statement Example: void main() { int mark; What will the output be if the mark is 21? } printf( Mark: ); scanf( %d, &mark); if (mark >= 40) printf( Pass\n ); else printf( Fail\n ); printf( Your mark is %d, mark); What will the output be if the mark is 74?

33 if else Statement Example: void main() { int mark; What will the output be if the mark is 74? } printf( Mark: ); scanf( %d, &mark); if (mark >= 40) printf( Pass\n ); else printf( Fail\n ); printf( Your mark is %d, mark); What will the output be if the mark is 14?

34 if else Statement Example: void main() { What will the output int mark; be if the mark is 14? printf( Mark: ); scanf( %d, &mark); What will the output if (mark >= 40) be if the mark is 70? printf( Pass\n ); else { printf( Fail\n ); printf( Your mark is %d, mark); } }

35 Take a break. Let s have a minute break. While you re having a break, let me introduce you some program styles.. Why we need program styles?.to ensure your program is readable. Let s look some examples..

36 Take a break (Learn styles) Example: void main() { int mark; printf( Mark: ); scanf( %d, &mark); if (mark >= 50) printf( Pass\n ); } else printf( Fail\n ); printf( Your mark is %d, mark);

37 Take a break (Learn styles) } Example: void main() { int mark; printf( Mark: ); scanf( %d, &mark); if (mark >= 50) printf( Pass\n ); else printf( Fail\n ); printf( Your mark is %d, mark); Difficult to read!!! Don t you think so??

38 Let s recap Syntax: if (expression) statement; else statement; if-else statement Ok now, let s look at if else if statement

39 if else - if Statement Syntax: if (expression) statement; else if (expression) statement; else if (expression) statement; else statement; if-else-if statement

40 if else - if Statement Syntax: if (expression) statement; else if (expression) statement; else if (expression) statement; else (expression) statement; if-else-if statement Be careful common mistake made by students!!

41 Let s recap Example: if <condition_1 is true> start step m end_if if <condition_2 is true> start step n end_if else start step x end_else Multiple Selection step Assume m, step condition. will 1 be is true, executed so

42 Let s recap Example: if <condition_1 is true> start step m step Assume m, step condition. will 1 be is end_if skipped, false, and so if <condition_2 is true> start step n condition 2 will be tested end_if else start step x end_else Multiple Selection

43 Let s recap Example: if <condition_1 is true> start step m end_if if <condition_2 is true> start step n end_if else start step x end_else Multiple Selection step Assume n, step condition. will 2 be is true, executed so

44 Let s recap Multiple Selection Example: if <condition_1 is true> start step m end_if if <condition_2 is true> start step n end_if Assume step n, condition step. will 2 is be also else start skipped, false, and so step x step x will be executed end_else

45 Multiple Selection in C Example: #include <stdio.h> int main( ) { char letter; } scanf( %c, &letter); if (letter >= a && letter <= z ) printf( Lower case\n ); else if (letter >= A && letter <= Z ) printf( Upper case\n ); else if (letter >= 0 && letter <= 9 ) printf( Digit\n ); else printf( Special character\n ); Is the letter a lower case? Is the letter an upper case? Is the letter a digit?

46 Multiple Selection in C Example: #include <stdio.h> int main( ) { char letter; } scanf( %c, &letter); if (letter >= a && letter <= z ) printf( Lower case\n ); else if (letter >= A && letter <= Z ) printf( Upper case\n ); else if (letter >= 0 && letter <= 9 ) printf( Digit\n ); else printf( Special character\n ); (the letter is a lower case) true (the letter is a lower case) false (the letter is is an upper case) false true (the letter is a digit) true (the letter is a lower case) false (the letter is an upper case) false (the letter is a digit) false

47 Exercise Develop a program for the following problem. Given a mark, determine its grade based on the table below: 86 <= mark <= 100 grade = A 71 <=mark <= 85 grade = B 56 <=mark <= 70 grade = C 41 <=mark <= 55 grade = D 0 <=mark <= 40 grade = E others error message

48 A n s w e r 1 int mark; printf( Key-in the mark: ); scanf( %d,&mark); if ((mark >= 86) && (mark <= 100)) printf("grade = A ); else if ((mark >= 71) && (mark <= 85)) printf(" Grade = B ); else if ((mark >= 56) && (mark <= 70)) printf(" Grade = C ); else if ((mark >= 41) && (mark <= 55)) printf(" Grade = D ); else if ((mark >= 0) && (mark <= 40)) printf(" Grade = E ); else printf( Input salah\n );

49 A n s w e r 2 int mark; char grade ; printf( Key-in the mark : ); scanf( %d,&mark); if ((mark >= 86) && (mark <= 100)) printf("grade = A ); else if ((mark >= 71) && (mark <= 85)) printf(" Grade = B ); else if ((mark >= 56) && (mark <= 70)) printf(" Grade = C ); else if ((mark >= 41) && (mark <= 55)) printf(" Grade = D ); else if ((mark >= 0) && (mark <= 40)) printf(" Grade = E ); else printf( Input salah\n ); printf( Your grade is %c, grade );

50 A n s w e r 3 int mark; char grade; printf( Key-in the mark: ); scanf( %d,&mark); if ((mark >= 86) && (mark <= 100)) grade= A ; else if ((mark >= 71) && (mark <= 85)) grade= B ; else if ((mark >= 56) && (mark <= 70)) grade= C ; else if ((mark >= 41) && (mark <= 55)) grade= D ; else if ((mark >= 0) && (mark <= 40)) grade= E ; if ((mark > 100) (mark < 0)) printf( Input salah\n ); else printf( Your grade is %c, grade);

51 Nested ifs if statement that contains other if / if - else statements

52 Nested ifs Example: if (age > 18) { if (age > 55) } else { } else This price is valid for people: people: 18 age < age > 55 < 55 price = 2.50; /* Price for senior citizens */ This price is valid for price = 5.00; /* Price for people: adults age */ < 1 This price is valid for people: 1 < age < 18 if (age < 1) price = 0.0; /* Price for infants */ else price = 1.50; /* for children & teenagers*/

53 Nested ifs - Problem Example: if (age > 18) if (age > 55) price = 2.50; /* Price for senior citizens */ else price = 5.00; /* Price for adults */ Which if does this else belong to?

54 Nested ifs - Problem Which one? if (age > 18) { if (age > 55) price = 2.50; } else price = 5.00; if (age > 18) { if (age > 55) price = 2.50; else price = 5.00; }

55 Nested ifs - Problem By default, else will be attached to the nearest if if (age > 18) else if (age > 55) price = 2.50; price = 5.00; if (age > 18) { } if (age > 55) else price = 2.50; price = 5.00;

56 switch and break The structure is similar to multiple selection (flowchart) Syntax: switch switch (expression) (expression) { { case expression1: statement1; break; case espression2: statement2; break; default: expressionx; break; } Don t forget the brackets!! Don t forget the curly colons brackets!!!!

57 switch and break Important Rule!! Syntax: switch (expression) { case expression1: statement1; break; case espression2: statement2; break; default: expressionx; break; } Must be INTEGER or CHAR!

58 switch and break Example: switch (month) { case 1: printf( January\n ); break; case 2: printf( February\n ); break; case 3: default: printf( March\n ); break; printf( Others\n ); break; } printf( End ); Assume month = 1, so this step will be case executed. is terminated Later here. Jump to January _ End _

59 switch and break Example: switch (month) { March case 1: _ End _ printf( January\n ); break; case 2: printf( February\n ); break; case 3: printf( March\n ); break; default: printf( Others\n ); break; } printf( End ); Assume month = 3, so this step will be case executed. is terminated Later here. Jump to

60 switch and break Example: switch (month) { case 1: printf( January\n ); break; case 2: printf( February\n ); break; case 3: printf( March\n ); break; default: printf( Others\n ); break; } } printf( End ); printf( End ); Now what more!! will happen if this break is taken out from the program?

61 switch and break Example: switch (month) { February case 1: _ March printf( January\n ); break; End case 2: printf( February\n ); case 3: printf( March\n ); break; default: printf( Others\n ); break; } printf( End ); execution continues. Thus, this step is executed. So Assume month = 2, so this step will be executed. Later case is terminated here. Jump to

62 switch and break Example: switch (month) { case 1: printf( January\n ); break; case 2: printf( February\n ); And if month = 34 1?? case 3: printf( March\n ); break; default: printf( Others\n ); break; } printf( End ); Now what will happen if these breaks are taken out from the program?

63 Exercise To practice what you ve learned, try exercises in lab

64 End of Lecture 5 At last, chap 5 is finished. What s next??? Control Structure: Repetition on the way

Chapter 5: Control Structures

Chapter 5: Control Structures Chapter 5: Control Structures In this chapter you will learn about: Sequential structure Selection structure if if else switch Repetition Structure while do while for Continue and break statements S1 2017/18

More information

Decision Making -Branching. Class Incharge: S. Sasirekha

Decision Making -Branching. Class Incharge: S. Sasirekha Decision Making -Branching Class Incharge: S. Sasirekha Branching The C language programs presented until now follows a sequential form of execution of statements. Many times it is required to alter the

More information

Control Structure: Loop

Control Structure: Loop Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop control structure 1 Loop Structure Condition is tested first

More information

Selection Control Structure CSC128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING

Selection Control Structure CSC128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING Selection Control Structure CSC128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING MULTIPLE SELECTION To solve a problem that has several selection, use either of the following method: Multiple selection nested

More information

COMP s1 Lecture 1

COMP s1 Lecture 1 COMP1511 18s1 Lecture 1 1 Numbers In, Numbers Out Andrew Bennett more printf variables scanf 2 Before we begin introduce yourself to the person sitting next to you why did

More information

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting Your factors.c and multtable.c files are due by Wednesday, 11:59 pm, to be submitted on the SoC handin page at http://handin.cs.clemson.edu.

More information

Control Structures in Java if-else and switch

Control Structures in Java if-else and switch Control Structures in Java if-else and switch Lecture 4 CGS 3416 Spring 2017 January 23, 2017 Lecture 4CGS 3416 Spring 2017 Selection January 23, 2017 1 / 26 Control Flow Control flow refers to the specification

More information

DECISION CONTROL AND LOOPING STATEMENTS

DECISION CONTROL AND LOOPING STATEMENTS DECISION CONTROL AND LOOPING STATEMENTS DECISION CONTROL STATEMENTS Decision control statements are used to alter the flow of a sequence of instructions. These statements help to jump from one part of

More information

Introduction to Programming

Introduction to Programming Introduction to Programming ( 數 ) Lecture 4 Spring 2005 March 11, 2005 Topics Review of if statement The switch statement Repetition and Loop Statements For-Loop Condition-Loop Reading: Chap. 5.7~ Chap.

More information

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

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan Lecture 08-1 Programming in C++ PART 1 By Assistant Professor Dr. Ali Kattan 1 The Conditional Operator The conditional operator is similar to the if..else statement but has a shorter format. This is useful

More information

Computer Science & Engineering 150A Problem Solving Using Computers

Computer Science & Engineering 150A Problem Solving Using Computers Computer Science & Engineering 150A Problem Solving Using Computers Lecture 04 - Conditionals Stephen Scott (Adapted from Christopher M. Bourke) Fall 2009 1 / 1 cbourke@cse.unl.edu Control Structure Conditions

More information

Decision Structures. Lecture 3 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Decision Structures. Lecture 3 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Decision Structures Lecture 3 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Chapter Topics o Relational Operators o The if Statement o The if-else Statement o Nested if statements o The if-else-if

More information

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions. Introduction In the programs that we have dealt with so far, all statements inside the main function were executed in sequence as they appeared, one after the other. This type of sequencing is adequate

More information

Flow Control. CSC215 Lecture

Flow Control. CSC215 Lecture Flow Control CSC215 Lecture Outline Blocks and compound statements Conditional statements if - statement if-else - statement switch - statement? : opertator Nested conditional statements Repetitive statements

More information

Decision Structures. Lesson 03 MIT 11053, Fundamentals of Programming

Decision Structures. Lesson 03 MIT 11053, Fundamentals of Programming Decision Structures Lesson 03 MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz M.Sc. in IS (SLIIT), PGD in IS (SLIIT), BBA (Hons.) Spl. In IS (SEUSL), MIEEE, Microsoft Certified Professional

More information

C Programming Decision Making

C Programming Decision Making 1 P a ge C Programming, Decision Making C Programming Decision Making Introduction to Decision Making OR Control Statement OR Branching Statement: - We know that the execution of program is done instruction

More information

Q1: Multiple choice / 20 Q2: C input/output; operators / 40 Q3: Conditional statements / 40 TOTAL SCORE / 100 EXTRA CREDIT / 10

Q1: Multiple choice / 20 Q2: C input/output; operators / 40 Q3: Conditional statements / 40 TOTAL SCORE / 100 EXTRA CREDIT / 10 EECE.2160: ECE Application Programming Spring 2016 Exam 1 February 19, 2016 Name: Section (circle 1): 201 (8-8:50, P. Li) 202 (12-12:50, M. Geiger) For this exam, you may use only one 8.5 x 11 double-sided

More information

Topics. Chapter 5. Equality Operators

Topics. Chapter 5. Equality Operators Topics Chapter 5 Flow of Control Part 1: Selection Forming Conditions if/ Statements Comparing Floating-Point Numbers Comparing Objects The equals Method String Comparison Methods The Conditional Operator

More information

BITG 1223: Selection Control Structure by: ZARITA (FTMK) LECTURE 4 (Sem 1, 16/17)

BITG 1223: Selection Control Structure by: ZARITA (FTMK) LECTURE 4 (Sem 1, 16/17) BITG 1223: Selection Control Structure by: ZARITA (FTMK) LECTURE 4 (Sem 1, 16/17) 1 Learning Outcomes At the end of this lecture, you should be able to: 1. Explain the concept of selection control structure

More information

Lecture 8. Daily Puzzle

Lecture 8. Daily Puzzle Lecture 8 if and switch Statements Daily Puzzle If a bottle, partly filled with liquid, has a round, square or rectangular bottom which is flat, can you find its volume using only a ruler? You may not

More information

Pseudocode syntax, descriptions and examples

Pseudocode syntax, descriptions and examples Pseudocode syntax, descriptions and examples Overview: This table provides a reference for commonly used pseudocode for introductory computer program design courses. You should use this as your reference

More information

BRANCHING if-else statements

BRANCHING if-else statements BRANCHING if-else statements Conditional Statements A conditional statement lets us choose which statement t t will be executed next Therefore they are sometimes called selection statements Conditional

More information

Introduction to C Programming

Introduction to C Programming 1 2 Introduction to C Programming 2.6 Decision Making: Equality and Relational Operators 2 Executable statements Perform actions (calculations, input/output of data) Perform decisions - May want to print

More information

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from INTRODUCTION TO C++ PROGRAM CONTROL Original slides are from http://sites.google.com/site/progntut/ Dept. of Electronic Engineering, NCHU Outline 2 Repetition Statement for while do.. while break and continue

More information

EECE.2160: ECE Application Programming Spring 2016 Exam 1 Solution

EECE.2160: ECE Application Programming Spring 2016 Exam 1 Solution EECE.2160: ECE Application Programming Spring 2016 Exam 1 Solution 1. (20 points, 5 points per part) Multiple choice For each of the multiple choice questions below, clearly indicate your response by circling

More information

Following is the general form of a typical decision making structure found in most of the programming languages:

Following is the general form of a typical decision making structure found in most of the programming languages: Decision Making Decision making structures have one or more conditions to be evaluated or tested by the program, along with a statement or statements that are to be executed if the condition is determined

More information

Selection Structures II

Selection Structures II Lecture 7 Selection Structures II Nested if and switch statements CptS 121 Summer 2016 Armen Abnousi If () if() Nested if statements An if- statement can be placed inside another if- statement. Every matches

More information

Note: If only one statement is to be followed by the if or else condition then there is no need of parenthesis.

Note: If only one statement is to be followed by the if or else condition then there is no need of parenthesis. Birla Institute of Technology & Science, Pilani Computer Programming (CSF111) Lab-4 ---------------------------------------------------------------------------------------------------------------------------------

More information

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

CMSC 104 -Lecture 9 John Y. Park, adapted by C Grasso CMSC 104 -Lecture 9 John Y. Park, adapted by C Grasso 1 Topics The while Loop Program Versatility Sentinel Values and Priming Reads Checking User Input Using a while Loop 2 A repetition structureallows

More information

Lesson #4. Logical Operators and Selection Statements. 4. Logical Operators and Selection Statements - Copyright Denis Hamelin - Ryerson University

Lesson #4. Logical Operators and Selection Statements. 4. Logical Operators and Selection Statements - Copyright Denis Hamelin - Ryerson University Lesson #4 Logical Operators and Selection Statements Control Structures Control structures combine individual instructions into a single logical unit with one entry point at the top and one exit point

More information

Decision Making and Loops

Decision Making and Loops Decision Making and Loops Goals of this section Continue looking at decision structures - switch control structures -if-else-if control structures Introduce looping -while loop -do-while loop -simple for

More information

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double ME 172 Lecture 2 variables scanf() printf() 07/03/2011 ME 172 1 Data Types and Modifier Basic data types are char int float double Modifiers signed unsigned short Long 07/03/2011 ME 172 2 1 Data Types

More information

Welcome! COMP s1. Programming Fundamentals

Welcome! COMP s1. Programming Fundamentals Welcome! 0 COMP1511 18s1 Programming Fundamentals COMP1511 18s1 Lecture 7 1 Strings Andrew Bennett chars arrays of chars strings 2 Before we begin introduce yourself to the

More information

Welcome! COMP s1 Lecture 7. COMP s1. Before we begin. Strings. Programming Fundamentals. Overview. Andrew Bennett

Welcome! COMP s1 Lecture 7. COMP s1. Before we begin. Strings. Programming Fundamentals. Overview. Andrew Bennett Welcome! COMP1511 18s1 Programming Fundamentals 0 COMP1511 18s1 Lecture 7 Strings Andrew Bennett 1 chars arrays of chars strings Before we begin 2 Overview after this lecture,

More information

Introduction to Computing Lecture 05: Selection (continued)

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 nukhet.ozbek@ege.edu.tr Topics Type int as Boolean

More information

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

Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Advance mode: Auto CS 170 Java Programming 1 The Switch Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Menu-Style Code With ladder-style if-else else-if, you might sometimes find yourself writing menu-style

More information

Chapter 4 - Notes Control Structures I (Selection)

Chapter 4 - Notes Control Structures I (Selection) Chapter 4 - Notes Control Structures I (Selection) I. Control Structures A. Three Ways to Process a Program 1. In Sequence: Starts at the beginning and follows the statements in order 2. Selectively (by

More information

Computer Programming. Decision Making (2) Loops

Computer Programming. Decision Making (2) Loops Computer Programming Decision Making (2) Loops Topics The Conditional Execution of C Statements (review) Making a Decision (review) If Statement (review) Switch-case Repeating Statements while loop Examples

More information

Repetition Structures

Repetition Structures Repetition Structures Chapter 5 Fall 2016, CSUS Introduction to Repetition Structures Chapter 5.1 1 Introduction to Repetition Structures A repetition structure causes a statement or set of statements

More information

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution. Repetition Structures Introduction to Repetition Structures Chapter 5 Spring 2016, CSUS Chapter 5.1 Introduction to Repetition Structures The Problems with Duplicate Code A repetition structure causes

More information

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs PDS Lab Section 16 Autumn-2017 Tutorial 3 C Programming Constructs This flowchart shows how to find the roots of a Quadratic equation Ax 2 +Bx+C = 0 Start Input A,B,C x B 2 4AC False x If 0 True B x 2A

More information

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode STUDENT OUTLINE Lesson 8: Structured Programming, Control Structures, if- Statements, Pseudocode INTRODUCTION: This lesson is the first of four covering the standard control structures of a high-level

More information

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

BBM 101 Introduc/on to Programming I Fall 2013, Lecture 4 BBM 101 Introduc/on to Programming I Fall 2013, Lecture 4 Instructors: Aykut Erdem, Erkut Erdem, Fuat Akal TAs: Ahmet Selman Bozkir, Gultekin Isik, Yasin Sahin 1 Today Condi/onal Branching Logical Expressions

More information

Introduction. C provides two styles of flow control:

Introduction. C provides two styles of flow control: Introduction C provides two styles of flow control: Branching Looping Branching is deciding what actions to take and looping is deciding how many times to take a certain action. Branching constructs: if

More information

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

printf( Please enter another number: ); scanf( %d, &num2); CIT 593 Intro to Computer Systems Lecture #13 (11/1/12) Now that we've looked at how an assembly language program runs on a computer, we're ready to move up a level and start working with more powerful

More information

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

Administration. Conditional Statements. Agenda. Syntax. Flow of control. Lab 2 due now on floppy Lab 3 due tomorrow via FTP Administration Conditional Statements CS 99 Summer 2000 Michael Clarkson Lecture 4 Lab 2 due now on floppy Lab 3 due tomorrow via FTP need Instruct account password Lab 4 posted this afternoon Prelim 1

More information

CpSc 1111 Lab 4 Formatting and Flow Control

CpSc 1111 Lab 4 Formatting and Flow Control CpSc 1111 Lab 4 Formatting and Flow Control Overview By the end of the lab, you will be able to: use fscanf() to accept a character input from the user and print out the ASCII decimal, octal, and hexadecimal

More information

Lecture 5 Tao Wang 1

Lecture 5 Tao Wang 1 Lecture 5 Tao Wang 1 Objectives In this chapter, you will learn about: Selection criteria Relational operators Logical operators The if-else statement Nested if statements C++ for Engineers and Scientists,

More information

REPETITION CONTROL STRUCTURE LOGO

REPETITION CONTROL STRUCTURE LOGO CSC 128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING REPETITION CONTROL STRUCTURE 1 Contents 1 Introduction 2 for loop 3 while loop 4 do while loop 2 Introduction It is used when a statement or a block of

More information

CpSc 1111 Lab 5 Formatting and Flow Control

CpSc 1111 Lab 5 Formatting and Flow Control CpSc 1111 Lab 5 Formatting and Flow Control Overview By the end of the lab, you will be able to: use fscanf() to accept a character input from the user execute a basic block iteratively using loops to

More information

Computers and FORTRAN Language Fortran 95/2003. Dr. Isaac Gang Tuesday March 1, 2011 Lecture 3 notes. Topics:

Computers and FORTRAN Language Fortran 95/2003. Dr. Isaac Gang Tuesday March 1, 2011 Lecture 3 notes. Topics: Computers and FORTRAN Language Fortran 95/2003 Dr. Isaac Gang Tuesday March 1, 2011 Lecture 3 notes Topics: - Program Design - Logical Operators - Logical Variables - Control Statements Any FORTRAN program

More information

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

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003 Control Flow COMS W1007 Introduction to Computer Science Christopher Conway 3 June 2003 Overflow from Last Time: Why Types? Assembly code is typeless. You can take any 32 bits in memory, say this is an

More information

n Group of statements that are executed repeatedly while some condition remains true

n Group of statements that are executed repeatedly while some condition remains true Looping 1 Loops n Group of statements that are executed repeatedly while some condition remains true n Each execution of the group of statements is called an iteration of the loop 2 Example counter 1,

More information

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

Materials covered in this lecture are: A. Completing Ch. 2 Objectives: Example of 6 steps (RCMACT) for solving a problem. 60-140-1 Lecture for Thursday, Sept. 18, 2014. *** Dear 60-140-1 class, I am posting this lecture I would have given tomorrow, Thursday, Sept. 18, 2014 so you can read and continue with learning the course

More information

Pointers and scanf() Steven R. Bagley

Pointers and scanf() Steven R. Bagley Pointers and scanf() Steven R. Bagley Recap Programs are a series of statements Defined in functions Can call functions to alter program flow if statement can determine whether code gets run Loops can

More information

In this lab, you will learn more about selection statements. You will get familiar to

In this lab, you will learn more about selection statements. You will get familiar to Objective: In this lab, you will learn more about selection statements. You will get familiar to nested if and switch statements. Nested if Statements: When you use if or if...else statement, you can write

More information

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 5. Repetition in Programs. Notes. Notes. Notes. Lecture 05 - Loops

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 5. Repetition in Programs. Notes. Notes. Notes. Lecture 05 - Loops Computer Science & Engineering 150A Problem Solving Using Computers Lecture 05 - Loops Stephen Scott (Adapted from Christopher M. Bourke) 1 / 1 Fall 2009 cbourke@cse.unl.edu Chapter 5 5.1 Repetition in

More information

5. Selection: If and Switch Controls

5. Selection: If and Switch Controls Computer Science I CS 135 5. Selection: If and Switch Controls René Doursat Department of Computer Science & Engineering University of Nevada, Reno Fall 2005 Computer Science I CS 135 0. Course Presentation

More information

What we have learned so far

What we have learned so far What we have learned so far Straight forward statements Conditional statements (branching) Repeated statements (loop) Arrays One variable many data Problem: Read 10 numbers from the keyboard and store

More information

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;

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; Programming in C++ If Statements If the sun is shining Choice Statements if (the sun is shining) go to the beach; True Beach False Class go to class; End If 2 1 Boolean Data Type (false, ) i.e. bool bflag;

More information

Topic 6: A Quick Intro To C

Topic 6: A Quick Intro To C Topic 6: A Quick Intro To C Assumption: All of you know Java. Much of C syntax is the same. Also: Many of you have used C or C++. Goal for this topic: you can write & run a simple C program basic functions

More information

CpSc 1011 Lab 5 Conditional Statements, Loops, ASCII code, and Redirecting Input Characters and Hurricanes

CpSc 1011 Lab 5 Conditional Statements, Loops, ASCII code, and Redirecting Input Characters and Hurricanes CpSc 1011 Lab 5 Conditional Statements, Loops, ASCII code, and Redirecting Input Characters and Hurricanes Overview For this lab, you will use: one or more of the conditional statements explained below

More information

Flow of Control. Selection. if statement. True and False in C False is represented by any zero value. switch

Flow of Control. Selection. if statement. True and False in C False is represented by any zero value. switch Flow of Control True and False in C Conditional Execution Iteration Nested Code(Nested-ifs, Nested-loops) Jumps 1 True and False in C False is represented by any zero value The int expression having the

More information

Welcome! COMP s1. Programming Fundamentals

Welcome! COMP s1. Programming Fundamentals Welcome! 0 COMP1511 18s1 Programming Fundamentals COMP1511 18s1 Lecture 5 1 More Loops Andrew Bennett while loops loops inside loops stopping loops 2 Before we begin introduce

More information

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

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program

More information

Lecture 6. Statements

Lecture 6. Statements Lecture 6 Statements 1 Statements This chapter introduces the various forms of C++ statements for composing programs You will learn about Expressions Composed instructions Decision instructions Loop instructions

More information

LAB 4.1 Relational Operators and the if Statement

LAB 4.1 Relational Operators and the if Statement LAB 4.1 Relational Operators and the if Statement // This program tests whether or not an initialized value of num2 // is equal to a value of num1 input by the user. int main( ) int num1, // num1 is not

More information

Assoc. Prof. Dr. Tansu FİLİK

Assoc. Prof. Dr. Tansu FİLİK Assoc. Prof. Dr. Tansu FİLİK Previously on Bil 200 Low-Level I/O getchar, putchar, getc, putc Control Structures if-else, switch-case, loops (while, dowhile, for loops) for loop (generic) A disturbingly

More information

Relational operators (1)

Relational operators (1) Review-2 Control of flow: ifs & loops How to set them up Where to break to When to use which kind 85-132 Introduction to C-Programming 10-1 Relational operators (1) Relational Operators

More information

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

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017 Control Structures Lecture 4 COP 3014 Fall 2017 September 18, 2017 Control Flow Control flow refers to the specification of the order in which the individual statements, instructions or function calls

More information

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur Lecture 18 Switch Statement (Contd.) And Introduction to

More information

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information Laboratory 2: Programming Basics and Variables Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information 3. Comment: a. name your program with extension.c b. use o option to specify

More information

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program Syntax What the Compiler needs to understand your program 1 Pre-Processing Any line that starts with # is a pre-processor directive Pre-processor consumes that entire line Possibly replacing it with other

More information

C: How to Program. Week /Mar/05

C: How to Program. Week /Mar/05 1 C: How to Program Week 2 2007/Mar/05 Chapter 2 - Introduction to C Programming 2 Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers

More information

Q1: Multiple choice / 20 Q2: C input/output; operators / 40 Q3: Conditional statements / 40 TOTAL SCORE / 100 EXTRA CREDIT / 10

Q1: Multiple choice / 20 Q2: C input/output; operators / 40 Q3: Conditional statements / 40 TOTAL SCORE / 100 EXTRA CREDIT / 10 16.216: ECE Application Programming Spring 2015 Exam 1 February 23, 2015 Name: ID #: For this exam, you may use only one 8.5 x 11 double-sided page of notes. All electronic devices (e.g., calculators,

More information

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University Unit 1 Programming Language and Overview of C 1. State whether the following statements are true or false. a. Every line in a C program should end with a semicolon. b. In C language lowercase letters are

More information

Arrays Arrays and pointers Loops and performance Array comparison Strings. John Edgar 2

Arrays Arrays and pointers Loops and performance Array comparison Strings. John Edgar 2 CMPT 125 Arrays Arrays and pointers Loops and performance Array comparison Strings John Edgar 2 Python a sequence of data access elements with [index] index from [0] to [len-1] dynamic length heterogeneous

More information

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

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15 Table of Contents 1 INTRODUCTION... 1 2 IF... 1 2.1 BOOLEAN EXPRESSIONS... 3 2.2 BLOCKS... 3 2.3 IF-ELSE... 4 2.4 NESTING... 5 3 SWITCH (SOMETIMES KNOWN AS CASE )... 6 3.1 A BIT ABOUT BREAK... 7 4 CONDITIONAL

More information

Decision making with if Statement : - Control Statements. Introduction: -

Decision making with if Statement : - Control Statements. Introduction: - Control Statements Introduction: - Any C program if you consider, the set of statements are normally executed sequentially in the order in which they are written, and such programs have sequential structure

More information

Assoc. Prof. Dr. Tansu FİLİK

Assoc. Prof. Dr. Tansu FİLİK Assoc. Prof. Dr. Tansu FİLİK Previously on Bil 200 Operators Logical and arithmetic operators Control Structures if-else mechanism Low-level I/O They are fast! Might be useful for console applications.

More information

Loops / Repetition Statements

Loops / Repetition Statements Loops / Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops C has three kinds of repetition statements: the while loop the for

More information

Control Structures in Java if-else and switch

Control Structures in Java if-else and switch Control Structures in Java if-else and switch Lecture 4 CGS 3416 Spring 2016 February 2, 2016 Control Flow Control flow refers to the specification of the order in which the individual statements, instructions

More information

MODULE 2: Branching and Looping

MODULE 2: Branching and Looping MODULE 2: Branching and Looping I. Statements in C are of following types: 1. Simple statements: Statements that ends with semicolon 2. Compound statements: are also called as block. Statements written

More information

Module 4: Decision-making and forming loops

Module 4: Decision-making and forming loops 1 Module 4: Decision-making and forming loops 1. Introduction 2. Decision making 2.1. Simple if statement 2.2. The if else Statement 2.3. Nested if Statement 3. The switch case 4. Forming loops 4.1. The

More information

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

Physics 2660: Fundamentals of Scientific Computing. Lecture 5 Instructor: Prof. Chris Neu Physics 2660: Fundamentals of Scientific Computing Lecture 5 Instructor: Prof. Chris Neu (chris.neu@virginia.edu) Reminder I am back! HW04 due Thursday 22 Feb electronically by noon HW grades are coming.

More information

Computer Science & Engineering 150A Problem Solving Using Computers

Computer Science & Engineering 150A Problem Solving Using Computers Computer Science & Engineering 150A Problem Solving Using Computers Lecture 06 - Stephen Scott Adapted from Christopher M. Bourke 1 / 30 Fall 2009 Chapter 8 8.1 Declaring and 8.2 Array Subscripts 8.3 Using

More information

CSE123 LECTURE 3-1. Program Design and Control Structures Repetitions (Loops) 1-1

CSE123 LECTURE 3-1. Program Design and Control Structures Repetitions (Loops) 1-1 CSE123 LECTURE 3-1 Program Design and Control Structures Repetitions (Loops) 1-1 The Essentials of Repetition Loop Group of instructions computer executes repeatedly while some condition remains true Counter-controlled

More information

Unit 3 Decision making, Looping and Arrays

Unit 3 Decision making, Looping and Arrays Unit 3 Decision making, Looping and Arrays Decision Making During programming, we have a number of situations where we may have to change the order of execution of statements based on certain conditions.

More information

CpSc 111 Lab 5 Conditional Statements, Loops, the Math Library, and Redirecting Input

CpSc 111 Lab 5 Conditional Statements, Loops, the Math Library, and Redirecting Input CpSc Lab 5 Conditional Statements, Loops, the Math Library, and Redirecting Input Overview For this lab, you will use: one or more of the conditional statements explained below scanf() or fscanf() to read

More information

Chapter 2 - Introduction to C Programming

Chapter 2 - Introduction to C Programming Chapter 2 - Introduction to C Programming 2 Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers 2.4 Memory Concepts 2.5 Arithmetic

More information

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU CSE101-lec#12 Designing Structured Programs Introduction to Functions Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU Outline Designing structured programs in C: Counter-controlled repetition

More information

COMP 208 Computers in Engineering

COMP 208 Computers in Engineering COMP 208 Computers in Engineering Lecture 14 Jun Wang School of Computer Science McGill University Fall 2007 COMP 208 - Lecture 14 1 Review: basics of C C is case sensitive 2 types of comments: /* */,

More information

CS1100 Introduction to Programming

CS1100 Introduction to Programming Decisions with Variables CS1100 Introduction to Programming Selection Statements Madhu Mutyam Department of Computer Science and Engineering Indian Institute of Technology Madras Course Material SD, SB,

More information

CSCE150A. Introduction. While Loop. Compound Assignment. For Loop. Loop Design. Nested Loops. Do-While Loop. Programming Tips CSCE150A.

CSCE150A. Introduction. While Loop. Compound Assignment. For Loop. Loop Design. Nested Loops. Do-While Loop. Programming Tips CSCE150A. Chapter 5 While For 1 / 54 Computer Science & Engineering 150A Problem Solving Using Computers Lecture 05 - s Stephen Scott (Adapted from Christopher M. Bourke) Fall 2009 While For 2 / 54 5.1 Repetition

More information

Structured Program Development

Structured Program Development Structured Program Development Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Outline Introduction The selection statement if if.else switch The

More information

Loops / Repetition Statements

Loops / Repetition Statements Loops / Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops C has three kinds of repetition statements: the while loop the for

More information

CS113: Lecture 4. Topics: Functions. Function Activation Records

CS113: Lecture 4. Topics: Functions. Function Activation Records CS113: Lecture 4 Topics: Functions Function Activation Records 1 Why functions? Functions add no expressive power to the C language in a formal sense. Why have them? Breaking tasks into smaller ones make

More information

Score score < score < score < 65 Score < 50

Score score < score < score < 65 Score < 50 What if we need to write a code segment to assign letter grades based on exam scores according to the following rules. Write this using if-only. How to use if-else correctly in this example? score Score

More information

Welcome! COMP s1. Programming Fundamentals

Welcome! COMP s1. Programming Fundamentals Welcome! 0 COMP1511 18s1 Programming Fundamentals COMP1511 18s1 Lecture 4 1 More Functions + Loops Andrew Bennett even more functions while loops 2 Before we begin introduce

More information

At the end of this lecture you should be able to have a basic overview of fundamental structures in C and be ready to go into details.

At the end of this lecture you should be able to have a basic overview of fundamental structures in C and be ready to go into details. Objective of this lecture: At the end of this lecture you should be able to have a basic overview of fundamental structures in C and be ready to go into details. Fundamental Programming Structures in C

More information