Week 6 CPS125.

Slides:



Advertisements
Similar presentations
Algorithm & Flow Charts Decision Making and Looping Presented By Manesh T Course:1090 CS.
Advertisements

CS201 - Repetition loops.
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
 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.
Do-while loop Syntax do statement while (loop repetition condition)
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
CECS 121 Test 1. Functions allow you to group program statements under one name C and C++ are case-sensitive so main(), MAIN(), and Main() are all different.
1. Agenda for loop Short-handed notation How to use break and continue 2.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
C Programming Lecture 7 : Control Structures. Control Structures Conditional statement : if, switch Determine a block of statements to execute depending.
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 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Program Looping Why we need loops in our code –Make code concise for repetitive processes When to use loops –Run a block of code repetitively –Process.
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”
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.
PGT C Programming1 Week 4 – Repetition Structures / Loops.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops
Algorithm & Flow Charts Decision Making and Looping
Arrays Name, Index, Address. Arrays – Declaration and Initialization int x; y[0] y[1] y[2]
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)
Lesson #5 Repetition and Loops.
Control Structures (Repetition structure) Jump Statements
(Numerical Arrays of Multiple Dimensions)
Control Structures and Data Files
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Lecture 7: Repeating a Known Number of Times
Lesson #5 Repetition and Loops.
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
CS1010 Programming Methodology
Lecture 13 & 14.
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
The nested repetition control structures & Continue Statements
Control Structures Lecture 7.
Arrays, For loop While loop Do while loop
CS1100 Computational Engineering
Outline Altering flow of control Boolean expressions
Lesson #5 Repetition and Loops.
Lec 7.
Chapter 6 Decision Making and Looping
Lec 6.
Chapter 4 - Program Control
Exam 1 Date: Feb. 2nd, 2015 during class time (50 minutes) Coverage
Loops in C.
A function with one argument
Program Control Topics While loop For loop Switch statement
Further C Programming Variable types Loops Conditional statements
Week 6 CPS125.
REPETITION STATEMENTS
Computer programming Lecture 3.
Week 3 – Program Control Structure
CPS125 Week
Lesson #5 Repetition and Loops.
Statements in C Programming
FUNCTION ||.
ICS103: Programming in C 5: Repetition and Loop Statements
Week 3 – Repetition (ctd.)
Chapter 13 Control Structures
Presentation transcript:

Week 6 CPS125

/*For Lesson for-1 */ #include <stdio.h> void main(void) { int day, hour, minutes; for(day=1; day<=3; day++) printf("Day=%2d\n", day); for (hour=5; hour>2; hour--) { minutes = 60 * hour; printf("Hour = %2d, Minutes=%3d\n",hour, minutes); }

/*For Lesson for-2 */ #include <stdio.h> void main(void) { int i, j, k, m=0; for (i=1; i<=5; i+=2) { for (j=1; j<=4; j++) k = i+j; printf("i=%3d, j=%3d, k=%3d\n", i, j, k); } m=k+i; printf("i=%3d, j=%3d, k=%3d, m=%3d \n", i, j, k,m);

HOME WORK!!!!!!!!! EXTRA PRACTICE! /*For Lesson for-3 */ #include <stdio.h> void main(void) { int i, isum1=0, isum2=0, N=9; float f, x, sum1=0.0, sum2=0.0; /*What is the result if we use double type*/ x=1.0/N; printf("x=%f\n",x); for (i=1;i<=N;i++) {sum1 +=x; isum1+=1;} printf("sum1=%f, isum1=%d\n",sum1, isum1); for (f=x;f<=1.0;f+=x) {sum2+=x; isum2+=1;} printf("sum2=%f, isum2=%d\n",sum2, isum2); }

Write a Program!!!!!!! TO PRINT NUMBERS FROM 1 TO 20 TO PRINT EVEN NUMBERS FROM 1 TO 20

/* PROGRAM # 24 */ /*TO PRINT NUMBERS FROM 1 TO 20 */ #include<stdio.h> main( ){ int i; int limit=20; for(i = 1; i <= limit; i=i+1){ printf(“%d “, i); } puts(“ STOPPPP!!!“);

/* PROGRAM # 25 */ /*TO PRINT EVEN NUMBERS FROM 1 TO 20 */ #include<stdio.h> main( ){ int i; int limit=20; for(i= 2; i<= limit; i=i+2){ printf(“%d “, i); } puts(“ EVEN NUMBER! “);

Nested if … else #include <stdio.h> int main (void) { int wilma, a, b,c; scanf("%d%d%d", &a,&b,&c); if (a > b&& a>c){ wilma = (a - b*c); printf ("%d\n", wilma);} else if (b>a||b>c){ wilma = (b); printf ("%d\n", wilma); } wilma = (a*b*c); return (0);

Write a Program!!!!!!! TO AVERAGE ODD NUMBERS FROM 1 TO 20

/* PROGRAM # 26 */ /*TO AVERAGE ODD NUMBERS FROM 1 TO 20 */ #include<stdio.h> main( ){ /* Declare the variables */ int i; float ave; int limit=20, sum=0, count=0; /* Initialize variables */ /* Print odd numbers 1 through 20 */ for(i= 1; i< limit; i=i+2){ printf(“%d “, i); sum+=i; count++; } /* Calculate average print the result */ ave=(float)sum/count; printf(“The average is %f.\n”, ave);

HOME WORK!! TO PRINT MULTIPLES OF 3 FROM 1 TO 99 IN A MATRIX FORMAT HAVING 5 COLUMNS TO PRINT MULTIPLES OF 3 FROM 1 TO 99 IN A MATRIX FORMAT HAVING 4 COLUMNS.

The while Loop condition is true */ Another type of looping structure Application: when we don't know how many times a loop is going to be repeated. Any FOR LOOP can be re-written as a WHILE LOOP, but the other way around is not necessary true. General Syntax: while ( condition ){ /* BODY OF WHILE LOOP statements executed/repeated if condition is true */ } Explanation: As long as the condition is true, the body of the while loop will be executed. If the condition is false, the statement after the Body of while-loop will be executed.

The while statement The while statement permits the repetition of the statements until the condition becomes false.