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

Size: px
Start display at page:

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

Transcription

1 Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II) Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics Engineering

2 Topics break statement continue statement Infinite loops Nested loops while and for Macros Example Factorization

3 The break statement Implements the "exit loop" primitive Causes flow of control to leave a loop block (while or for) immediately

4 The continue statement Causes flow of control to start immediately the next iteration of a loop block (while or for)

5 Infinite Loops while ( 1 )...etc...etc...etc... for ( ; 1 ; )...etc...etc...etc... for ( ; ; )...etc...etc...etc... Use an: if ( condition ) break; statement to break the loop

6 Read in numbers, and add only the positive ones. Quit when input is 0 set sum to 0 loop input number if (number is negative) begin next iteration else if ( number is zero) exit loop add number to sum output sum Example: addpos.c

7 Example: addpos.c (cont) Read in numbers, and add only the positive ones. Quit when input is 0 set sum to 0 loop input number if (number is negative) begin next iteration else if ( number is zero) exit loop add number to sum include <stdio.h> /**************************** ** Read in numbers, and add ** only the positive ones. ** Quit when input is 0 *****************************/ int main() float num, sum = 0.0; output sum printf("sum = %f\n", sum); return 0;

8 Example: addpos.c (cont) Read in numbers, and add only the positive ones. Quit when input is 0 set sum to 0 loop input number if (number is negative) begin next iteration else if ( number is zero) exit loop add number to sum include <stdio.h> /**************************** ** Read in numbers, and add ** only the positive ones. ** Quit when input is 0 *****************************/ int main() float num, sum = 0.0; while (1) scanf("%f", &num); sum += num; output sum printf("sum = %f\n", sum); return 0;

9 Example: addpos.c (cont) Read in numbers, and add only the positive ones. Quit when input is 0 set sum to 0 loop input number if (number is negative) begin next iteration else if ( number is zero) exit loop include <stdio.h> /**************************** ** Read in numbers, and add ** only the positive ones. ** Quit when input is 0 *****************************/ int main() float num, sum = 0.0; while (1) scanf("%f", &num); if (num < 0) continue; else if (num == 0) break; add number to sum sum += num; output sum printf("sum = %f\n", sum); return 0;

10 Example: addpos.c (cont) Read in numbers, and add only the positive ones. Quit when input is 0 set sum to 0 loop input number if (number is negative) begin next iteration else if ( number is zero) exit loop add number to sum output sum include <stdio.h> /**************************** ** Read in numbers, and add ** only the positive ones. ** Quit when input is 0 *****************************/ int main() float num, sum = 0.0; while (1)) scanf("%f", &num); if (num < 0) continue; else if (num == 0) break; sum += num; printf("sum = %f\n", sum); return 0;

11 Example: addpos.c (cont) Read in numbers, and add only the positive ones. Quit when input is 0 set sum to 0 loop input number if (number is negative) begin next iteration else if ( number is zero) exit loop include <stdio.h> /**************************** ** Read in numbers, and add ** only the positive ones. ** Quit when input is 0 *****************************/ int main() float num, sum = 0.0; while (1) scanf("%f", &num); if (num < 0) continue; else if (num == 0) break; add number to sum sum += num; output sum printf("sum = %f\n", sum); return 0;

12 for and continue In the case of a for statement, control passes to the update expression. Example: for (a = 0; a < 100; a++) if (a%4) continue; printf( %d\n,a);

13 Nested Loops - Example: rect.c Print an m by n rectangle of asterisks input width and height for each row for each column in the current row print an asterisk start next row

14 Example: rect.c (cont) Print an m by n rectangle of asterisks input width and height #include <stdio.h> /* Print an m-by-n rectangle of asterisks */ int main() int i, j, m, n; printf("\nenter width: "); scanf("%d", &m); printf("\nenter height: "); scanf("%d", &n); for each row for each column in the current row print an asterisk start next row return 0;

15 Example: rect.c (cont) Print an m by n rectangle of asterisks input width and height for each row for each column in the current row print an asterisk start next row #include <stdio.h> /* Print an m-by-n rectangle of asterisks */ int main() int i, j, m, n; printf("\nenter width: "); scanf("%d", &m); printf("\nenter height: "); scanf("%d", &n); for (i=0; i < n; i++) return 0;

16 Example: rect.c (cont) Print an m by n rectangle of asterisks input width and height for each row for each column in the current row print an asterisk start next row #include <stdio.h> /* Print an m-by-n rectangle of asterisks */ int main() int i, j, m, n; printf("\nenter width: "); scanf("%d", &m); printf("\nenter height: "); scanf("%d", &n); for (i=0; i < n; i++) for (j=0; j < m; j++) return 0;

17 Example: rect.c (cont) Print an m by n rectangle of asterisks input width and height for each row for each column in the current row print an asterisk start next row #include <stdio.h> /* Print an m-by-n rectangle of asterisks */ int main() int i, j, m, n; printf("\nenter width: "); scanf("%d", &m); printf("\nenter height: "); scanf("%d", &n); for (i=0; i < n; i++) for (j=0; j < m; j++) printf("*"); return 0;

18 Example: rect.c (cont) Print an m by n rectangle of asterisks input width and height for each row for each column in the current row print an asterisk start next row #include <stdio.h> /* Print an m-by-n rectangle of asterisks */ int main() int i, j, m, n; printf("\nenter width: "); scanf("%d", &m); printf("\nenter height: "); scanf("%d", &n); for (i=0; i < n; i++) for (j=0; j < m; j++) printf("*"); printf("\n"); return 0;

19 Example: rect.c (cont) Print an m by n rectangle of asterisks input width and height for each row for each column in the current row print an asterisk start next row #include <stdio.h> /* Print an m-by-n rectangle of asterisks */ int main() int i, j, m, n; printf("\nenter width: "); scanf("%d", &m); printf("\nenter height: "); scanf("%d", &n); for (i=0; i < n; i++) for (j=0; j < m; j++) printf("*"); printf("\n"); return 0;

20 Example: rect.c (cont) Print an m by n rectangle of asterisks algorithm input width and height for each row for each column in the current row print an asterisk start next row #include <stdio.h> /* Print an m-by-n rectangle of asterisks */ int main() int i, j, m, n; program printf("\nenter width: "); scanf("%d", &m); printf("\nenter height: "); scanf("%d", &n); for (i=0; i < n; i++) for (j=0; j < m; j++) printf("*"); printf("\n"); return 0;

21 Variation: rect2.c Print an m by n rectangle of asterisks input width and height for each row for each column in the current row print an asterisk start next row #include <stdio.h> /* Print an m-by-n rectangle of asterisks */ int main() int i, j, m, n; printf("\nenter width: "); scanf("%d", &m); printf("\nenter height: "); scanf("%d", &n); i = 0; while (i < n) for (j=0; j < m; j++) printf("*"); printf("\n"); i++; return 0;

22 Variation: rect3.c Print an m by n rectangle of asterisks input width and height #include <stdio.h> /* Print an m-by-n rectangle of asterisks */ int main() int i, j, m, n; printf("\nenter width: "); scanf("%d", &m); printf("\nenter height: "); scanf("%d", &n); for each row for each column in the current row print an asterisk start next row for (i=0; i < n; i++) j = 0; while (1) printf("*"); j++; if (j == m) break; printf("\n"); return 0;

23 Variation: rect3.c (cont) #include <stdio.h> /* Print an m-by-n rectangle of asterisks */ int main() int i, j, m, n; The innermost enclosing loop for this break is the while-loop printf("\nenter width: "); scanf("%d", &m); printf("\nenter height: "); scanf("%d", &n); for (i=0; i < n; i++) j = 0; while (1) printf("*"); j++; if (j == m) break; printf("\n"); return 0;

24 while and for A for loop can always be rewritten as an equivalent while loop, and vice-versa The continue statement in a for loop passes control to the update expression

25 ASCII (American Standard Code for Information Interchange) Is a character encoding based on the English alphabet. ASCII codes represent text in computers, communications equipment, and other devices that work with text There are 128 characters (0-127) First 32 (0-31) are control characters and are not printable

26 Example: asciicheck.c while (1) printf("enter bounds (low high): "); scanf("%d %d", &low, &high); if ((low >= 0) && (high <= 127) && (low < high)) break; else printf("bad bounds. Try again.\n");

27 Example: asciicheck.c while (1) printf("enter bounds (low high): "); scanf("%d %d", &low, &high); if ((low >= 0) && (high <= 127) && (low < high)) break; else printf("bad bounds. Try again.\n");

28 Example: asciiprint Print out a section of the ASCII table for each character from the lower bound to higher bound print its ascii value and ascii character for ( ch = low; ch <= high; ch++ ) printf("%d: %c\n", ch, ch); asciiprint1.c ch = low; while ( ch <= high ) printf("%d: %c\n", ch, ch); ch++; asciiprint2.c

29 Example: asciiprint (cont) for ( ch = low; ch <= high; ch++ ) printf("%d: %c\n", ch, ch); asciiprint1.c ch = low; while (1) printf("%d: %c\n", ch, ch); if (ch < high) ch++; continue; else break; asciiprint3.c

30 Example: asciiprint (cont) for ( ch = low; ch <= high; ch++ ) printf("%d: %c\n", ch, ch); asciiprint1.c ch = low; for (;;) printf("%d: %c\n", ch, ch); ch++; if (ch > high) break; asciiprint4.c

31 Example: ascii1.c #include <stdio.h> /* Print a section of the ASCII table */ #define MIN 0 #define MAX 127 int main() int low, high; char ch; while (1) printf("enter bounds (low high): "); scanf("%d %d", &low, &high); if ((low >= MIN) && (high <= MAX) && (low < high)) break; else printf("bad bounds. Try again.\n"); for (ch=low; ch <= high; ch++) printf("%d: %c\n", ch, ch); return 0;

32 Example: ascii1.c (cont) #include <stdio.h> /* Print a section of the ASCII table */ #define MIN 0 #define MAX 127 int main() int low, high; char ch; while (1) printf("enter bounds (low high): "); scanf("%d %d", &low, &high); if ((low >= MIN) && (high <= MAX) && (low < high)) break; else printf("bad bounds. Try again.\n"); for (ch=low; ch <= high; ch++) printf("%d: %c\n", ch, ch); return 0;

33 Example: ascii1.c (cont) #include <stdio.h> /* Print a section of the ASCII table */ #define MIN 0 #define MAX 127 int main() int low, high; char ch; while (1) printf("enter bounds (low high): "); scanf("%d %d", &low, &high); if ((low >= MIN) && (high <= MAX) && (low < high)) break; else printf("bad bounds. Try again.\n"); Macro definition: for (ch=low; ch <= high; ch++) printf("%d: %c\n", ch, ch); #define identifier tokens return 0; All subsequent instances of identifier are replaced with its token

34 Output of Ascii1.c Enter bounds (low high): : 33:! 34:" 35:# 36:$ 37:% 38:& 39:' 40:( 41:) 42:* 43:+ 44:, 45:- 46:. 47:/ 48:0 49:1 50:2 51:3 52:4 53:5 54:6 55:7 56:8 57:9 58:: 59:; 60:< 61:= 62:> 63:? 65:A 66:B 67:C 68:D 69:E 70:F 71:G 72:H 73:I 74:J 75:K 76:L 77:M 78:N 79:O 80:P 81:Q 82:R 83:S 84:T 85:U 86:V 87:W 88:X 89:Y 90:Z 91:[ 92:\ 93:] 94:^ 95:_ 96:` 97:a 98:b 99:c 100:d 101:e 102:f 103:g 104:h 105:i 106:j 107:k 108:l 109:m 110:n 111:o 112:p 113:q 114:r 115:s 116:t 117:u 118:v 119:w 120:x 121:y 122:z 123: 124: 125: 126:~

35 Example : Factorization Write a program which prints out the prime factorization of a number (treat 2 as the first prime) For example, on input 6, desired output is: 2 3 " " 24, " " : " " 14, " " : 2 7 " " 23, " " : 23 (23 is prime)

36 Algorithm input n set factor to 2

37 Algorithm (cont) input n set factor to 2 while(some factor yet to try)

38 Algorithm (cont) input n set factor to 2 while(some factor yet to try) if (n is divisible by factor) output factor set n to n / factor

39 Algorithm (cont) input n set factor to 2 while(some factor yet to try) if (n is divisible by factor) output factor set n to n / factor else increment factor

40 Algorithm (cont) input n Why not? set factor to 2 while(some factor yet to try) if (n is divisible by factor) output factor set n to n / factor else increment factor while(some factor yet to try) if (n is divisible by factor) output factor set n to n / factor increment factor

41 #include <stdio.h> Program /* Print out the prime factors of a number */ int main() int n, factor ; return 0;

42 #include <stdio.h> Program (cont) /* Print out the prime factors of a number */ int main() int n, factor ; printf("\nenter integer: ") ; scanf("%d", &n) ; return 0;

43 #include <stdio.h> Program (cont) /* Print out the prime factors of a number */ int main() int n, factor ; printf("\nenter integer: ") ; scanf("%d", &n) ; printf("\nthe prime factors of %d are: ", n) ; /* Try each possible factor in turn */ printf("\n\n"); return 0;

44 /* Try each possible factor in turn */ factor = 2; while ( factor <= n && n > 1 )

45 /* Try each possible factor in turn */ factor = 2; while ( factor <= n && n > 1 ) if (n % factor == 0) /* n is a multiple of factor, ** so print factor and divide n by factor */ printf(" %d", factor) ; n = n / factor ;

46 /* Try each possible factor in turn */ factor = 2; while ( factor <= n && n > 1 ) if (n % factor == 0) /* n is a multiple of factor, ** so print factor and divide n by factor */ printf(" %d", factor) ; n = n / factor ; else /* n is not a multiple of factor; ** try next possible factor */ factor++ ;

47 /* Try each possible factor in turn */ factor = 2; while ( factor <= n && n > 1 ) if (n % factor == 0) /* n is a multiple of factor, ** so print factor and divide n by factor */ printf(" %d", factor) ; n = n / factor ; else /* n is not a multiple of factor; ** try next possible factor */ factor++ ;

48 #include <stdio.h> /* Print out the prime factors of a number */ int main() int n, factor ; printf("\nenter integer: ") ; scanf("%d", &n) ; printf("\nthe prime factors of %d are: ", n) ; /* Try each possible factor in turn */ factor = 2; while ( factor <= n && n > 1 ) if (n % factor == 0) /* n is a multiple of factor, ** so print factor and divide n by factor. */ printf(" %d", factor) ; n = n / factor ; else /* n is not a multiple of factor; ** try next possible factor */ factor++ ; printf("\n\n"); return 0; factor1.c

49 /* Try each possible factor in turn */ factor = 2; while ( factor <= n && n > 1 ) if (n % factor == 0) Change from /* n is a multiple while-loop of factor, to for- ** so print factor and divide n by factor */ loop? printf(" %d", factor) ; n = n / factor ; else /* n is not a multiple of factor; ** try next possible factor */ factor++ ;

50 /* Try each possible factor in turn */ for ( factor = 2; factor <= n && n > 1 ; ) if (n % factor == 0) /* n is a multiple of factor, ** so print factor and divide n by factor */ printf(" %d", factor) ; n = n / factor ; else /* n is not a multiple of factor; ** try next possible factor */ factor++ ;

51 #include <stdio.h> /* Print out the prime factors of a number. */ int main() int n, factor ; printf("\nenter integer: ") ; scanf("%d", &n) ; printf("\nthe prime factors of %d are: ", n) ; /* Try each possible factor in turn. */ for ( factor = 2; factor <= n && n > 1 ; ) if (n % factor == 0) /* n is a multiple of factor, ** so print factor and divide n by factor. */ printf(" %d", factor) ; n = n / factor ; else /* n is not a multiple of factor; ** try next possible factor. */ factor++ ; printf("\n\n"); return 0; factor2.c

공학프로그래밍언어 (PROGRAMMING LANGUAGE FOR ENGINEERS) -CONTROL FLOW : LOOP- SPRING 2015, SEON-JU AHN, CNU EE

공학프로그래밍언어 (PROGRAMMING LANGUAGE FOR ENGINEERS) -CONTROL FLOW : LOOP- SPRING 2015, SEON-JU AHN, CNU EE 공학프로그래밍언어 (PROGRAMMING LANGUAGE FOR ENGINEERS) -CONTROL FLOW : LOOP- SPRING 2015, SEON-JU AHN, CNU EE LOOPS WHILE AND FOR while syntax while (expression) statement The expression is evaluated. If it is

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

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

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

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

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

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

Introduction. Pretest and Posttest Loops. Basic Loop Structures ว ทยาการคอมพ วเตอร เบ องต น Fundamentals of Computer Science

Introduction. Pretest and Posttest Loops. Basic Loop Structures ว ทยาการคอมพ วเตอร เบ องต น Fundamentals of Computer Science 204111 ว ทยาการคอมพ วเตอร เบ องต น Fundamentals of Computer Science ภาคการศ กษาท ภาคการศกษาท 1 ป ปการศกษา การศ กษา 2556 4. การเข ยนโปรแกรมพ นฐาน 4.5 โครงสร างควบค มแบบท าซ า รวบรวมโดย อ. ดร. อาร ร ตน ตรงร

More information

Structured Programming. Dr. Mohamed Khedr Lecture 9

Structured Programming. Dr. Mohamed Khedr Lecture 9 Structured Programming Dr. Mohamed Khedr http://webmail.aast.edu/~khedr 1 Two Types of Loops count controlled loops repeat a specified number of times event-controlled loops some condition within the loop

More information

LAB 6: While and do-while Loops

LAB 6: While and do-while Loops Name: Joe Bloggs LAB 6: While and do-while Loops Exercises 1 while loop /* Program name: lab6ex1.c * Purpose : This program demonstrates use of while loop int num = 0; while( num!= -1 ) printf("please

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

MA 511: Computer Programming Lecture 2: Partha Sarathi Mandal

MA 511: Computer Programming Lecture 2: Partha Sarathi Mandal MA 511: Computer Programming Lecture 2: http://www.iitg.ernet.in/psm/indexing_ma511/y10/index.html Partha Sarathi Mandal psm@iitg.ernet.ac.in Dept. of Mathematics, IIT Guwahati Semester 1, 2010-11 Largest

More information

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

ECE15: Introduction to Computer Programming Using the C Language. Lecture Unit 4: Flow of Control ECE15: Introduction to Computer Programming Using the C Language Lecture Unit 4: Flow of Control Outline of this Lecture Examples of Statements in C Conditional Statements The if-else Conditional Statement

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

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

Fundamental of Programming (C)

Fundamental of Programming (C) Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 5 Structured Program Development Department of Computer Engineering How to develop

More information

Fundamentals of Programming

Fundamentals of Programming Fundamentals of Programming Lecture 5 - Structured Program Development Lecturer : Ebrahim Jahandar Borrowed from lecturer notes by Omid Jafarinezhad How to develop a program? Requirements Problem Analysis

More information

Introduction to Computing Lecture 10 Arrays (Part 1)

Introduction to Computing Lecture 10 Arrays (Part 1) Introduction to Computing Lecture 10 Arrays (Part 1) Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr 1 Topics Arrays Declaration Initialization

More information

PROGRAMMING IN C LAB MANUAL FOR DIPLOMA IN ECE/EEE

PROGRAMMING IN C LAB MANUAL FOR DIPLOMA IN ECE/EEE PROGRAMMING IN C LAB MANUAL FOR DIPLOMA IN ECE/EEE 1. Write a C program to perform addition, subtraction, multiplication and division of two numbers. # include # include int a, b,sum,

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

Computer Programming Lecture 14 Arrays (Part 2)

Computer Programming Lecture 14 Arrays (Part 2) Computer Programming Lecture 14 Arrays (Part 2) Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics Engineering nukhet.ozbek@ege.edu.tr 1 Topics The relationship between

More information

Fundamental of Programming (C)

Fundamental of Programming (C) Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 3 Constants, Variables, Data Types, And Operations Department of Computer Engineering

More information

Basic Assignment and Arithmetic Operators

Basic Assignment and Arithmetic Operators C Programming 1 Basic Assignment and Arithmetic Operators C Programming 2 Assignment Operator = int count ; count = 10 ; The first line declares the variable count. In the second line an assignment operator

More information

Lecture 3. More About C

Lecture 3. More About C Copyright 1996 David R. Hanson Computer Science 126, Fall 1996 3-1 Lecture 3. More About C Programming languages have their lingo Programming language Types are categories of values int, float, char Constants

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

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

Case Control Structure. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

Case Control Structure. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan Case Control Structure DCS COMSATS Institute of Information Technology Rab Nawaz Jadoon Assistant Professor COMSATS IIT, Abbottabad Pakistan Introduction to Computer Programming (ICP) Decision control

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

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

16.216: ECE Application Programming Fall 2011

16.216: ECE Application Programming Fall 2011 16.216: ECE Application Programming Fall 2011 Exam 2 Solution 1. (24 points, 6 points per part) Multiple choice For each of the multiple choice questions below, clearly indicate your response by circling

More information

Computer Programming Lecture 12 Pointers

Computer Programming Lecture 12 Pointers Computer Programming Lecture 2 Pointers Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics Engineering nukhet.ozbek@ege.edu.tr Topics Introduction to Pointers Pointers and

More information

Lecture 5: C programming

Lecture 5: C programming CSCI-GA.1144-001 PAC II Lecture 5: C programming Mohamed Zahran (aka Z) mzahran@cs.nyu.edu http://www.mzahran.com Brian Kernighan Dennis Ritchie In 1972 Dennis Ritchie at Bell Labs writes C and in 1978

More information

Programming & Data Structure: CS Section - 1/A DO NOT POWER ON THE MACHINE

Programming & Data Structure: CS Section - 1/A DO NOT POWER ON THE MACHINE DS Tutorial: III (CS 11001): Section 1 Dept. of CS&Engg., IIT Kharagpur 1 Tutorial Programming & Data Structure: CS 11001 Section - 1/A DO NOT POWER ON THE MACHINE Department of Computer Science and Engineering

More information

Data types, variables, constants

Data types, variables, constants Data types, variables, constants 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 in C 2.6 Decision

More information

C Programming Lecture V

C Programming Lecture V C Programming Lecture V Instructor Özgür ZEYDAN http://cevre.beun.edu.tr/ Modular Programming A function in C is a small sub-program that performs a particular task, and supports the concept of modular

More information

Structured programming. Exercises 3

Structured programming. Exercises 3 Exercises 3 Table of Contents 1. Reminder from lectures...................................................... 1 1.1. Relational operators..................................................... 1 1.2. Logical

More information

LAB 5: REPETITION STRUCTURE(LOOP)

LAB 5: REPETITION STRUCTURE(LOOP) LAB 5: REPETITION STRUCTURE(LOOP) OBJECTIVES 1. To introduce two means of repetition/loop structures; counter-controlled and sentinelcontrolled. 2. To introduce the repetition structures; for, while, do-while

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

LAB 5: REPETITION STRUCTURE(LOOP)

LAB 5: REPETITION STRUCTURE(LOOP) LAB 5: REPETITION STRUCTURE(LOOP) OBJECTIVES 1. To introduce two means of repetition/loop structures; counter-controlled and sentinelcontrolled. 2. To introduce the repetition structures; for, while, do-while

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

Statements. Control Flow Statements. Relational Operators. Logical Expressions. Relational Operators. Relational Operators 1/30/14

Statements. Control Flow Statements. Relational Operators. Logical Expressions. Relational Operators. Relational Operators 1/30/14 Statements Control Flow Statements Based on slides from K. N. King Bryn Mawr College CS246 Programming Paradigm So far, we ve used return statements and expression statements. Most of C s remaining statements

More information

C Program. Output. Hi everyone. #include <stdio.h> main () { printf ( Hi everyone\n ); }

C Program. Output. Hi everyone. #include <stdio.h> main () { printf ( Hi everyone\n ); } C Program Output #include main () { printf ( Hi everyone\n ); Hi everyone #include main () { printf ( Hi everyone\n ); #include and main are Keywords (or Reserved Words) Reserved Words

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

'C' Programming Language

'C' Programming Language F.Y. Diploma : Sem. II [DE/EJ/ET/EN/EX] 'C' Programming Language Time: 3 Hrs.] Prelim Question Paper Solution [Marks : 70 Q.1 Attempt any FIVE of the following : [10] Q.1(a) Define pointer. Write syntax

More information

Computer Programming Unit 3

Computer Programming Unit 3 POINTERS INTRODUCTION Pointers are important in c-language. Some tasks are performed more easily with pointers such as dynamic memory allocation, cannot be performed without using pointers. So it s very

More information

Engineering 12 - Spring, 1998

Engineering 12 - Spring, 1998 Engineering 12 - Spring, 1998 1. (15 points) Rewrite the following program so that it uses a while loop in place of the for loop. (Note that a part of the new program is already shown at the bottom of

More information

Variables in C. CMSC 104, Spring 2014 Christopher S. Marron. (thanks to John Park for slides) Tuesday, February 18, 14

Variables in C. CMSC 104, Spring 2014 Christopher S. Marron. (thanks to John Park for slides) Tuesday, February 18, 14 Variables in C CMSC 104, Spring 2014 Christopher S. Marron (thanks to John Park for slides) 1 Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement 2 What

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

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

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

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants Data types, variables, constants Outline.1 Introduction. Text.3 Memory Concepts.4 Naming Convention of Variables.5 Arithmetic in C.6 Type Conversion Definition: Computer Program A Computer program is a

More information

C Program Reviews 2-12

C Program Reviews 2-12 Program 2: From Pseudo Code to C ICT106 Fundamentals of Computer Systems Topic 12 C Program Reviews 2-12 Initialise linecount and alphacount to zero read a character while (character just read is not end

More information

Chapter 13 Control Structures

Chapter 13 Control Structures Chapter 13 Control Structures Control Structures Conditional making a decision about which code to execute, based on evaluated expression if if-else switch Iteration executing code multiple times, ending

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

APS105. Collecting Elements 10/20/2013. Declaring an Array in C. How to collect elements of the same type? Arrays. General form: Example:

APS105. Collecting Elements 10/20/2013. Declaring an Array in C. How to collect elements of the same type? Arrays. General form: Example: Collecting Elements How to collect elements of the same type? Eg:., marks on assignments: APS105 Arrays Textbook Chapters 6.1-6.3 Assn# 1 2 3 4 5 6 Mark 87 89 77 96 87 79 Eg: a solution in math: x 1, x

More information

Chapter 2. Section 2.5 while Loop. CS 50 Hathairat Rattanasook

Chapter 2. Section 2.5 while Loop. CS 50 Hathairat Rattanasook Chapter 2 Section 2.5 while Loop CS 50 Hathairat Rattanasook Loop Iteration means executing a code segment more than once. A loop is an iterative construct. It executes a statement 0..n times while a condition

More information

Introduction to Computing Lecture 03: Basic input / output operations

Introduction to Computing Lecture 03: Basic input / output operations Introduction to Computing Lecture 03: Basic input / output operations Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics Engineering nukhet.ozbek@ege.edu.tr Topics Streams

More information

Introduction to C. Systems Programming Concepts

Introduction to C. Systems Programming Concepts Introduction to C Systems Programming Concepts Introduction to C A simple C Program Variable Declarations printf ( ) Compiling and Running a C Program Sizeof Program #include What is True in C? if example

More information

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C C: A High-Level Language Chapter 11 Introduction to Programming in C Original slides from Gregory Byrd, North Carolina State University Modified slides by Chris Wilcox, Colorado State University! Gives

More information

Unit 5. Decision Making and Looping. School of Science and Technology INTRODUCTION

Unit 5. Decision Making and Looping. School of Science and Technology INTRODUCTION INTRODUCTION Decision Making and Looping Unit 5 In the previous lessons we have learned about the programming structure, decision making procedure, how to write statements, as well as different types of

More information

Chapter 13. Control Structures

Chapter 13. Control Structures Chapter 13 Control Structures Control Structures Conditional making a decision about which code to execute, based on evaluated expression if if-else switch Iteration executing code multiple times, ending

More information

Syntax of for loop is as follows: for (inite; terme; updatee) { block of statements executed if terme is true;

Syntax of for loop is as follows: for (inite; terme; updatee) { block of statements executed if terme is true; Birla Institute of Technology & Science, Pilani Computer Programming (CSF111) Lab-5 ---------------------------------------------------------------------------------------------------------------------------------------------

More information

CSCI 2132 Software Development. Lecture 8: Introduction to C

CSCI 2132 Software Development. Lecture 8: Introduction to C CSCI 2132 Software Development Lecture 8: Introduction to C Instructor: Vlado Keselj Faculty of Computer Science Dalhousie University 21-Sep-2018 (8) CSCI 2132 1 Previous Lecture Filename substitution

More information

Structured programming

Structured programming Exercises 2 Version 1.0, 22 September, 2016 Table of Contents 1. Simple C program structure................................................... 1 2. C Functions..................................................................

More information

Worksheet 4 Basic Input functions and Mathematical Operators

Worksheet 4 Basic Input functions and Mathematical Operators Name: Student ID: Date: Worksheet 4 Basic Input functions and Mathematical Operators Objectives After completing this worksheet, you should be able to Use an input function in C Declare variables with

More information

Concept of algorithms Understand and use three tools to represent algorithms: Flowchart Pseudocode Programs

Concept of algorithms Understand and use three tools to represent algorithms: Flowchart Pseudocode Programs Morteza Noferesti Concept of algorithms Understand and use three tools to represent algorithms: Flowchart Pseudocode Programs We want to solve a real problem by computers Take average, Sort, Painting,

More information

The C Programming Language Part 2. (with material from Dr. Bin Ren, William & Mary Computer Science)

The C Programming Language Part 2. (with material from Dr. Bin Ren, William & Mary Computer Science) The C Programming Language Part 2 (with material from Dr. Bin Ren, William & Mary Computer Science) 1 Overview Input/Output Structures and Arrays 2 Basic I/O character-based putchar (c) output getchar

More information

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C Chapter 11 Introduction to Programming in C Original slides from Gregory Byrd, North Carolina State University Modified slides by Chris Wilcox, Colorado State University C: A High-Level Language! Gives

More information

These are reserved words of the C language. For example int, float, if, else, for, while etc.

These are reserved words of the C language. For example int, float, if, else, for, while etc. Tokens in C Keywords These are reserved words of the C language. For example int, float, if, else, for, while etc. Identifiers An Identifier is a sequence of letters and digits, but must start with a letter.

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

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

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

upper and lower case English letters: A-Z and a-z digits: 0-9 common punctuation symbols special non-printing characters: e.g newline and space.

upper and lower case English letters: A-Z and a-z digits: 0-9 common punctuation symbols special non-printing characters: e.g newline and space. The char Type The C type char stores small integers. It is 8 bits (almost always). char guaranteed able to represent integers 0.. +127. char mostly used to store ASCII character codes. Don t use char for

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

16.216: ECE Application Programming Fall 2015 Exam 1 Solution

16.216: ECE Application Programming Fall 2015 Exam 1 Solution 16.216: ECE Application Programming Fall 2015 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

Lecture 10: Recursive Functions. Computer System and programming in C 1

Lecture 10: Recursive Functions. Computer System and programming in C 1 Lecture 10: Recursive Functions Computer System and programming in C 1 Outline Introducing Recursive Functions Format of recursive Functions Tracing Recursive Functions Examples Tracing using Recursive

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

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C Chapter 11 Introduction to Programming in C Original slides from Gregory Byrd, North Carolina State University Modified by Chris Wilcox, Yashwant Malaiya Colorado State University C: A High-Level Language

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

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

Computer Programing. for Physicists [SCPY204] Class 02: 25 Jan 2018

Computer Programing. for Physicists [SCPY204] Class 02: 25 Jan 2018 Computer Programing Class 02: 25 Jan 2018 [SCPY204] for Physicists Content: Data, Data type, program control, condition and loop, function and recursion, variable and scope Instructor: Puwis Amatyakul

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

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

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University Overview of C, Part 2 CSE 130: Introduction to Programming in C Stony Brook University Integer Arithmetic in C Addition, subtraction, and multiplication work as you would expect Division (/) returns the

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

& Technology. Expression? Statement-x. void main() int no; scanf("%d", &no); if(no%2==0) if(no%2!=0) Syllabus for 1. of execution of statements.

& Technology. Expression? Statement-x. void main() int no; scanf(%d, &no); if(no%2==0) if(no%2!=0) Syllabus for 1. of execution of statements. statement- Computer Programming and Utilization (CPU) 110003 D) Decision Control Structure (if, if, if e if, switch, ) 1 Explain if with example and draw flowchart. if is used to control the flow of execution

More information

Programming Language A

Programming Language A Programming Language A Takako Nemoto (JAIST) 30 October Takako Nemoto (JAIST) 30 October 1 / 29 From Homework 3 Homework 3 1 Write a program to convert the input Celsius degree temperature into Fahrenheight

More information

CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad

CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad Outline 1. C++ Iterative Constructs 2. The for Repetition Structure 3. Examples Using the for Structure 4. The while Repetition Structure

More information

16.216: ECE Application Programming Fall 2013

16.216: ECE Application Programming Fall 2013 16.216: ECE Application Programming Fall 2013 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

Midterm Exam. CSCI 2132: Software Development. March 4, Marks. Question 1 (10) Question 2 (10) Question 3 (10) Question 4 (10) Question 5 (5)

Midterm Exam. CSCI 2132: Software Development. March 4, Marks. Question 1 (10) Question 2 (10) Question 3 (10) Question 4 (10) Question 5 (5) Banner number: Name: Midterm Exam CSCI 2132: Software Development March 4, 2019 Marks Question 1 (10) Question 2 (10) Question 3 (10) Question 4 (10) Question 5 (5) Question 6 (5) Total (50) Instructions:

More information

While Loops CHAPTER 5: LOOP STRUCTURES. While Loops. While Loops 2/7/2013

While Loops CHAPTER 5: LOOP STRUCTURES. While Loops. While Loops 2/7/2013 While Loops A loop performs an iteration or repetition A while loop is the simplest form of a loop Occurs when a condition is true CHAPTER 5: LOOP STRUCTURES Introduction to Computer Science Using Ruby

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

More Programming Constructs -- Introduction

More Programming Constructs -- Introduction More Programming Constructs -- Introduction We can now examine some additional programming concepts and constructs Chapter 5 focuses on: internal data representation conversions between one data type and

More information

Chapter 13 Control Structures

Chapter 13 Control Structures Chapter 13 Control Structures Highlights Sequence action_1 action_2 Conditional Iteration 13-2 Control Structures Conditional making a decision about which code to execute, based on evaluated expression

More information

Name :. Roll No. :... Invigilator s Signature : INTRODUCTION TO PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

Name :. Roll No. :... Invigilator s Signature : INTRODUCTION TO PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 Name :. Roll No. :..... Invigilator s Signature :.. 2011 INTRODUCTION TO PROGRAMMING Time Allotted : 3 Hours Full Marks : 70 The figures in the margin indicate full marks. Candidates are required to give

More information

Introduction to Computing Lecture 08: Functions (Part I)

Introduction to Computing Lecture 08: Functions (Part I) Introduction to Computing Lecture 08: Functions (Part I) Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics Engineering nukhet.ozbek@ege.edu.tr Topics Uses of functions

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

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

Fundamentals of Programming

Fundamentals of Programming Fundamentals of Programming Lecture 3 - Constants, Variables, Data Types, And Operations Lecturer : Ebrahim Jahandar Borrowed from lecturer notes by Omid Jafarinezhad Outline C Program Data types Variables

More information

Control Statements Loops

Control Statements Loops CS 117 Spring 2004 Nested if statements if-else statements can reside within other if-else statements nested if statements Control Statements Loops April 26, 2004 Example (pseudocode) Get interest rate

More information