Introduction to Programming

Size: px
Start display at page:

Download "Introduction to Programming"

Transcription

1 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. 6 1

2 Review: if (if-else statements) if-else if (expr) Statement1; else Statement2; if-else 流 false expr true Statement2 Statement1 int main() int num; Use Logical Operators printf("please input a number between 1 to 100 : "); scanf("%d", &num); if (num >= 1) if (num <= 100) //nested if printf("valid number %d\n",num); else printf("invalid number %d\n",num); else printf("invalid number %d\n",num); printf("bye Bye!\n"); system("pause"); return 0; If ((num >= 1) && (num <= 100)) 2

3 NESTED IF with relational op if (( time >= 0) && (time < 12)) printf( Good Morning ); else if ((time >= 12) && (time < 18)) printf( Good Afternoon ); else if ((time >= 18) && (time < 24.)) printf( Good Evening ); else printf( Time is out of range ); NESTED IF with logical op if ( x == 5 ) if ( y == 5 ) printf ( Both are 5. \n ) ; else printf ( x is 5, but y is not. \n ) ; else if ( y == 5 ) printf ( y is 5, but x is not. \n ) ; else printf ( Neither is 5. \n ) ; Income Tax Example income < 15,000 < 15,000 15,000, < 30,000 30,000, < 50,000 50,000, < 100, ,000 tax 0% 18% 22% 28% 31% if ( income < ) printf( No tax. ); if ( income >= && income < ) printf( 18%% tax. ); if ( income >= && income < ) printf( 22%% tax. ); if ( income >= && income < ) printf( 28%% tax. ); if ( income >=100000) printf( 31%% tax. ); 3

4 Cascaded ifs if ( income < ) if ( income < ) printf( No tax ); printf( No tax ); else else if ( income < ) if ( income < ) printf( 18%% tax. ); printf( 18%% tax. ); else if ( income < ) else printf( 22%% tax. ); if ( income < ) else if ( income < ) printf( 22%% tax. ); printf( 28%% tax. ); else else if ( income < ) printf( 31%% tax. ); printf( 28%% tax. ); else printf( 31%% tax. ); Order is important. Conditions are evaluated in order given. DeMorgan s Laws DeMorgan s laws help determine when two complex conditions are equivalent They state:! ( P && Q ) is equivalent to (!P!Q )! ( P Q ) is equivalent to (!P &&!Q ) This applies for any Boolean expressions P and Q, which might themselves be complex expressions if (! (age < 25 && sex == M ) ) printf ( Cheap rates. \n ) ; is equivalent to (?) if ( age >= 25 sex!= M ) ) printf ( Cheap rates. \n ) ; 4

5 Gotcha! = versus == int a = 2 ; (C Has No Boolean values) if ( a = 1 ) /* semantic (logic) error! */ printf ( a is one\n ) ; else if ( a == 2 ) printf ( a is two\n ) ; else printf ( a is %d\n, a) ; Multiple Selection So far, we have only seen binary selection. if ( age >= 18 ) printf( Vote!\n ) ; if ( age >= 18 ) printf( Vote!\n ) ; else printf( Maybe next time!\n ) ; 5

6 練 3 數 流 流 6

7 狀 if 3 數 狀 if 3 數 7

8 狀 if 3 數 狀 if 3 數 8

9 行 Multiple Selection (con t) Sometimes it is necessary to branch in more than two directions. We do this via multiple selection. The multiple selection mechanism in C is the switch statement. 9

10 Multiple Selection with if if (day == 0 ) printf ( Sunday ) ; if (day == 1 ) printf ( Monday ) ; if (day == 2) printf ( Tuesday ) ; if (day == 3) printf ( Wednesday ) ; (continued) if (day == 4) printf ( Thursday ) ; if (day == 5) printf ( Friday ) ; if (day == 6) printf ( Saturday ) ; if ((day < 0) (day > 6)) printf( Error - invalid day.\n ) ; Multiple Selection with if-else if (day == 0 ) printf ( Sunday ) ; else if (day == 1 ) printf ( Monday ) ; else if (day == 2) printf ( Tuesday ) ; else if (day == 3) printf ( Wednesday ) ; else if (day == 4) printf ( Thursday ) ; else if (day == 5) printf ( Friday ) ; else if (day == 6) printf ( Saturday ) ; else printf ( Error - invalid day.\n ) ; This if-else structure is more efficient than the corresponding if structure. Why? 10

11 The switch Multiple-Selection Structure switch ( integer expression ) case constant 1 : statement(s) break ; case constant 2 : statement(s) break ;... default: statement(s) break ; switch Example switch ( day ) case 0: printf ( Sunday\n ) ; break ; case 1: printf ( Monday\n ) ; break ; case 2: printf ( Tuesday\n ) ; break ; case 3: printf ( Wednesday\n ) ; break ; case 4: printf ( Thursday\n ) ; break ; case 5: printf ( Friday\n ) ; break ; case 6: printf ( Saturday\n ) ; break ; default: printf ( Error -- invalid day.\n ) ; break ; Is this structure more efficient than the equivalent nested if-else structure? 11

12 switch Statement Details The last statement of each case in the switch should almost always be a break. The break causes program control to jump to the closing brace of the switch structure. Without the break, the code flows into the next case. This is almost never what you want. A switch statement will compile without a default case, but always consider using one. Include a default case to catch invalid data. Inform the user of the type of error that has occurred (e.g., Error - invalid day. ). A Different Switch Example #include <stdio.h> int main(void) int mo; scanf("%d", &mo); /* get month number from user */ switch (mo) case 2: printf("28 day month"); break; case 4: /* 30 days hath sep, apr, jun, nov */ case 6: case 9: case 11: printf("30 day month"); break; default: printf("31 day month"); break; return 0; 12

13 Why Use a switch Statement? A nested if-else structure is just as efficient as a switch statement. However, a switch statement may be easier to read. Also, it is easier to add new cases to a switch statement than to a nested if-else structure. The char Data Type The char data type holds a single character. char ch; Example assignments: char grade, symbol; grade = B ; symbol = $ ; The char is held as a one-byte integer in memory. The ASCII code is what is actually stored, so we can use them as characters or integers, depending on our need. 13

14 The char Data Type (con t) Use scanf ( %c, &ch) ; to read a single character into the variable ch. (Note that the variable does not have to be called ch. ) Use printf( %c, ch) ; to display the value of a character variable. #include <stdio.h> int main ( ) char ch ; char Example printf ( Enter a character: ) ; scanf ( %c, &ch) ; printf ( The value of %c is %d.\n, ch, ch) ; return 0 ; If the user entered an A, the output would be: The value of A is

15 switch statement with chars Example: switch (c) case '+' : i = i+2; break; case '-' : i = i-2; break; case '*' : i = i*2; break; case '/' : i = i/2; break; default: i = 0; switch statement ---- 例 int main() char grade; printf( Input grade : "); scanf("%c", &grade); switch (grade) case a : case A : printf( Excellent \n ); break; case b : case B : printf( Good \n ); break; case c : case C : printf( Be study hard \n ); break; default : printf( Failed\n ); return 0; 15

16 例 switch statement 力 類 度 度 度 度 契 力 度 度 度 度 度 類 度數 switch statement 例 2 流 類 / / Yes 度數 No 列 類 契 0~300 度 /301 度 力 C 0~300 度 /301 度 300 度 301 度 300 度 301 度 = 度數 *3.3 =300*3.3+ ( 度數 -300)*4.2 = 度數 *(1.9) +150*C = 度數 *6 =300*6+ ( 度數 -300)*6.8 16

17 switch statement 例 2 () 類 類 度數 契 力 if (T>=1 && T<=3) printf(" 度數 = "); scanf("%f", &Deg); /* */ Ch2_1_2.c switch statement 例 2 switch(t) case 1: if (Deg<=100) Fee = Deg*3.3; else Fee = (Deg-300)* *3.3; break; case 2: printf(" 契 力 = "); scanf("%f", &C); Fee = C*150 + Deg*1.9; break; case 3: if (Deg<=300) Fee = Deg*6; else Fee = (Deg-300)* *6; break; 17

18 例 2 switch statement printf(" %f", Fee); printf("\n"); else printf(" 類!"); printf("\n"); 練 if switch 流 18

19 if switch if switch 19

20 if switch if switch 20

21 if switch if switch 21

22 if switch 行 22

23 Repetition Statements (Repetition statements, Loop, Iteration ): ( ) 行 C Loop statements for loop: (Counting loop) while loop: (Condition loop) do-while loop: (Condition loop) Reading: Chap. 5 while loop Syntax for while loop while ( expr ) statements; next-statements; Condition: true of false expr: children = 7; cookies = 1; while ( children > 0 ) children = children - 1 ; cookies = cookies * 2 ; 23

24 while loop Flow chart for while loop expr false true statements Note: C has no boolean value; C uses int for boolean values 0 for false and non-zero for true next-statements while Loop: Example 1 Problem: Write a program that calculates the average exam grade for a class of 10 (or n) students. What are the program inputs? the exam grades What are the program outputs? the average exam grade 24

25 The Pseudocode <total> = 0 <grade_counter> = 1 // 錄 數 While (<grade_counter> <= 10) Display Enter a grade: Read <grade> <total> = <total> + <grade> <grade_counter> = <grade_counter> + 1 End_while <average> = <total> / 10 Display Class average is:, <average> #include <stdio.h> int main ( ) int counter, grade, total, average ; total = 0 ; counter = 1 ; while ( counter <= 10 ) printf ( Enter a grade : ) ; scanf ( %d, &grade) ; The C Code total = total + grade ; counter = counter + 1 ; average = total / 10 ; printf ( Class average is: %d\n, average) ; return 0 ; 25

26 Versatile? How versatile is this program? It only works with class sizes of 10. We would like it to work with any class size. A better way : Ask the user how many students are in the class. Use that number in the condition of the while loop and when computing the average. <total> = 0 <grade_counter> = 1 New Pseudocode Display Enter the number of students: Read <num_students> While (<grade_counter> <= <num_students>) Display Enter a grade: Read <grade> <total> = <total> + <grade> <grade_counter> = <grade_counter> + 1 End_while <average> = <total> / <num_students> Display Class average is:, <average> 26

27 New C Code #include <stdio.h> int main ( ) int numstudents, counter, grade, total, average ; total = 0 ; counter = 1 ; printf ( Enter the number of students: ) ; scanf ( %d, &numstudents) ; while ( counter <= numstudents) printf ( Enter a grade : ) ; scanf ( %d, &grade) ; total = total + grade ; counter = counter + 1 ; average = total / numstudents ; printf ( Class average is: %d\n, average) ; return 0 ; Why Bother to Make It Easier? Why do we write programs? So the user can perform some task The more versatile the program, the more difficult it is to write. BUT it is more useable. The more complex the task, the more difficult it is to write. But that is often what a user needs. Always consider the user first. 27

28 Using a Sentinel Value We could let the user keep entering grades and when he s done enter some special value that signals us that he s done. This special signal value is called a sentinel value. We have to make sure that the value we choose as the sentinel isn t a legal value. For example, we can t use 0 as the sentinel in our example as it is a legal value for an exam score. The Priming Read When we use a sentinel value to control a while loop, we have to get the first value from the user before we encounter the loop so that it will be tested and the loop can be entered. This is known as a priming read. We have to give significant thought to the initialization of variables, the sentinel value, and getting into the loop. 28

29 New Pseudocode( ) <total> = 0 <grade_counter> = 1 Display Enter a grade: Priming read Read <grade> While ( <grade>!= SENTINEL ) <total> = <total> + <grade> <grade_counter> = <grade_counter> + 1 Display Enter another grade: Read <grade> End_while <average> = <total> / <grade_counter> Display Class average is:, <average> New C Code #include <stdio.h> #define SENTINEL -99 int main ( ) int counter, grade, total, average ; total = 0 ; counter = 1 ; printf( Enter a grade: ) ; scanf( %d, &grade) ; while (grade!= SENTINEL) total = total + grade ; counter = counter + 1 ; printf( Enter another grade: ) ; scanf( %d, &grade) ; average = total / counter ; printf ( Class average is: %d\n, average) ; return 0 ; 29

30 Final Clean C Code #include <stdio.h> #define SENTINEL -99 int main ( ) int counter ; /* counts number of grades entered */ int grade ; /* individual grade */ int total; /* total of all grades */ int average ; /* average grade */ /* Initializations */ total = 0 ; counter = 1 ; Final Clean C Code (con t) /* Get grades from user */ /* Compute grade total and number of grades */ printf( Enter a grade: ) ; scanf( %d, &grade) ; while (grade!= SENTINEL) total = total + grade ; counter = counter + 1 ; printf( Enter another grade (or %d to quit):, SENTINEL) ; scanf( %d, &grade) ; User friendly /* Compute and display the average grade */ average = total / counter ; printf ( Class average is: %d\n, average) ; return 0 ; 30

31 while loop: Example 2 gcd(i, j) = gcd(j, i%j) Start j!= 0? temp = i % j; i = j; j = temp; End while loop: gcd example #include<stdio.h> void main() int i, j, tmp; printf("\1: Please input two positive integer for finding their GCD:\n"); scanf("%d %d", &i, &j); while ( j!= 0 ) tmp = i % j; i = j; j = tmp; printf("\2: The GCD is %d \n",i); // 數? // 料 31

32 Using a while Loop to Check User Input #include <stdio.h> int main ( ) int number ; printf ( Enter a positive integer : ) ; scanf ( %d, &number) ; priming read while ( number <= 0 ) printf ( \nthat s incorrect. Try again.\n ) ; printf ( Enter a positive integer: ) ; scanf ( %d, &number) ; printf ( You entered: %d\n, number) ; return 0 ; The do-while Repetition Structure do statement(s) while ( condition ) ; The body of a do-while is ALWAYS executed at least once. 32

33 Check User Input Using do-while do printf ( Enter a positive number: ) ; scanf ( %d, &num) ; if ( num <= 0 ) printf ( \nthat is not positive. Try again\n ) ; while ( num <= 0 ) ; do printf ( Enter a letter from A through Z: ) ; scanf ( %c, &letter) ; while ( letter < A letter > Z ) ; Counter-Controlled loop: for loop * * condition-controlled loop 33

34 Counter-Controlled Repetition (Definite Repetition) If it is known in advance exactly how many times a loop will execute, it is known as a countercontrolled loop. int i = 1 ; while ( i <= 10 ) printf( i = %d\n, i) ; i = i + 1 ; Counter-Controlled Repetition (con t) Is the following loop a counter-controlled loop? while ( x!= y ) printf( x = %d, x) ; x = x + 2 ; 34

35 Event-Controlled Repetition (Indefinite Repetition) If it is NOT known in advance exactly how many times a loop will execute, it is known as an eventcontrolled loop or condition-controlled loop. sum = 0 ; printf( Enter an integer value: ) ; scanf( %d, &value) ; while ( value!= -1) sum = sum + value ; printf( Enter another value: ) ; scanf( %d, &value) ; Event-Controlled Repetition (con t) An event-controlled loop will terminate when some event occurs (which makes the condition true). The event may be the occurrence of a sentinel value, as in the previous example. There are other types of events that may occur, such as reaching the end of a data file. 35

36 The 3 Parts of a Loop #include <stdio.h> int main () int i = 1 ; initialization of loop control variable /* count from 1 to 100 */ while ( i < 101 ) test of loop termination condition printf ( %d, i) ; i = i + 1 ; modification of loop control variable return 0 ; The for Loop Repetition Structure The for loop handles details of the counter-controlled loop automatically. The initialization of the the loop control variable, the termination condition test, and control variable modification are handled in the for loop structure. for ( i = 1; i < 101; i = i + 1) initialization modification test 36

37 for for (for loop) for ( expr1 ; expr2 ; expr3 ) statements; next-statements; 數 行 數 --for (for loop) 流 for 流 expr1 expr2 false true statements expr3 next-statements 37

38 When Does a for-loop Initialize, Test and Modify? Just as with a while loop, a for loop initializes the loop control variable before beginning the first loop iteration, modifies the loop control variable at the very end of each iteration of the loop, and performs the loop termination test before each iteration of the loop. The for loop is easier to write and read for counter-controlled loops. Various forms of for loop counting for ( i = 0; i < 10; i = i + 1 ) printf ( %d\n, i) ; for ( i = 9; i >= 0; i = i - 1 ) printf ( %d\n, i) ; for ( i = 0; i < 10; i = i + 2 ) printf ( %d\n, i) ; for ( i = 9; i >= 0; i = i - 2 ) printf ( %d\n, i) ; 38

39 for loop--- 例 例 累 100 #include<stdio.h> k = 1 main() int i, sum=0; /* 數 i sum 數 */ for (i=1; i<=100; i=i+1) sum= sum+ i; printf( sum = %d \n, sum); /* sum */ k 流 for loop--- 例 數 累 流 說 理 / 數 N Sum=0 i=1 i<=n? true false 流 i=i+1 39

40 例 for loop--- 例 2 #include<stdio.h> main() int i,n, sum=0; printf( Please input an integer: ); scanf( %d, &N ); for (i=1; i<=n; i=i+1) sum= sum+ i; printf( %d = %d \n,n, sum); /* 數 */ / N*/ /* sum */ for loop--- 例 3 例 f(n)=n*(n-1)*(n-2)*(n-3) *3*2*1 -- 數 Factorial /* * Computes n! * Pre-condition: n is greater than or equal to zero */ Int factorial(int n) int i, /* local variables */ product; /* accumulator for product computation */ product = 1; /* Computes the product n x (n-1) x (n-2) x... x 2 x 1 */ for (i = n; i >= 1; --i) product = product * i; /* Returns function result */ return (product); --i; i = i -1; i 40

41 C --i, i--, ++i, i++ The increment operator ++ The decrement operator -- If we want to add one to a variable, we can say: count = count + 1; (count = count - 1;) Programs often contain statements that increment variables, so to save on typing, C provides these shortcuts: count++; (count--;) OR ++count; (--count;) Both do the same thing. They change the value of count by adding one to it. *But post or pre increment (decrement) have different implications. (come back later) Nested Loops Loops inside other loops! 41

42 Nested for loops-- 例 例 #include<stdio.h> main() int i, j, k; for (i=1; i<=9; i++) for (j=1; j<=9; j++) k = i*j; printf("%d*%d = %2d ", i, j, k); 行 : Nested for loop--- 例 1*1= 1 1*2= 2 1*3= 3 1*4= 4 1*5= 5 1*6= 6 1*7= 7 1*8= 8 1*9= 9 2*1= 2 2*2= 4 2*3 = 6 2*4= 8 2*5=10 2*6=12 2*7=14 2*8=16 2*9=18 3*1= 3 3*2= 6 3*3= 9 3*4=12 3*5=15 3*6=18 3*7=21 3*8=24 3*9=27 4*1= 4 4*2= 8 4*3=12 4*4=16 4*5=20 4*6=24 4*7=28 4* 8=32 4*9=36 5*1= 5 5*2=10 5*3=15 5*4=20 5*5=25 5*6=30 5*7=35 5*8=40 5*9=45 6*1= 6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 6*7=42 6*8=48 6*9=54 7*1= 7 7*2=14 7*3=21 7 *4=28 7*5=35 7*6=42 7*7=49 7*8=56 7*9=63 8*1= 8 8*2=16 8*3=24 8*4=32 8*5=40 8*6= 48 8*7=56 8*8=64 8*9=72 9*1= 9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81 若 列 42

43 Revised Nested for loops-- 例 例 #include<stdio.h> main() int i, j, k; for (i=1; i<=9; i++) for (j=1; j<=9; j++) k = i*j; printf("%d*%d = %2d ", i, j, k); printf( \n ); Nested for loops-- 例 for ( i = 1; i < 5; i = i + 1 ) for ( j = 1; j < 5; j = j + 1 ) if ( j % 2 == 0 ) printf ( O ) ; else printf ( X ) ; printf ( \n ) ; How many times is the if statement executed? What is the output? XOXO XOXO XOXO XOXO 43

44 Nested for loops-- 例 for ( i = 1; i < 7; i = i + 1 ) for ( j = i; j < 7; j = j + 1 ) if ( j % 2 == 0 ) printf ( O ) ; else printf ( X ) ; printf ( \n ) ; How many times is the if statement executed? What is the output? XOXOXO OXOXO XOXO OXO XO O Figure 5.14 Validating Input Using nested do-while statement 44

45 Nested for loop---lab 練 練 狀 列 列 // 8 rows # ### ##### ####### ######### ########### ############# ############### // 7 spaces // 2 spaces // 1 space // 0 space // 15 # Assignment 2 1. nested for loops 年 (2005) 45

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

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

CMSC 104 -Lecture 11 John Y. Park, adapted by C Grasso CMSC 104 -Lecture 11 John Y. Park, adapted by C Grasso 1 Topics Multiple Selection vs Binary Selection switch Statement char data type and getchar( ) function Reading newline characters 2 So far, we have

More information

Review: Repetition Structure

Review: Repetition Structure Loops 1 Topics The while Loop Program Versatility Sentinel Values and Priming Reads Checking User Input Using a while Loop Counter-Controlled (Definite) Repetition Event-Controlled (Indefinite) Repetition

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

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

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

Repetition Structures II

Repetition Structures II Lecture 9 Repetition Structures II For and do-while loops CptS 121 Summer 2016 Armen Abnousi Types of Control Structures Sequential All programs that we have written so far The statements inside a pair

More information

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University ITC213: STRUCTURED PROGRAMMING Bhaskar Shrestha National College of Computer Studies Tribhuvan University Lecture 08: Control Statements Readings: Chapter 6 Control Statements and Their Types A control

More information

Control Structure: Selection

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

More information

1.4 Control Structures: Selection. Department of CSE

1.4 Control Structures: Selection. Department of CSE 1.4 Control Structures: Selection 1 Department of CSE Objectives To understand how decisions are made in a computer To understand the relational operators To understand the logical operators and,or and

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

Chapter 3 Structured Program Development

Chapter 3 Structured Program Development 1 Chapter 3 Structured Program Development Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 3 - Structured Program Development Outline 3.1 Introduction

More information

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018. Week 2 Branching & Looping Gaddis: Chapters 4 & 5 CS 5301 Spring 2018 Jill Seaman 1 Relational Operators l relational operators (result is bool): == Equal to (do not use =)!= Not equal to > Greater than

More information

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

Programming Language. Control Structures: Selection (switch) Eng. Anis Nazer First Semester Programming Language Control Structures: Selection (switch) Eng. Anis Nazer First Semester 2018-2019 Multiple selection choose one of two things if/else choose one from many things multiple selection using

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

STUDENT LESSON A12 Iterations

STUDENT LESSON A12 Iterations STUDENT LESSON A12 Iterations Java Curriculum for AP Computer Science, Student Lesson A12 1 STUDENT LESSON A12 Iterations INTRODUCTION: Solving problems on a computer very often requires a repetition of

More information

Dept. of CSE, IIT KGP

Dept. of CSE, IIT KGP Control Flow: Looping CS10001: Programming & Data Structures Pallab Dasgupta Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur Types of Repeated Execution Loop: Group of

More information

Repetition and Loop Statements Chapter 5

Repetition and Loop Statements Chapter 5 Repetition and Loop Statements Chapter 5 1 Chapter Objectives To understand why repetition is an important control structure in programming To learn about loop control variables and the three steps needed

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

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

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

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

Branching is deciding what actions to take and Looping is deciding how many times to take a certain action.

Branching is deciding what actions to take and Looping is deciding how many times to take a certain action. 3.0 Control Statements in C Statements The statements of a C program control the flow of program execution. A statement is a command given to the computer that instructs the computer to take a specific

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

Sudeshna Sarkar Dept. of Computer Science & Engineering. Indian Institute of Technology Kharagpur

Sudeshna Sarkar Dept. of Computer Science & Engineering. Indian Institute of Technology Kharagpur Programming and Data Structure Sudeshna Sarkar Dept. of Computer Science & Engineering. Indian Institute of Technology Kharagpur Shortcuts in Assignment Statements A+=C A=A+C A-=B A=A-B A*=D A=A*D A/=E

More information

Subject: PIC Chapter 2.

Subject: PIC Chapter 2. 02 Decision making 2.1 Decision making and branching if statement (if, if-, -if ladder, nested if-) Switch case statement, break statement. (14M) 2.2 Decision making and looping while, do, do-while statements

More information

UNIT 4. CONTROL FLOW. Programming Year Grade in Industrial Technology Engineering. Paula de Toledo. David Griol

UNIT 4. CONTROL FLOW. Programming Year Grade in Industrial Technology Engineering. Paula de Toledo. David Griol Programming Unit 4. Control flow Haga clic para modificar el estilo de texto del patrón UNIT 4. CONTROL FLOW Programming Year 2017-2018 Grade in Industrial Technology Engineering Paula de Toledo. David

More information

while for do while ! set a counter variable to 0 ! increment it inside the loop (each iteration)

while for do while ! set a counter variable to 0 ! increment it inside the loop (each iteration) Week 7: Advanced Loops while Loops in C++ (review) while (expression) may be a compound (a block: {s) Gaddis: 5.7-12 CS 1428 Fall 2015 Jill Seaman 1 for if expression is true, is executed, repeat equivalent

More information

Conditional Statement

Conditional Statement Conditional Statement 1 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition Also called Branching How do we specify conditions?

More information

COP 2000 Introduction to Computer Programming Mid-Term Exam Review

COP 2000 Introduction to Computer Programming Mid-Term Exam Review he exam format will be different from the online quizzes. It will be written on the test paper with questions similar to those shown on the following pages. he exam will be closed book, but students can

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 Computing Lecture 07: Repetition and Loop Statements (Part II)

Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II) Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II) Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics Engineering nukhet.ozbek@ege.edu.tr Topics

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

Relational & Logical Operators, if and switch Statements

Relational & Logical Operators, if and switch Statements 1 Relational & Logical Operators, if and switch Statements Topics Relational Operators and Expressions The if Statement The if- Statement Nesting of if- Statements switch Logical Operators and Expressions

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

CSE 142 Programming I

CSE 142 Programming I CSE 142 Programming I Conditionals Chapter 4 lread Sections 4.1 4.5, 4.7 4.9 lthe book assumes that you ve read chapter 3 on functions Read it anyway, you should do fine Their order is a little screwy...

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

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

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

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

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

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee C Language Part 2 (Minor modifications by the instructor) 1 Scope Rules A variable declared inside a function is a local variable Each local variable in a function comes into existence when the function

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

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

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam Multimedia Programming 2004 Lecture 2 Erwin M. Bakker Joachim Rijsdam Recap Learning C++ by example No groups: everybody should experience developing and programming in C++! Assignments will determine

More information

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

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++ CHAPTER 9 C++ 1. WRITE ABOUT THE BINARY OPERATORS USED IN C++? ARITHMETIC OPERATORS: Arithmetic operators perform simple arithmetic operations like addition, subtraction, multiplication, division etc.,

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

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

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

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

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

Chapter 3: Arrays and More C Functionality

Chapter 3: Arrays and More C Functionality Chapter 3: Arrays and More C Functionality Objectives: (a) Describe how an array is stored in memory. (b) Define a string, and describe how strings are stored. (c) Describe the implications of reading

More information

C Programming for Engineers Structured Program

C Programming for Engineers Structured Program C Programming for Engineers Structured Program ICEN 360 Spring 2017 Prof. Dola Saha 1 Switch Statement Ø Used to select one of several alternatives Ø useful when the selection is based on the value of

More information

Chapter 4: Expressions. Chapter 4. Expressions. Copyright 2008 W. W. Norton & Company. All rights reserved.

Chapter 4: Expressions. Chapter 4. Expressions. Copyright 2008 W. W. Norton & Company. All rights reserved. Chapter 4: Expressions Chapter 4 Expressions 1 Chapter 4: Expressions Operators Expressions are built from variables, constants, and operators. C has a rich collection of operators, including arithmetic

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

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

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

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

A Look Back at Arithmetic Operators: the Increment and Decrement

A Look Back at Arithmetic Operators: the Increment and Decrement A Look Back at Arithmetic Operators: the Increment and Decrement Spring Semester 2016 Programming and Data Structure 27 Increment (++) and Decrement (--) Both of these are unary operators; they operate

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

LESSON 3 CONTROL STRUCTURES

LESSON 3 CONTROL STRUCTURES LESSON CONTROL STRUCTURES PROF. JOHN P. BAUGH PROFJPBAUGH@GMAIL.COM PROFJPBAUGH.COM CONTENTS INTRODUCTION... Assumptions.... Logic, Logical Operators, AND Relational Operators..... - Logical AND (&&) Truth

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

Chapter 4: Control structures. Repetition

Chapter 4: Control structures. Repetition Chapter 4: Control structures Repetition Loop Statements After reading and studying this Section, student should be able to Implement repetition control in a program using while statements. Implement repetition

More information

Programming for Electrical and Computer Engineers. Loops

Programming for Electrical and Computer Engineers. Loops Programming for Electrical and Computer Engineers Loops Dr. D. J. Jackson Lecture 6-1 Iteration Statements C s iteration statements are used to set up loops. A loop is a statement whose job is to repeatedly

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

Pseudocode. ARITHMETIC OPERATORS: In pseudocode arithmetic operators are used to perform arithmetic operations. These operators are listed below:

Pseudocode. ARITHMETIC OPERATORS: In pseudocode arithmetic operators are used to perform arithmetic operations. These operators are listed below: Pseudocode There are 3 programming/pseudocode constructs: 1. Sequence: It refers that instructions should be executed one after another. 2. Selection: This construct is used to make a decision in choosing

More information

Dr. R. Z. Khan, Associate Professor, Department of Computer Science

Dr. R. Z. Khan, Associate Professor, Department of Computer Science ALIGARH MUSLIM UNIVERSITY Department of Computer Science Course: CSM-102: Programming & Problem Solving Using C Academic Session 2015-2016 UNIT-2: Handout-3 Topic: Control Structures (Selection & Repetition)

More information

CS110D: PROGRAMMING LANGUAGE I

CS110D: PROGRAMMING LANGUAGE I CS110D: PROGRAMMING LANGUAGE I Computer Science department Lecture 5&6: Loops Lecture Contents Why loops?? While loops for loops do while loops Nested control structures Motivation Suppose that you need

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

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

UNIVERSITY OF WINDSOR Fall 2007 QUIZ # 2 Solution. Examiner : Ritu Chaturvedi Dated :November 27th, Student Name: Student Number:

UNIVERSITY OF WINDSOR Fall 2007 QUIZ # 2 Solution. Examiner : Ritu Chaturvedi Dated :November 27th, Student Name: Student Number: UNIVERSITY OF WINDSOR 60-106-01 Fall 2007 QUIZ # 2 Solution Examiner : Ritu Chaturvedi Dated :November 27th, 2007. Student Name: Student Number: INSTRUCTIONS (Please Read Carefully) No calculators allowed.

More information

UIC. C Programming Primer. Bharathidasan University

UIC. C Programming Primer. Bharathidasan University C Programming Primer UIC C Programming Primer Bharathidasan University Contents Getting Started 02 Basic Concepts. 02 Variables, Data types and Constants...03 Control Statements and Loops 05 Expressions

More information

Programming for Engineers Iteration

Programming for Engineers Iteration Programming for Engineers Iteration ICEN 200 Spring 2018 Prof. Dola Saha 1 Data type conversions Grade average example,-./0 class average = 23450-67 893/0298 Grade and number of students can be integers

More information

Looping Subtasks. We will examine some basic algorithms that use the while and if constructs. These subtasks include

Looping Subtasks. We will examine some basic algorithms that use the while and if constructs. These subtasks include 1 Programming in C Looping Subtasks We will examine some basic algorithms that use the while and if constructs. These subtasks include Reading unknown quantity of data Counting things Accumulating (summing)

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

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

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

Laboratory 4. INSTRUCTIONS (part II) I. THEORETICAL BACKGROUND PROGRAMMING LANGUAGES Laboratory 4 INSTRUCTIONS (part II) I. THEORETICAL BACKGROUND 1. Instructions (overview) 1.1. The conditional instruction (if..else) if(expresie) instruction1; else instruction2;

More information

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible) Name Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code necessary C++ statements to perform the action described not a complete program Program a complete C++ program

More information

Prepared by: Shraddha Modi

Prepared by: Shraddha Modi Prepared by: Shraddha Modi Introduction In looping, a sequence of statements are executed until some conditions for termination of the loop are satisfied. A program loop consist of two segments Body of

More information

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3 More Flow of Control Overview 3.1 Using Boolean Expressions 3.2 Multiway Branches 3.3 More about C++ Loop Statements 3.4 Designing Loops Slide 3-3 Flow Of Control Flow of control refers to the

More information

Chapter 4: Control structures

Chapter 4: Control structures Chapter 4: Control structures Repetition Loop Statements After reading and studying this Section, student should be able to Implement repetition control in a program using while statements. Implement repetition

More information

Chapter 6. Loops. Iteration Statements. C s iteration statements are used to set up loops.

Chapter 6. Loops. Iteration Statements. C s iteration statements are used to set up loops. Chapter 6 Loops 1 Iteration Statements C s iteration statements are used to set up loops. A loop is a statement whose job is to repeatedly execute some other statement (the loop body). In C, every loop

More information

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop Announcements Lab Friday, 1-2:30 and 3-4:30 in 26-152 Boot your laptop and start Forte, if you brought your laptop Create an empty file called Lecture4 and create an empty main() method in a class: 1.00

More information

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

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators Expressions 1 Expressions n Variables and constants linked with operators Arithmetic expressions n Uses arithmetic operators n Can evaluate to any value Logical expressions n Uses relational and logical

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

Structured Program Development in C

Structured Program Development in C 1 3 Structured Program Development in C 3.2 Algorithms 2 Computing problems All can be solved by executing a series of actions in a specific order Algorithm: procedure in terms of Actions to be executed

More information

Comments. Comments: /* This is a comment */

Comments. Comments: /* This is a comment */ Flow Control Comments Comments: /* This is a comment */ Use them! Comments should explain: special cases the use of functions (parameters, return values, purpose) special tricks or things that are not

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

Loops (while and for)

Loops (while and for) Loops (while and for) CSE 1310 Introduction to Computers and Programming Alexandra Stefan 1 Motivation Was there any program we did (class or hw) where you wanted to repeat an action? 2 Motivation Name

More information

Fundamentals of Computer Science. Sentinel Based Repetition

Fundamentals of Computer Science. Sentinel Based Repetition Sentinel Based Repetition 59 syntax: The while Loop Statement while (expression) statement a while loop statement is (almost) identical to a for loop statement that has no initialization or increment expressions

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

Lecture 10. Daily Puzzle

Lecture 10. Daily Puzzle Lecture 10 Daily Puzzle Imagine there is a ditch, 10 feet wide, which is far too wide to jump. Using only eight narrow planks, each no more than 9 feet long, construct a bridge across the ditch. Daily

More information

CS111: PROGRAMMING LANGUAGE II

CS111: PROGRAMMING LANGUAGE II CS111: PROGRAMMING LANGUAGE II Computer Science Department Lecture 1(c): Java Basics (II) Lecture Contents Java basics (part II) Conditions Loops Methods Conditions & Branching Conditional Statements A

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

ECET 264 C Programming Language with Applications. C Program Control

ECET 264 C Programming Language with Applications. C Program Control ECET 264 C Programming Language with Applications Lecture 7 C Program Control Paul I. Lin Professor of Electrical & Computer Engineering Technology http://www.etcs.ipfw.edu/~lin Lecture 7 - Paul I. Lin

More information

Slides adopted from T. Ferguson Spring 2016

Slides adopted from T. Ferguson Spring 2016 CSE1311 Introduction to Programming for Science & Engineering Students Mostafa Parchami, Ph.D. Dept. of Comp. Science and Eng., Univ. of Texas at Arlington, USA Slides adopted from T. Ferguson Spring 2016

More information

Conditionals. CSE / ENGR 142 Programming I. Chapter 4. Conditional Execution. Conditional ("if ") Statement. Conditional Expressions

Conditionals. CSE / ENGR 142 Programming I. Chapter 4. Conditional Execution. Conditional (if ) Statement. Conditional Expressions 1999 UW CSE CSE / ENGR 142 Programming I Conditionals Chapter 4 Read Sections 4.1-4.5, 4.7-4.9 4.1: Control structure preview 4.2: Relational and logical operators 4.3: if statements 4.4: Compound statements

More information

More examples for Control statements

More examples for Control statements More examples for Control statements C language possesses such decision making capabilities and supports the following statements known as control or decision-making statements. 1. if statement 2. switch

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

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

A control structure refers to the way in which the Programmer specifies the order of executing the statements

A control structure refers to the way in which the Programmer specifies the order of executing the statements Control Structures A control structure refers to the way in which the Programmer specifies the order of executing the statements The following approaches can be chosen depending on the problem statement:

More information