20
C Programming Language By: Yogendra Pal [email protected] Dedicated to My mother and Father

Loops

  • View
    8

  • Download
    2

Embed Size (px)

DESCRIPTION

 

Citation preview

CProgramming Language

By:

Yogendra [email protected]

Dedicated to My mother and Father

THIS IS LOOPS

Keep Watching Keep Learning

2

t Keep your notebook with you.

yWrite important point and questions that comes in your mind

Solve Mind band exercise.

Ask Questions by call or SMS or by mail

CRewind when not clear

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 codeany number of times untill some conditionoccures.

Looping statements : 1

• while statement

– General form:

• The program will repeatedly execute thestatement inside the while until the conditionbecomes false (0)

while(condition)

statement;

initial_statement;

while(condition){

statement;

iteration_statement;

}

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 consecutivenumbers, enter first number using keyboard.

• Write a program that print all even numbers from1 to 100.

• Write a program to find the factorial value of anynumber entered through the keyboard.

break statement

• break statement:

– Loops can be exited at any point through the use ofa break statement.

• Write a program that checks whether a givennumber is prime of not.

continue statement

• continue statement:

– continue starts re-executing the body of the loopfrom 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)

statement;

while(condition)

{

while(condition)

{

statement;

}

}

while(condition)

{

if(condition)

statement;

else

statement;

}

Write a program that print all the numbersfrom 2 to 100 with the indication whether anumber is prime of not.

Output will be like:

2 : prime

3 : prime

4 : not prime

Mind Bend

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 throughthe keyboard. Write a program to find thevalue of xy

• Write a program that print numbers from 1to 100 except the numbers that are divisibleby 5 and 7.

• Write a program to print all characters withtheir ASCII value.

16

Looping Statement : 3

• do-while loop

• do-while run the statements at least once.

do{

statement1;

statement2

}while(condition)

Problems

• Write a program that first take two numbersfrom user and then one operator (+, -, *, /, %)and print the result accordingly.

• Now modify the above program that give anoption to the user to repeat the operationagain and again with different operatorchoosen by him.

• Write a program that takes 10 floating-pointnumbers as a input from user and print thegreatest number.

• Write a program that take floating pointnumber 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.

Mind Bend

19

NEXT IS FUNCTIONSKeep Watching Keep Learning

To get complete benefit of this tutorial solve all the quiz on

www.learnbywatch.com

For any problem in this tutorial mail me at

[email protected]

with the subject “C”

For Other information mail at

[email protected]

20