0% found this document useful (0 votes)
5 views

3 Loops

The document discusses various programming concepts related to loops in C, including finding the greatest common divisor, identifying prime numbers, and calculating factorials. It provides code examples for while loops, for loops, and flag variables, along with tips on choosing the appropriate loop type. Additionally, it includes lab assignments for practicing these concepts.

Uploaded by

cpb977zs9n
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

3 Loops

The document discusses various programming concepts related to loops in C, including finding the greatest common divisor, identifying prime numbers, and calculating factorials. It provides code examples for while loops, for loops, and flag variables, along with tips on choosing the appropriate loop type. Additionally, it includes lab assignments for practicing these concepts.

Uploaded by

cpb977zs9n
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Loops

Weng Kai
2020 Fall
Greatest Common Divisor
• Problem: Find the greatest common divisor of two
positive integers u and v.

• Step I: if v is zero, gcd is u, and done.

• Step II: calculate temp = u%v, u=v, v=temp, and go back


to step I.

• Write a program asks for two integers and print out their
gcd.
#include <stdio.h>
int main()
{
int a, b;
scanf("%d %d", &a, &b);
while ( 1 ) {
if ( b == 0 ) {
break;
}
int r = a%b;
a = b;
b = r;
}
printf("%d\n", a);
}
Opening Question

• PTA W3开课

• Turn it into a while loop (with condition expression in the ()


of while)
Prime Numbers
• A Prime Number is a whole number that cannot be made
by multiplying other whole numbers

• (if we can make it by multiplying other whole numbers it is


a Composite Number)

• And 1 is not prime and also not composite.


For Loop
#include <stdio.h>
int main()
{
int x;
scanf("%d", &x);
for ( int i =2; i<x; i++ ) {
if ( x%i == 0 ) {
break;
}
}
printf("%d is prime.\n", x);
}
Flag Variable
#include <stdio.h>
int main()
{
int x;
scanf("%d", &x);
int isprime = 1;
for ( int i =2; i<x; i++ ) {
if ( x%i == 0 ) {
isprime = 0;
break;
}
}
if ( isprime ) {
printf("%d is prime.\n", x);
} else {
printf("%d is not prime.\n", x);
}
}
For Loop
for loop acts like a counter: you name a counter,
initialize it at the beginning, repeat to do what inside
the loop before the counter reaches a specified value
for ( i=0; i<5; i=i+1 ) {
printf("%d", i);
}

• for ( init ; condition; step ) {

}
For

• for ( count=10; count>0; count-- )

• It reads: “For count=10, when count>0, repeat the loop


body. And count decreases every cycle.”
for == while

==
For Loop

for ( init; condition; cyclic action ) {

• each and every expression in for is omissible

for (; cond; ) == while ( con )


Cycles For a Loop

• for ( i=0; i<n; i++ )

• 则循环的次数是n,⽽循环结束以后,i的值是n。循环的控
制变量i,是选择从0开始还是从1开始,是判断i<n还是判断
i<=n,对循环的次数,循环结束后变量的值都有影响
variants of for loops

• multiple expressions

for ( i=0, j=0; i<10; ++i )

• omitting some fields

for ( ; j != 100; ++j )


Define Var. in for

• If the “counter” i is used inside the loop only, its definition


could be inside.
for ( int i =2; i<x; i++ ) {

• But it is in that for loop only and not available after the
loop.
Factorial
• n! = 1×2×3×4×…×n

• Write program reads n and outputs n!

• variable:

• n for user input

• factorial for result

• some number changes from 1 to n, like i as int


Atoms

• The result variable should be initialized to zero for a sum-


up program. But it should be one for a product program.
Lab 1

• Write your code to calculate factorial.

• Using int, what is the maxim n! your code can get?

• How do you figure out the maxim n?


int Has a Limited Range
Three Loops
Tips to Choose Loops

• for: determined times • determined times—>for

• do_while: at least once • while w/ break first

• otherwise: while • turn that while into while


or do-while
Prime Numbers
• Write a program to print prime numbers below 100.
for (int x = 2; x<100; x++ ) {
int isprime = 1;
for ( int i =2; i<x; i++ ) {
if ( x%i == 0 ) {
isprime = 0;
break;
}
}
if ( isprime ) {
Nested Loops

• Loop inside loop

• Do NOT mess up the two (or more) variables in different


loops
Lab 2

• PTA W2课内7-2

• Write a program to print out the first 50 prime numbers.


Coins

• 如何⽤1⻆、2⻆和5⻆的硬币凑出10元以下的⾦额呢?
Break

• is for the loop they are in


break by break
goto
Rules to goto

• In two situation goto is allowed:

• To jump out of multiple loops

• To jump to the end of a function


Lab 3

• PTA W3课内7-3

You might also like