Explanation of error: loop does not terminate because the assignment n = n + i
results in n growing faster than i.
inti=0;while(i<10)printf("%d\n",i);i=i+1;
Explanation of error: missing braces mean the while loop code is only the first
code statement after the while. This means the update statement, i = i + 1,
is not part of the loop regardless of the indentation, and the loop is an infinite
loop.
In groups, discuss various ways this while loop could fail.
inti=0;intj=0;while(i<10&&j>-10){printf("i is %d and j is %d\n",i,j);i=i+1;j=j-1;}
There are a few ways to break this while loop:
What if we didn't initialize i or j? What if they were initialized with different values?
What if we swapped the comparison operator for both i and j in the while condition?
What if i decreased instead of increased, and vice versa for j?
Write a C program count_up.c which reads an integer n
and then prints the integers 1..n, one per line.
How could we build on our code from count_up.c to use a scanned increment value?
(Note: 'increment' means "how much to increase our number by each time the loop runs")
dcc -o count_inc count_inc.c./count_inc
Enter finish: 3
Enter increment: 2
1
3
./count_inc
Enter finish: 17
Enter increment: 5
1
6
11
16
Write a C program range7.c which reads 2 integers n and
m, and then prints the integers between n and m (including
n and m) which are divisible by 7.
Hint: if x is divisible by 7, then x % 7 == 0
dcc -o range7 range7.c./range7
Enter start: 3
Enter finish: 49
7
14
21
28
35
42
49
Two-Dimensional Loops
Write a program that reads in an integer and prints out that many
asterisks, each on a new line.
./asterisks
Please enter an integer: 5
*
*
*
*
*
How would you convert it to printing out a square rather than a line?
The square program written above might behave like this:
./square
Enter size: 5
*****
*****
*****
*****
*****
Modify the program so that it prints out a triangle like this:
./triangle
Enter number: 5
*----
**---
***--
****-
*****
Now modify so it prints the following pattern:
./bars
Enter an integer: 9
-*-*-*-*-
-*-*-*-*-
-*-*-*-*-
-*-*-*-*-
-*-*-*-*-
-*-*-*-*-
-*-*-*-*-
-*-*-*-*-
-*-*-*-*-
Debugging
This program 'won't work'. What's wrong with it? Split off into groups and discuss why. Line numbers are included for reference.
1#include<stdio.h>23intmain(void){4printf("Counting the number of minutes in a number of hours.");5printf("How many hours: ");67inta;8scanf("%d",$a);910intb;11intc=0;12intd=0;13chartotal_minutes=0;1415while(b<a){16while@c<60@{17}18total_minutes=total_minutes+1;19a=a+1;20}2122printf("There are %c minutes in %d hours.\n",total_minutes,a);2324return1;25
Part 1: While Loops (15 mins)
In this short section, we will look at some while loops, and understand the four main components
of a while loop.
You will use these eight programs during the tutorial:
In this section we will learn more about variable names.
You should remember the following rules about Legal variable names in C:
Variable names can contain letters, numbers, or _.
Variable names must not start with a number.
You should also remember these rules about variable names which are Good Style in C:
Variable names should always start with a lowercase letter.
Variable names should always use snake_case
#defines names must be in SHOUTING_SNAKE_CASE.
Variable names can still be a poor name, even if they follow the style guide.
Variables should be named descriptively, and in a way which is relevant to
the program.