Control Structures (Repetition structure) Jump Statements

Slides:



Advertisements
Similar presentations
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
Advertisements

COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
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.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
CS1061: C Programming Lecture 8: Repetition A. O’Riordan, 2004.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
 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.
Do-while loop Syntax do statement while (loop repetition condition)
do - while  while: Execute the body of the loop at least once
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.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
Chapter 8 Iteration Dept of Computer Engineering Khon Kaen University.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Looping Construct or Statements. Introduction of looping constructs In looping,a sequence of statements are executed until some condition for termination.
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
Repetition Repetition allows you to repeat an operation or a series of operations many times. This is called looping and is one of the basic structured.
BY ILTAF MEHDI (MCS, MCSE, CCNA)1. INSTRUCTOR: ILTAF MEHDI (MCS, MCSE, CCNA, Web Developer) BY ILTAF MEHDI (MCS, MCSE, CCNA)2 Chapter No: 04 “Loops”
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
LOOPS IN ‘C’ PROGRAMMING. V ERY O FTEN, Y OU W ILL W ANT TO D O S OMETHING M ORE T HAN O NCE HA.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops
The Repetition control structure using while loop.
Computer Programming -1-
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
ECE Application Programming
‘C’ Programming Khalid Jamal.
CSE 220 – C Programming Loops.
Chapter 6: Loops.
REPETITION CONTROL STRUCTURE
EKT120 COMPUTER PROGRAMMING
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Quick Test What do you mean by pre-test and post-test loops in C?
The nested repetition control structures & Continue Statements
Chapter 2.2 Control Structures (Iteration)
JavaScript: Control Statements.
Control Structures Lecture 7.
INC 161 , CPE 100 Computer Programming
Looping.
- Additional C Statements
Lec 7.
Additional Control Structures
Chapter 6 Decision Making and Looping
Chapter 4 - Program Control
Exam 1 Date: Feb. 2nd, 2015 during class time (50 minutes) Coverage
Loops in C.
Iteration: Beyond the Basic PERFORM
CSC215 Lecture Control Flow.
Chapter 2.2 Control Structures (Iteration)
Lab5 PROGRAMMING 1 Loop chapter4.
UMBC CMSC 104 – Section 01, Fall 2016
More Loops Topics Counter-Controlled (Definite) Repetition
Dale Roberts, Lecturer IUPUI
More Loops Topics Counter-Controlled (Definite) Repetition
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Repetition Structures
CSC215 Lecture Control Flow.
More Loops Topics Counter-Controlled (Definite) Repetition
Programming Fundamental
More Loops Topics Counter-Controlled (Definite) Repetition
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

Control Structures (Repetition structure) Jump Statements CSE101-Lec#8 Control Structures (Repetition structure) Jump Statements

Outline Repetition structure/Control Loop Statements Jump Statements for statement while statement do-while statement Jump Statements break continue goto return

Repetition(Going to School) Day = Monday to Saturday

Repetition Statement A repetition statement allows you to specify that an action is to be repeated while some condition remains true.

Looping (repetition) What if we want to display hello 500 times? Quick yak: Share that if one might have known this earlier: How easy it might have been to undergo the punishment of writing 100 times “I will not talk in class” in school. What if we want to display hello 500 times? Should we write 500 printf statements or equivalent ? Obviously not. It means that we need some programming facility to repeat certain works. Such facility is available in form of looping statements.

Loop The main idea of a loop is to repeat an action or a series of actions. The concept of a loop without condition

But, when to stop looping? Quick yak: Condition specific eject can enumerated as: Stop walking if destination is reached Start rinsing after said repetition of brush movements But, when to stop looping? In the following flowchart, the action is executed over and over again. It never stops – This is called an infinite loop Solution – put a condition to tell the loop either continue looping or stop.

Loop A loop has two parts – body and condition Body – a statement or a block of statements that will be repeated. Condition – is used to control the iteration – either to continue or stop iterating.

Loop statements C provides three loop statements:

The “while” Statement in C The syntax of while statement in C: while (loop repetition condition){ statement; updating control; } Syntax While fatigue level is not reached

while statement while(loop repetition condition) { Statements; } Loop repetition condition is the condition which controls the loop. The statement is repeated as long as the loop repetition condition is true. A loop is called an infinite loop if the loop repetition condition is always true.

while statement Quick yak: To repeat the car game when game has ended i.e. life is equal to zero . That is while the choice is not equal to ‘n’o. Example: This while statement prints numbers 10 down to 1 #include<stdio.h> int main() { int n=10; while (n>0){ printf(“%d ”, n); n=n-1; } Do TEN push ups imposes a count condition 10 9 8 7 6 5 4 3 2 1

The for Statement in C The syntax of for statement in C: The initialization-expression set the initial value of the loop control variable. The loop-repetition-condition test the value of the loop control variable. The update-expression update the loop control variable. for (initialization-expression; loop-repetition-condition; update-expression){ statement; } Syntax

for statement for (Initialization; Condition; Updating) { Quick yak: For loop to repeat the car game from life = 5 to life > 0. for (Initialization; Condition; Updating) { Repeated_Actions; }

for statement Example: This for statement prints numbers 10 down to 1 #include<stdio.h> int main() { int n; for (n=10; n>0; n=n-1){ printf(“%d ”, n); } Do TEN push ups = for count=1; count<=10; count++ 10 9 8 7 6 5 4 3 2 1

Nested Loops Nested loops consist of an outer loop with one or more inner loops. Eg: for (i=1;i<=100;i++){ for(j=1;j<=50;j++){ … } The above loop will run for 100*50 iterations. Outer loop Inner loop

Program to print tables up to a given number. #include<stdio.h> void main() { int i,j,k ; printf(“Enter a number:”); scanf(“%d”, &k); printf(“the tables from 1 to %d: \n”,k); for(i=1; i<k; i++){ for(j=1; j<=10; j++){ printf(“%d ”,i*j); } //end inner for loop printf(“\n”); } //end outer for loop getch(); } //end main Program to print tables up to a given number. Quick yak: Another example would be to display prime numbers upto any given number K =100 Number: 1 cube: 1 Number:2 cube: 8 Number: 3 cube: 27 Number: 4 cube: 64 Enter a number 4 The tables from 1 to 4 1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40

Program to display a pattern. #include<stdio.h> #include<conio.h> void main() { int i,j; printf(“Displaying right angled triangle for 5 rows”); for(i=1 ; i<=5 ; i++) { for(j=1 ; j<=i ; j++) printf(“* ”); printf(“\n”); } Program to display a pattern. Quick yak: Tell them to display various patterns, ex: reverse right angle triangle, isometric triangle etc. Displaying right angled triangle for 5 rows * * * * * * * * * * * * * * *

While vs. for statements Quick yak: Daily life ex: While: (date<=last day of class) {Attend class; } For: (rad=1;rad<=10; rad++) Circle(x,y,rad) Will make 10 concentric circles Comparing for and while loops

The do-while Statement in C The syntax of do-while statement in C: The statement executed at least one time. For second time, If the condition is true, then the statement is repeated else the loop is exited. do { statement; } while (condition); Syntax

do…while statement do { Repeated_Actions; } while (Condition);

do…while statement Example: this do…while statement prints numbers 10 down to 1 #include<stdio.h> int main() { int n=10; do{ printf(“%d ”, n); n=n-1; }while (n>0); } 10 9 8 7 6 5 4 3 2 1

Difference between while and do..while Quick yak: The difference is like Ask age before VOTING: Allow/Disallow Ask age after VOTING: Reject/accept while loop do..while loop 1. Condition is specified at the top 1. Condition is mentioned at the bottom 2. Body statements are executed when the condition is satisfied 2. Body statements are executed at least once even if the expression value evaluates to false 3. It is an entry controlled loop 3. It is an exit controlled loop 4.Syntax: while (condition) statement; do { statements; } while (condition);

Jump statements You have learn that, the repetition of a loop is controlled by the loop condition. C provides another way to control the loop, by using jump statements. There are four jump statements:

break statement break is a keyword. break allows the programmer to terminate the loop. A break statement causes control to transfer to the first statement after the loop or block. The break statement can be used in nested loops. If we use break in the innermost loop then the control of the program is terminated only from the innermost loop.

break statement Program to show use of break statement. ##include<stdio.h> int main() { int n; for (n=10; n>0; n=n-1){ if (n<8) break; printf(“%d ”, n); } //end for } Program to show use of break statement. 10 9 8

continue statement continue statement is exactly opposite to break. continue statement is used for continuing the next iteration of the loop statements When it occurs in the loop, it does not terminate, but skips the statements after this statement

continue statement In while and do…while loops, the continue statement transfers the control to the loop condition. In for loop, the continue statement transfers the control to the updating part.

continue statement #include<stdio.h> int main() { int n; for (n=10; n>0; n=n-1){ if (n%2==1) continue; printf(“%d ”, n); } Program to show the use of continue statement in for loop 10 8 6 4 2

continue statement #include<stdio.h> int main() { int n = 10; while(n>0){ printf(“%d”, n); if (n%2==1) continue; n = n –1; } Program to show the use of continue statement in for loop For n=9, loop goes to infinite execution The loop then prints number 9 over and over again. It never stops. 10 9 9 9 9 9 …………

goto Unconditionally transfer control. goto may be used for transferring control from one place to another. The syntax is: goto identifier; Control is unconditionally transferred to the location of a local label specified by identifier. For example, Again: ... goto Again;

goto statement Output: 10 9 8 7 6 5 4 3 2 1 n=10; A: printf(“%d “, n); n = n -1; if (n>0) goto A; Output: 10 9 8 7 6 5 4 3 2 1

Program to show goto statement. #include<stdio.h> void main() { int x; printf(“enter a number: ”); scanf(“%d”,&x); if(x%2==0) goto even; else goto odd; even: printf(“ %d is even”, x); return; odd: printf(“%d is odd”, x); } Program to show goto statement. enter a number: 18 18 is even

return statement Exits the function. Quick yak: Return can be enumerated by example of sugar in tea i.e. Put more sugar is returned by the function check_sugar() Exits the function. return exits immediately from the currently executing function to the calling routine, optionally returning a value. The syntax is: return [expression]; For example, int sqr (int x){ return (x*x); }

Next Class: Formatted and Unformatted Input/Output functions