C Programming Lecture 7 : Control Structures. Control Structures Conditional statement : if, switch Determine a block of statements to execute depending.

Slides:



Advertisements
Similar presentations
BBS514 Structured Programming (Yapısal Programlama)1 Selective Structures.
Advertisements

Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Some loop programs 1: Read in 10 integers and output their sum
CS1061: C Programming Lecture 8: Repetition A. O’Riordan, 2004.
LOOP (Part 2) for while do-while 1. TK1913-C Programming2 TK1913-C Programming 2 Loop : for Loop : for Condition is tested first Loop is controlled by.
1 Lecture 5  More flow control structures  for  do  continue  break  switch  Structured programming  Common programming errors and tips  Readings:
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
Principles of Programming - NI July Chapter 5: Structured Programming In this chapter you will learn about: Sequential structure Selection structure.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 5: Structured Programming.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
Do-while loop Syntax do statement while (loop repetition condition)
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
Chapter 6: Control Structures Computer Programming Skills Second Term Department of Computer Science Foundation Year Program Umm Alqura.
1 Flowchart notation and loops Implementation of loops in C –while loops –do-while loops –for loops Auxiliary Statements used inside the loops –break –continue.
Control Statements (Decision Making)
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
Chapter 5: Structured Programming
Decision making statements. Decision making statements are used to skip or to execute a group of statements based on the result of some condition. The.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
CMSC 104, Lecture 171 More Loops Topics l Counter-Controlled (Definite) Repetition l Event-Controlled (Indefinite) Repetition l for Loops l do-while Loops.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
CSCI 171 Presentation 5. The while loop Executes a block as long as the condition is true general form: while (condition) { statement 1; statement 2;
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Introduction to Programming Lecture 7: Repeating Statements.
CMSC 104, Version 9/011 More Loops Topics Counter-Controlled (Definite) Repetition Event-Controlled (Indefinite) Repetition for Loops do-while Loops Choosing.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
COMP Loop Statements Yi Hong May 21, 2015.
Beginning C For Engineers Fall 2005 Lecture 3: While loops, For loops, Nested loops, and Multiple Selection Section 2 – 9/14/05 Section 4 – 9/15/05 Bettina.
1 Lecture03: Control Flow 9/24/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
1 ICS103 Programming in C Lecture 7: Repetition Structures.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
PGT C Programming1 Week 4 – Repetition Structures / Loops.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
 Real numbers representation - Floating Point Notation  First C Program  Variables Declaration  Data Types in C ◦ char, short, int, long, float, double,
Module 6 – Decision Control Statements Objectives  Understands Increment/Decrement operators, Conditional and special operators in C  Understands significance.
CHAPTER 4 REPETITION STRUCTURES 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 A.AlOsaimi.
Week 3.  TO PRINT NUMBERS FROM 1 TO 20  TO PRINT EVEN NUMBERS FROM 1 TO 20 2.
Exercise 2 : Using for loop Repetition (loop) (1)control variable initialization (2)Test Conditon (3)Modification of control variable value order : (1)
Chapter 5: Structured Programming
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
JavaScript: Control Statements I
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
Control Structures Lecture 7.
Arrays, For loop While loop Do while loop
- Additional C Statements
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
More Loops Topics Counter-Controlled (Definite) Repetition
Dale Roberts, Lecturer IUPUI
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Lec 6 Loop Statements Introduction to Computer Programming
CSC215 Lecture Control Flow.
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
REPETITION Why Repetition?
Presentation transcript:

C Programming Lecture 7 : Control Structures

Control Structures Conditional statement : if, switch Determine a block of statements to execute depending on whether the condition is true or false Repetition statement : for, while, do-while Loop : repeat a block of statements a number of times Conditional loop : repeat while the condition is true Other control structures : goto, …

if num1 >= num2 diff = num1 – num2 true false... if (num1 >= num2) diff = num1 – num2;...

if-else... if (num1 >= num2) diff = num1 – num2; else diff = num2 – num1;... num1 >= num2diff = num1 – num2 true false diff = num2 – num1 ?

if-else if ( grade >= 90 ) // 90 and above printf("A“); else if ( grade >= 80 ) // printf("B“); else if ( grade >= 70 ) // printf("C“); else if ( grade >= 60 ) // printf("D“); else // less than 60 printf("F“);

if example #include int main ( ) { int num1, num2, num3, min = 0; printf (“input three integers : "); scanf("%d %d %d", &num1, &num2, &num3); if (num1 < num2) if (num1 < num3) min = num1; else min = num3; else if (num2 < num3) min = num2; else min = num3; printf (“min value: %d", min); return 0; }

Compound statement block : enclosed by { } Example if ( num1 >= num2 ) { printf(“num1 is greater than num2\n“); printf(“The difference is: %d\n”, num1- num2); } else { printf(“num2 is greater than or equal to num1\n”; printf(“The difference is: %d\n”, num2 – num1); }

Ternary conditional operator ?: Example printf(“Enter two integers :”); scanf(“%d %d”,&num1, &num2); printf(“%d\n”, ((num1 >= num2)? num1–num2: num2-num1)); false true print num1-num2 Print num2-num1 num1 >= num2

Dangling Else Problem if a then if b then s1 else s2

switch The value in switch statement has many cases. int main() { int value; scanf(“%d”,&value); switch (value) { case 1 : printf(“1 received\n”); break; case 2 : printf(“2 received\n”); break; default : printf(“ values except 1 and 2 were received.\n”); break; } return 0; }

while num <= 10 sum = sum + num; num = num + 1; true false num = 1; Sum = 0; num = 1; sum = 0; while (num <= 10) { sum = sum + num; num = num + 1; }

do-while num = 1; sum = 0; do { sum = sum + num; num = num + 1; } while (n <= 10) sum = sum + num; num = num + 1; true num <= 10 false num = 1; Sum = 0; The body (block) of do- while statement is executed at least once.

while example #include int main () { int total = 0, score, count = 0; float average; printf (“score input (quit:0): \n"); scanf("%d",&score); while (score != 0) { total += score; count++; scanf("%d",&score); } if (count == 0) printf (“No input received!"); else { average = (float) total / count; printf (“total: %d \n", total); printf (“average: %5.2f \n", average); } return 0; }

for Repetition (1)control variable initialization (2)Test Conditon (3)Modification of control variable value order : (1) (2) body (3) (2) body (3) (2) body … body (3) (2) * Example for(counter = 1; counter <= 10; counter++ ) printf(“%d\n”,counter); for ( (1); (2); (3) ) { // for-repetition body // {} is not necessary // if there is only one statement in body } (2) body TRUE FALSE (1) (3)

for example #include int main () { int total = 0, score, count; float average; printf (“score input (quit:0): "); scanf("%d",&score); for (count=0; score != 0; count++) { total += score; scanf("%d",&score); } if (count == 0) printf (“No input received!"); else { average = (float) total / count; printf (“total: %d \n", total); printf (“avarage: %5.2f \n", average); } return 0; }

break break in loop Go out of the loop block and execute next to the loop example while (1) { scanf("%d",&j) if (j == 0) break; result = i/j; }

continue continue in loop Go to condition test of the loop Example for (i = 0, sum = 0; i <= n; i++) { if (i % 2 == 0) continue; sum += i; }

Nested Loop loop in a loop int main () { int i, j; for (i=1; i<10; i++) { printf ("%d-th iteration \n", i); for (j = 1; j < 10; j++) printf("%d X %d = %d\n", i, j, i*j); printf ("\n", i); } return 0; }

Infinite Loop If the condition of the loop is always TRUE, the body of the loop is executed infinitely example while(1) { i=0; i++; printf(“%d”,i); } int count = 1; while (count != 100) count += 2;