26
Loops(2) do-while, for Computer Programming

Loops(2) do-while, forceng198.cankaya.edu.tr/uploads/files/ceng198 week6.pdf · Do While Loop do-while loop is the same as while loop except that the condition is checked at the end

  • Upload
    others

  • View
    14

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Loops(2) do-while, forceng198.cankaya.edu.tr/uploads/files/ceng198 week6.pdf · Do While Loop do-while loop is the same as while loop except that the condition is checked at the end

Loops(2)

do-while, for

Computer Programming

Page 2: Loops(2) do-while, forceng198.cankaya.edu.tr/uploads/files/ceng198 week6.pdf · Do While Loop do-while loop is the same as while loop except that the condition is checked at the end

Topics

• Repeating Statements (review)

• while loop (review)

• do-while loop

• for loop

• Infinite loops

• Examples

Page 3: Loops(2) do-while, forceng198.cankaya.edu.tr/uploads/files/ceng198 week6.pdf · Do While Loop do-while loop is the same as while loop except that the condition is checked at the end

Repeating Statements In many cases same operations are repeated for different

data.

For example, finding average of a student is repeated for other students too.

In these cases, we can use a statement called a loop

The statements inside a loop are repeated until its condition becomes false.

C uses three types of loops:

While loop (discussed last week)

Do-while loop

For loop

Page 4: Loops(2) do-while, forceng198.cankaya.edu.tr/uploads/files/ceng198 week6.pdf · Do While Loop do-while loop is the same as while loop except that the condition is checked at the end

While Loop While loop repeats a group of C statements until its

condition becomes false

While loop has the following syntax:

while(condition)

[statements];

Condition is a logical expression

Statements inside the while loop are called body of while loop

If more than one statement is written inside body of a while loop, they should be put in {}

Page 5: Loops(2) do-while, forceng198.cankaya.edu.tr/uploads/files/ceng198 week6.pdf · Do While Loop do-while loop is the same as while loop except that the condition is checked at the end

Example 1

Write a program to print all positive even integers less than

100.

The output should be like:

2

4

6

…..

96

98

Page 6: Loops(2) do-while, forceng198.cankaya.edu.tr/uploads/files/ceng198 week6.pdf · Do While Loop do-while loop is the same as while loop except that the condition is checked at the end

#include<stdio.h>

void main()

{

int count;

count = 2;

while( count < 100 )

{

printf(“%d\n”, count);

count = count + 2;

}

}

Page 7: Loops(2) do-while, forceng198.cankaya.edu.tr/uploads/files/ceng198 week6.pdf · Do While Loop do-while loop is the same as while loop except that the condition is checked at the end

Example 2

Write a program to read a series of positive integers and find their

sum until -1 is entered.

Example input can be:

3 9 11 -1

Output: 23

Example input can also be:

-1

Output: 0

In all cases the input has at least one number (at lease -1 is entered)

Page 8: Loops(2) do-while, forceng198.cankaya.edu.tr/uploads/files/ceng198 week6.pdf · Do While Loop do-while loop is the same as while loop except that the condition is checked at the end

#include<stdio.h>

void main()

{

int num, sum;

sum = 0;

printf(“Enter an integer:\n”);

scanf(“%d”,&num);

while( num != -1)

{

sum = sum + num;

printf(“Enter an integer:\n”);

scanf(“%d”,&num);

}

printf(“The sum of the numbers is %d \n”, sum);

}

Page 9: Loops(2) do-while, forceng198.cankaya.edu.tr/uploads/files/ceng198 week6.pdf · Do While Loop do-while loop is the same as while loop except that the condition is checked at the end

Do While Loop

do-while loop is the same as while loop except that the

condition is checked at the end of the loop.

do-while syntax is:

do

{

[Loop Body]

}while( condition);

The body is execute until the condition becomes false

As the condition is checked at the end, the loop is executed

at least once.

Page 10: Loops(2) do-while, forceng198.cankaya.edu.tr/uploads/files/ceng198 week6.pdf · Do While Loop do-while loop is the same as while loop except that the condition is checked at the end

Example 3

Re-write previous example with do-while loop

[Notice that scanf is written only once]

Page 11: Loops(2) do-while, forceng198.cankaya.edu.tr/uploads/files/ceng198 week6.pdf · Do While Loop do-while loop is the same as while loop except that the condition is checked at the end

#include<stdio.h>

void main()

{

int num, sum;

sum = 0;

do

{

printf(“Enter an integer:\n”);

scanf(“%d”,&num);

if( sum != -1 )

sum = sum + num;

} while( num != -1);

printf(“The sum of the numbers is %d \n”, sum);

}

Page 12: Loops(2) do-while, forceng198.cankaya.edu.tr/uploads/files/ceng198 week6.pdf · Do While Loop do-while loop is the same as while loop except that the condition is checked at the end

For Loop (1)

Whenever you know the number of repetitions in a loop, for

loop is used.

In a loop which counts the repetitions, you need a counter ( a

variable defined in your program)

The counter is initialized before the loop.

The counter is updated inside the body of the loop

For loop does:

Gives initial value to counter

Updates the counter

Checks loop condition

Page 13: Loops(2) do-while, forceng198.cankaya.edu.tr/uploads/files/ceng198 week6.pdf · Do While Loop do-while loop is the same as while loop except that the condition is checked at the end

For Loop (2)

For loop syntax:

for ( initial value assignment ; condition ; update counter )

{

[loop body statements]

}

Page 14: Loops(2) do-while, forceng198.cankaya.edu.tr/uploads/files/ceng198 week6.pdf · Do While Loop do-while loop is the same as while loop except that the condition is checked at the end

Example 4 Write a program to find sum of the integer numbers from 1 to 10.

#include<stdio.h>

void main()

{

int count, sum;

sum = 0;

for( count = 1; count <=10 ; count ++ )

sum = sum+count;

printf(“The sum of positive integer up to 10 is %d\n”,sum);

}

Page 15: Loops(2) do-while, forceng198.cankaya.edu.tr/uploads/files/ceng198 week6.pdf · Do While Loop do-while loop is the same as while loop except that the condition is checked at the end

Example 5 Write a program to read a positive integer and find its factorial.

#include<stdio.h>

void main()

{ int count, num, fact;

fact = 1;

scanf(“%d”,&num);

if( num > 0 )

{

for( count = 1; count <=10 ; count ++ )

fact = fact*count;

printf(“The factorial of %d is %d\n”,num,fact);

}

else

printf(“You entered a non-positive number\n”);

}

Page 16: Loops(2) do-while, forceng198.cankaya.edu.tr/uploads/files/ceng198 week6.pdf · Do While Loop do-while loop is the same as while loop except that the condition is checked at the end

For Loop Without Initialization

You may have no initialization in a for loop.

In this case you can leave the first part empty however, you

need to put the ;

Therefore the syntax of for loop without initialization will

be

for( ; condition ; update counter )

[loop body]

Page 17: Loops(2) do-while, forceng198.cankaya.edu.tr/uploads/files/ceng198 week6.pdf · Do While Loop do-while loop is the same as while loop except that the condition is checked at the end

Example 6

Write a C program to compute the discount offered to a

customer by an insurance company. The company has the

following rules:

A customer gets a discount for each year of being registered with

the company.

For each year, the age of the customer is multiplied by 0.05

The discount is the sum of the discounts given for each year

For example, if the customer registers when he is 30, and his

current age is 34, he gets a discount as below:

Discount = (30*0.05 + 31*0.05 + 32*0.05 + 33*0.05) %

Discount = 6.3 %

Page 18: Loops(2) do-while, forceng198.cankaya.edu.tr/uploads/files/ceng198 week6.pdf · Do While Loop do-while loop is the same as while loop except that the condition is checked at the end

#include<stdio.h>

void main()

{

int startAge, currentAge;

float discount ;

discount = 0;

printf(“Enter the age you started insurance, and your current age: \n”);

scanf(“%d%d”, &startAge, &currentAge);

for( ; startAge < currentAge ; startAge ++)

discount = discount + startAge*0.05;

printf(“The total discount is %f\n”, discount );

}

Note: The initialization part of the loop is empty here

Page 19: Loops(2) do-while, forceng198.cankaya.edu.tr/uploads/files/ceng198 week6.pdf · Do While Loop do-while loop is the same as while loop except that the condition is checked at the end

For Loop without Condition Part

The condition part is optional in for loops

A loop without a condition should still include ; characters to

separate three parts of the loop

for( initialization ; ; update )

[body statements]

A loop without a condition will not terminate. These loops

are called infinite loops

You can terminate a for loop without a condition using

break statement.

Page 20: Loops(2) do-while, forceng198.cankaya.edu.tr/uploads/files/ceng198 week6.pdf · Do While Loop do-while loop is the same as while loop except that the condition is checked at the end

Example 7

Assume 3 people labeled as ‘A’, ‘B’, and ‘C’ are playing a game. The user enters an integer number.

If the number is divisible by 7 ‘A’ gets 1 point.

If the number is divisible by 11 ‘B’ gets 2 point,

If the number is divisible by 13 ‘C’ will get 3 point.

The program ends when -1 is entered.

The score of each player is the total point he gets divided by the number of times user enters a number

Example: with data 3, 6, 7, 11, 33, 21, 39 scores are:

A=2/7

B=4/7

C=3/7

Write a C program to compute the scores in this game

Page 21: Loops(2) do-while, forceng198.cankaya.edu.tr/uploads/files/ceng198 week6.pdf · Do While Loop do-while loop is the same as while loop except that the condition is checked at the end

#include<stdio.h>

void main()

{

float scoreA, scoreB, scoreC;

int nextNum, count ;

scoreA = scoreB = scoreC = 0;

for( count = 0 ; ; count ++ )

{

printf(“Enter next number: \n”);

scanf(“%d”, &nextNum);

if( nextNum == -1 ) break ;

if( nextNum % 7 == 0 )

scoreA = scoreA + 1;

if( nextNum % 11 == 0 )

scoreB = scoreB + 2;

if( nextNum % 13 == 0 )

scoreC = scoreC + 3;

}

scoreA = scoreA / count;

scoreB = scoreB / count;

scoreC = scoreC / count;

printf(“The scores are: A=%f B=%f C=%f\n”, scoreA, scoreB, scoreC );

}

No condition part in for

loop

Page 22: Loops(2) do-while, forceng198.cankaya.edu.tr/uploads/files/ceng198 week6.pdf · Do While Loop do-while loop is the same as while loop except that the condition is checked at the end

For Loop without update counter Part

The update counter part of a for loop is also optional

If you decide not to have update counter part in a for loop you

should:

Update the variable inside the body of the loop, otherwise the

condition will not change and the loop will not terminate.

Include ; even if update part is omitted.

A for loop without initialization and update parts is the

same as a while loop

Page 23: Loops(2) do-while, forceng198.cankaya.edu.tr/uploads/files/ceng198 week6.pdf · Do While Loop do-while loop is the same as while loop except that the condition is checked at the end

Example 8

Assume we want to compare the probability of getting a total of

10 when we roll two dice with the probability of getting a total

of 9. For instance, the number of times you get (4,6) or (5,5)

compared to the probability of getting (3,6) or (4,5), etc.

Write a C program to read two positive integer and find out

their sum. The program should count the number of times that

you get 10, and the number of times you get 9. The program

terminates when you get twenty 10s or 9s.

Hint: Make sure that the numbers are between 1 and 6

Page 24: Loops(2) do-while, forceng198.cankaya.edu.tr/uploads/files/ceng198 week6.pdf · Do While Loop do-while loop is the same as while loop except that the condition is checked at the end

#include<stdio.h>

void main()

{

int dice1, dice2, tens, nines;

nines = tens = 0;

for( ; tens < 20 && nines < 20 ; )

{

printf(“Enter dice numbers \n”);

scanf(“%d%d”, &dice1, &dice2);

if( dice1 < 1 || dice1 > 6 || dice1 < 1 || dice2 > 6)

printf(“ You entered invalid numbers\n”);

else

{

if( dice1 + dice 2 == 10 )

tens++;

if( dice1 + dice 2 == 9 )

nines ++;

}

} /* end of for loop */

if( nines == 20 )

printf(“The total of 9 is more probable\n”);

else

printf(“The total of 10 is more probable\n”);

}

Page 25: Loops(2) do-while, forceng198.cankaya.edu.tr/uploads/files/ceng198 week6.pdf · Do While Loop do-while loop is the same as while loop except that the condition is checked at the end

Summary

If parts of a program are repeated with different values, we can use a loop

Do-While loop repeats executing the statements until the condition becomes false. Do-while is executed at least once

For loops have initialization, condition, and update parts

All parts of a for loop are optional

Page 26: Loops(2) do-while, forceng198.cankaya.edu.tr/uploads/files/ceng198 week6.pdf · Do While Loop do-while loop is the same as while loop except that the condition is checked at the end

Questions?