Like this presentation? Why not share!

# Loops

## by Learn By Watch on Apr 27, 2010

• 2,950 views

### Views

Total Views
2,950
Views on SlideShare
2,829
Embed Views
121

Likes
0
49
0

### 4 Embeds121

 http://www.ustudy.in 77 http://en.learnbywatch.com 41 http://www.slideshare.net 2 http://ustudy.in 1

## LoopsPresentation Transcript

• C Programming Language By: Yogendra Pal yogendra@learnbywatch.com Dedicated to My mother and Father
• t y Keep your notebook with you. Write important point and questions that comes in your mind Solve Mind band exercise. C Rewind when not clear Ask Questions by call or SMS or by mail Keep Watching Keep Learning THIS IS LOOPS 2
• Need • Consider a problem: – Write a program that print “Hello” 5 times. – One possible statement to solve this problem is : • printf(“Hello n Hello n Hello n Hello n Hello); – But what if we want to print “Hello” 1000 times? – We need a control structure that repeatedly execute the following statement. • printf(“n Hello);
• Decision statements • To change the control flow of a program we use: – Branching statements : cause one section of code to be executed or not executed, depending on a conditional clause. – Looping statements : used to repeat a section of code a number of times or until some condition occurs.
• Looping Statements • Allow the program to repeat a section of code any number of times untill some condition occures.
• Looping statements : 1 • while statement initial_statement; – General form: while(condition){ statement; while(condition) iteration_statement; statement; } • The program will repeatedly execute the statement inside the while until the condition becomes false (0)
• Problems • Write a program that print “Hello” 5 times. while(counter <= 5) printf(“Hello”); • This statement reads – “while counter is less than or equal to 5 print Hello ”
• Multiple Statements • Put all statements inside the curly { } braces. while(condition) { statement 1; statement 2; statement 3; }
• (I ++) or (++ I) • while (i++ < 10) – compare i with 10, – increment i. • while (++i < 10) – increment i, – compare i with 10.
• Problems • Write a program that print numbers from 1 to 100. • Write a program that print sum of 10 consecutive numbers, enter first number using keyboard. • Write a program that print all even numbers from 1 to 100. • Write a program to find the factorial value of any number entered through the keyboard.
• break statement • break statement: – Loops can be exited at any point through the use of a break statement. • Write a program that checks whether a given number is prime of not.
• continue statement • continue statement: – continue starts re-executing the body of the loop from the top. • Write a program that print numbers from 1 to 100 except the numbers that are divisible by 5 using continue statement.
• Nested while while(condition) while(condition) while(condition) { statement; while(condition) { statement; while(condition) } { } if(condition) statement; else statement; }
• Mind Bend Write a program that print all the numbers from 2 to 100 with the indication whether a number is prime of not. Output will be like: 2 : prime 3 : prime 4 : not prime
• Looping Statement : 2 • for statement • General form: for (initial_statement; condition; iteration_statement) body_statement; initial_statement; while(condition){ statement; iteration_statement; }
• Problems • Two numbers (x and y) are entered through the keyboard. Write a program to find the value of xy • Write a program that print numbers from 1 to 100 except the numbers that are divisible by 5 and 7. • Write a program to print all characters with their ASCII value. 16
• Looping Statement : 3 • do-while loop do{ statement1; statement2 }while(condition) • do-while run the statements at least once.
• Problems • Write a program that first take two numbers from user and then one operator (+, -, *, /, %) and print the result accordingly. • Now modify the above program that give an option to the user to repeat the operation again and again with different operator choosen by him.
• Mind Bend • Write a program that takes 10 floating-point numbers as a input from user and print the greatest number. • Write a program that take floating point number from user and print – ICE if it is less than 0. – WATER if it lies between 0 and 100. – STEAM if it is greater then 100. 19
• To get complete benefit of this tutorial solve all the quiz on www.learnbywatch.com For any problem in this tutorial mail me at yogendra@learnbywatch.com with the subject “C” For Other information mail at info@learnbywatch.com Keep Watching Keep Learning NEXT IS FUNCTIONS 20