CPNM Lecture 4 - C Control Structures
CPNM Lecture 4 - C Control Structures
Jadavpur University
2024
C Control Statements
1. Three categories
▶ Selection statements - allow a program to select a particular
execution path, ex: if, switch
▶ Iteration statements - support iterations, ex: while, do,
for
▶ Jump statements - cause an unconditional jump to some
other place in the program, ex: break, continue, goto
if Statement I
▶ Allows a program to choose between two alternatives by
testing the value of an expression
if ( expression ) statement
▶ A nonzero value of the expression is treated as true
▶ Compound Statements: a group of statements within braces
if ( expression ) { statements }
▶ The else clause
if ( expression ) { statements } else {
statements }
▶ Example
if (i > j )
max = i;
else
max = j;
if Statement II
▶ Nested if
if (i > j) {
if (i > k)
max = i;
else
max = k;
}
else{
if(j > k)
max = j;
else
max = k;
}
if (n < 0)
printf("n is less than 0\n");
else
if (n == 0)
printf("n is equal to 0\n") ;
else
printf("n is greater than 0\n");
Iteration Statements I
▶ C’s iteration statements allows us to set up loops
▶ A loop is a statement whose job is to repeatedly execute some
other statement (the loop body)
▶ Every loop has a controlling expression. Each time the loop
body is executed (an iteration of the loop), the controlling
expression is evaluated; if the expression is true - has a value
that’s not zero - the loop continues to execute
▶ C provides three iteration statements: while, do, for
▶ The while statement is used for loops whose controlling
expression is tested before the loop body is executed
▶ The do statement is used if the expression is tested after the
loop body is executed
▶ The for statement is convenient for loops that increment or
decrement a counting variable
The while Statement I
i = 1;
while(i <= 5){ /* Controlling expression */
printf("Hello\n"); /* Loop body */
i++;
}
while(1){
printf("Hello\n");
...
}
The while Statement III
▶ Example: Printing a table of squares
#include <stdio.h>
int main (void){
int i, n;
printf ("This program prints a table of squares: \n");
printf ("Enter number of entries in table: ");
scanf ("%d", &n);
i = 1;
while (i <= n){
printf("%lOd%10d\n", i , i*i);
i++;
}
return 0;
}
The while Statement IV
#include <stdio.h>
#include <stdio.h>
int main(void){
int digits=0, n;
printf("Enter a nonnegative integer: ");
scanf("%d", &n);
do {
n = n / 10;
digits++;
} while (n > 0);
printf("The number has %d digit(s) \n", digits);
return 0;
}
The do Statement III
while(n>0){
n = n / 10;
digits++;
}
expr1;
while(expr2){
statement;
expr3;
}
i = 10;
for (;i > 0; i--){
printf("Hello\n");
}
n = 0;
sum = 0:
while (n < 10) {
scanf("%d", &i);
if (i == 0 )
continue;
sum += i:
n++;
/* continue jumps here */
}
The goto Statement
▶ goto statement allows jumping to any statement in a
function, provided that the statement has a label
▶ A label is just an identifier placed at the beginning of a
statement:
identifier: statement
goto identifier;
▶ Example:
{
...
goto end_of_stmt;
...
end_of_stmt: ;
}
The switch Statement I
▶ Used when we need to compare an expression against a series
of values to see which one it currently matches
▶ Format
switch ( expression ) {
case constant-expression : statements
...
case constant-expression : statements
default: statements
}
switch(grade){
case 4: printf("Excellent"); break;
case 3: printf("Good"); break;
case 2: printf("Average"); break;
case 1: printf("poor"); break;
case 0: printf("Failing"); break;
default: printf("Illegal grade"); break;
}
switch(day){
case 1: case 21: case 31:
printf ("st"); break;
case 2: case 22:
printf("nd"); break;
case 3: case 23:
printf("rd"); break;
default: printf("th"); break;
}
printf(" day of ");
The Switch Statement - Example II
switch(month){
case 1: printf ("January"); break;
case 2: printf("February"); break;
case 3: printf("March"); break;
case 4: printf("April"); break;
case 5: printf("May"); break;
case 6: printf("June"); break;
case 7: printf("July"); break;
case 8: printf("August"); break;
case 9: printf("September"); break;
case 10: printf("October"); break;
case 11: printf("November"); break;
case 12: printf("December"); break;
}
printf(", 20%.2d.\n", year);
return 0;
}
Case Ranges - C Language Extension
▶ GNU C provides several language features not found in ISO standard C
▶ It supports, as a language extension, case ranges
▶ The -pedantic option directs GCC to print a warning message if any of these
features is used; Be careful: write spaces around the ...
main(){
int data[10] = { 5, 4, 10, 25, 60, 47, 23, 80, 14, 11}, i;
for(i = 0; i < 10; i++) {
switch (data[i]) {
case 1 ... 10:
printf("%d in range 1 to 10\n", data[i]);
break;
case 11 ... 20:
printf("%d in range 11 to 20\n", data[i]);
break;
case 21 ... 30:
printf("%d in range 21 to 30\n", data[i]);
break;
case 31 ... 40:
printf("%d in range 31 to 40\n", data[i]);
break;
default:
printf("%d Exceeds the range\n", data[i]);
break;
}
}
}