WHILE, DO-WHILE AND FOR LOOPS

Slides:



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

Algorithm: Step by step procedure of solving a particular problem. Pseudo code: Artificial informal language used to develop algorithms. Flow chart: Graphical.
Understanding Loops Using C Language Week 15 Mr.Omer Salih.
D EVELOPING P ROGRAMS USING ALGORITHMS AND FLOW CHARTS AND VICE VERSA Week 14 Mr.Mohammed Rahmath.
לולאות 02 יולי יולי יולי 1502 יולי יולי יולי 1502 יולי יולי יולי 15 1 Department of Computer Science-BGU.
1 Agenda - Loops while for for & while Nested Loops do-while Misc. & Questions.
 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.
Real World Applications: Statistical Measures Problem (page 95-98) Read a series of floating-point numbers from the standard input, and print a statistical.
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
 An instruction or group of instructions.  Computer executes program repeatedly for specified number of times. Or until some terminating conditions are.
Loop Control Structures L11-L14. OBJECTIVES To learn and appreciate the following concept The for Statement Relational Operators Nested for Loops for.
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)
C Programming Lecture 7 : Control Structures. Control Structures Conditional statement : if, switch Determine a block of statements to execute depending.
Iterations Very Useful: Ability to repeat a block of code Example:
Example #include void main() { int a,b,c,n; clrscr(); printf("\nEnter the value of a,b:"); scanf("%d%d",&a,&b); printf("\nMENU"); printf("\n1.ADD\n2.SUB\n3.MULTIPLY\n0.EXIT");
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;
Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.
Strings program. C Program to Check if a given String is Palindrome #include void main() { char string[25], reverse_string[25] = {'\0'}; int i, length.
Strings program. C Program to Check if a given String is Palindrome #include void main() { char string[25], reverse_string[25] = {'\0'}; int i, length.
For Loop Lecture No 8. Definition In computer science a for loop is a programming language statement which allows code to be repeatedly executed. A for.
Asstt. Prof Mandeep Kaur Computer Dept. TOPIC: Control Statements in C language.
Sum of Arithmetic Sequences. Definitions Sequence Series.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops
Algorithm & Flow Charts Decision Making and Looping
UNIT-3. Repetitive control structures – Pre-test and post-test loops, initialization and updation, event and counter controlled loops, while, do..while,
CSC COMPUTER EDUCATION, M.K.B.NAGAR CHAPTER 3. CSC COMPUTER EDUCATION, M.K.B.NAGAR Session Objectives Explain If, If..else statements Understand Looping.
The following statements are for y = -1; if ( x ) if ( x>0 ) y = 1; else y = 0; A. y= -1 x0 B. y= 0 x0 C. y= 1 x
Module 6 – Decision Control Statements Objectives  Understands Increment/Decrement operators, Conditional and special operators in C  Understands significance.
Week 3.  TO PRINT NUMBERS FROM 1 TO 20  TO PRINT EVEN NUMBERS FROM 1 TO 20 2.
The Repetition control structure using while loop.
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
Unit 3 Control Structure
Control Structures (Repetition structure) Jump Statements
Control Structures Introduction
Lecture 4b Repeating With Loops
‘C’ Programming Khalid Jamal.
CSE 220 – C Programming Loops.
Chapter 6: Loops.
INTRODUCTION TO C.
LESSON 3 IO, Variables and Operators
C Program Controls + Flow Structure
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Condition Statements.
JUMP STATEMENT C++ has the four statements that perform an unconditional branch. They are, 1. return 2. goto 3. break 4. continue In addition to four statements.
CHAPTER 7 RECURSIVE FUNCTION
Chapter 2.2 Control Structures (Iteration)
Functions Dr. Ashish Sasankar. In programming, a function is a segment that groups code to perform a specific task. A C program has at least one function.
INC 161 , CPE 100 Computer Programming
Programming in C Part 2.
Looping.
CS1100 Computational Engineering
Conditional Construct
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.
Recursion Prog #include <stdio.h> #include<conio.h> main()
UMBC CMSC 104 – Section 01, Fall 2016
Control Statements_2.
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
REPETITION Why Repetition?
break & continue Statements
Control Structure គោលបំណងនៃមេរៀន អ្វីជា Control structure ?
Presentation transcript:

WHILE, DO-WHILE AND FOR LOOPS

Program to check whether a given number is prime or not. void main() { int i=1, n, count=0; printf(“Enter a number”); scanf(“%d”,&n); while(i<=n) { if(n%i==0) count++; i++; } if(count==2) printf(“It is prime number”); else printf(“Not a prime number”);

Class work Problems 1 write a C program to print “Welcome to C programming” 5 times, each time in new line. 2 Write a C program to display all the prime numbers between 50 and 100.

Program to find the factorial of a given integer using entry control while loop void main() { int i=1, n, fact=1; printf(“Enter the number”); scanf(“%d”, &n); while(i<=n) fact = fact * i; i++; } printf(“Factorial=%d”, fact);

Program to find the factorial of a given integer using exit control do-while loop void main() { int i=1, n, fact=1; printf(“Enter the number”); scanf(“%d”, &n); do fact = fact * i; i++; } while(i<=n); printf(“Factorial=%d”, fact); }

Difference between While and Do- While Loop It is an Entry Control Loop. Condition or Test expression is checked at the beginning. It is an Exit Control Loop. Condition or Test expression is checked at the last. Statements inside the while loop will be executed only if the condition or test expression is true. Statements inside the do-while loop will execute at least once even if the condition or test expression is false.

Difference Example void main() { int a=5,b=6; while(a>b) printf(“\n%d”, a); a++; } getch(); do } while(a>b);

Sum of digits of a number void main() { int n, i, sum=0, rem; printf(“Enter any number”); scanf(“%d”, &n); while(n>0) rem = n % 10; sum = sum + rem; n = n / 10; } printf(“Sum of digits of number=%d”, sum);

Program to reverse a number void main() { int n, rev=0, rem; printf(“Enter any number”); scanf(“%d”, &n); while(n>0) rem = n % 10; rev = rev * 10 + rem; n = n / 10; } printf(“Reverse of given number=%d”, rev);

Classwork problems 1 write a program to check whether a given number is Harshad number or not using do-while loop or exit control loop. 2 write a program to print all the harshad numbers between 1 and 100 using entry control loop or while loop. 3 write a program to check whether a given number is palindrome or not.

For loop for(initialization ; test expression ; updation) { statements; }

Example to print first n odd natural numbers void main() { int limit, i; printf(“Enter the limit”); scanf(“%d”, &limit); for(i=1 ; i<=limit ; i++) if( i % 2!= 0 ) printf(“%d”, i); } getch();

Classwork problems 1 write a program to check whether the given number is perfect or not using for loop.

Program to generate Fibonacci series void main() { int a=0, b=1, c, i, limit; printf(“Enter the limit”); scanf(“%d”, &limit); if(limit==1) printf(“%5d”, a); else if(limit==2) printf(“%5d%5d”, a, b); else { for(i=1 ; i<=n-2 ; i++) c = a + b; printf(“%5d”, c); a = b; b = c; } getch()

Classwork problems 1 write a program to generate the Lucas sequence. 2 write a program to check whether a given 3 digit number is armstrong or not?

Program to generate Lucas Sequence void main() { int a=1,b=3,c,n,i; clrscr(); printf("Enter the limit\n"); scanf("%d",&n); if(n==1) printf("%5d",a); else if(n==2) printf("%5d%5d",a,b); else { for(i=1;i<=n-2;i++) c=a+b; printf("%5d",c); a=b; b=c; } getch();

Program to check whether a given integer is a Harshad number or not. void main() { int n,sum=0,i,rem,num; clrscr(); printf("Enter the number\n"); scanf("%d",&n); num=n; for(;n>0;) rem=n%10; sum=sum+rem; n=n/10; } if(num%sum==0) printf("number is Harshad Number"); else printf("number is not a Harshad Number"); getch();

Program to check whether a number is Armstrong or not. #include<stdio.h> #include<conio.h> #include<math.h> void main() { int n,i,rem,num,digit_count=0,sum=0; clrscr(); printf("Enter the number\n"); scanf("%d",&n); num=n; while(n>0) n=n/10; digit_count++; } n=num; do rem=n%10; sum=sum+pow(rem,digit_count); } while(n>0); if(num==sum) printf("number is Armstrong Number"); else printf("number is not an Armstrong Number"); getch();

Unconditional Statements 1 break 2 continue 3 goto

Program to check prime number using break statement. void main() { int n,i,flag=0; clrscr(); printf("Enter the number\n"); scanf("%d",&n); for(i=2;i<=n/2;i++) if(n%i==0) flag=1; break; //Using break Here… } if(flag==0) printf("Prime number"); else printf("Not a prime number"); getch();

Program using Continue Statement. void main() { int i; clrscr(); for(i=200;i<=300;i++) if(i%2==0) continue; // Continue used Here… printf("%5d",i); } getch();

goto statement example 1 //To count the number of digits of a number. void main() { int i,n,count=0,L1; clrscr(); printf("Enter the number to count its number of digits"); scanf("%d",&n); L1: if(n>0) n=n/10; count++; goto L1; } printf("digit count=%d",count); getch();