Lecture 4: Loops and Scope
Frequently we want a program to keep doing the same thing over and over again until something happens. That's called looping. This lecture focuses on the primary loop styles in c along with a brief discussion on scope.
Scope
A scope in a program is a region of the program where a defined variable can have its existence and beyond that variable it cannot be accessed. There are three places where variables can be declared in C programming language
- Inside a function or a block which is called local variables.
- Outside of all functions which is called global variables.
- In the definition of function parameters which are called formal parameters.
Local variables
Variables that are declared inside a function or block are called local variables. They can be used only by statements that are inside that function or block of code. Local variables are not known to functions outside their own. The following example shows how local variables are used. Here all the variables a, b, and c are local to main() function.
#include <stdio.h>
int main () {
//local declaration
int a, b;
int c;
//initialization
a = 10;
b = 20;
c = a + b;
printf ("value of a = %d, b = %d and c = %d\n", a, b, c);
return 0;
}
Global Variables
Global variables are defined outside a function, usually on top of the program. Global variables hold their values throughout the lifetime of your program and they can be accessed inside any of the functions defined for the program.
A global variable can be accessed by any function. That is, a global variable is available for use throughout your entire program after its declaration. The following program show how global variables are used in a program.
#include <stdio.h>
int g;
int main () {
//local declaration
int a, b;
//initialization
a = 10;
b = 20;
g = a + b;
printf ("value of a = %d, b = %d and g = %d\n", a, b, g);
return 0;
}
A program can have same name for local and global variables but the value of local variable inside a function will take preference. Here is an example:
#include <stdio.h>
int g = 20;
int main () {
int g = 10;
printf ("value of g = %d\n", g);
return 0;
}
When the above code is compiled and executed, it produces the following result
value of g = 10
Formal Parameters
Formal parameters, are treated as local variables with-in a function and they take precedence over global variables. Following is an example
#include <stdio.h>
int a = 20;
int main () {
int a = 10;
int b = 20;
int c = 0;
printf ("value of a in main() = %d\n", a);
c = sum( a, b);
printf ("value of c in main() = %d\n", c);
return 0;
}
// function to add two integers
int sum(int a, int b) {
printf ("value of a in sum() = %d\n", a);
printf ("value of b in sum() = %d\n", b);
return a + b;
}
Loops
There are three primary types of loops in C. The while loop, the do while loop, and the for loop. Here we briefly discuss all three types.
while loop
The while loop
while(CONDITION) {
//code to repeat
}
To execute this kind of statement, the computer first evaluates the condition, which is just an expression. If the condition is true, it executes the statement. If the condition is false, it skips the whole thing and goes on to the next statement.
while loop condition checking is similar to the if statement. The difference is when the computer is done executing the statement in the body of an if, it goes on to the next statement. When the computer is done executing the statement in the body of a while, it goes back and repeats the whole statement. The computer will test the condition and execute the statement over and over, until finally when the condition is false, it gets to go on to the next statement.
Sample code to print out the numbers 1 to 10.
int foo = 1;
while(foo <= 10) {
printf("%d, ", foo++);
}
This outputs the following:
1 2 3 4 5 6 7 8 9 10
The loop continues until the value of foo
reaches 11. At this point, foo is >10 and the loop breaks. The program then proceeds as normal from the end of the while loop.
Here's another example. This time the condition of the while statement is a compound statement:
int foo = 0;
while(foo < 1 || foo > 10) {
printf("Please enter an integer between 1 and 10.\n");
scanf("%d", &foo);
}
This program will keep prompting the user and reading an integer until we
get one between 1 and 10. Note that foo
is initialized to 0; if it
were initialized to 7 instead for some reason, we'd never prompt at
all, because the condition would be false the first time we got to the
while statement.
That concludes while loops. Prior to our discussion of other types of loops, lets first discuss variable scope.
do ... while
There's another loop construct like while, which isn't as often used, but which might have been better for the example above. It looks like this:
do {
//statements
} while(CONDITION);
The difference is that the condition is tested after the statement is executed, instead of before. That means that the statement is always executed at least once. For example:
int foo = 7;
do {
printf("Please enter an integer between 1 and 10.\n");
scanf("%d", &foo);
} while(foo < 1 || foo > 10);
Now it doesn't matter that foo is initialized to 7, because we prompt and ask for a number from the user at least once anyway, and the 7 is obliterated by the call to scanf() before we ever look at the value of foo.
Use while when it might be appropriate for the actions in the body to never happen at all; use do ... while when you always want the actions to be executed at least once.
for
This code exemplifies the notion that every loop has four logical parts:
int x = 1;
while (x <= 50) {
printf("%d ", x);
x++;
}
There's an initialization to set up the variable or variables that form the main control of the loop; that's the x = 1. There's a control expression that says how long to run the loop and when to stop; that's x <= 50 here. There's a control update, which updates the values the main control variables; that's x++ here. Finally, there's a loop body, which contains the statements that the control expression actually controls; that's the printf().
There's a loop construct that makes all four parts explicit. It looks like this:
for (initialization; control; update) {
//loop body
}
this is equivalent to
//initialization
while(control) {
//loop body
//update
}