Professional Documents
Culture Documents
Computer Programming
Lecture 2:
Control Statements in C
int main()
{
False if (0)
{
// this block will never execute
printf("Always false\n");
}
if (1)
{
// this block will always execute
printf("Always true\n");
}
if (5)
True
{
// this block will always execute
printf("Always true\n");
}
if (-5)
{
// this block will always execute
printf("Always true\n");
}
return 0;
}
#include<stdio.h>
int main()
if statement
{
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
if ( a > b )
printf("a "); // this is part of the if
printf(" is greater"); // this is not part of the if
printf("\n");
return 0;
}
#include<stdio.h>
int main()
{
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
if ( a > b )
{
// everything inside this { } is part of the if
printf("a is greater\n");
}
return 0;
}
if statement
#include<stdio.h>
• Common programming error:
• Placing ; (semicolon) int main()
{
immediately after condition in int a, b;
if printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
• if(expression); statement; if ( a = b )
• Confusing equality operator {
printf("a equals b\n");
(==) with assignment operator }
(=) // try this program with
// a = 10 b = 0
• if(a=b) // a = 10 b = 10
• if(a=5) // a = 10 b = 5
// a = 10 b = -10
• if(9=5) // In C, 0 means false, anything else
• left operand must be l-value is true
return 0;
• if(9+5) }
if-else statement
• if(expression) statement1;
else statement2;
• If expression is true tru num is
statement1 will be evaluated num>=0
e positive
and statement2 will be
skipped fals
• If expression is false e num is
statement1 will be bypassed negative
and statement2 will be
executed
• Under no circumstances both
the statements will execute
• Two-way decision path
if-else statement
Checking the sign of a number Checking a number for odd/even
#include<stdio.h> #include<stdio.h>
if ( num >= 0 && num < 60) if (num >= 90 ) if (num < 60 )
{ { {
grade = 'F'; grade = 'A'; grade = 'F';
} } }
if ( num >= 60 && num < 70) else if ( num >= 80) else if ( num < 70)
{ { {
grade = 'D'; grade = 'B'; grade = 'D';
} } }
if ( num >= 70 && num < 80) else if (num >= 70) else if (num < 80)
{ { {
grade = 'C'; grade = 'C'; grade = 'C';
} } }
if ( num >= 80 && num < 90) else if (num >= 60) else if (num < 90)
{ { {
grade = 'B'; grade = 'D'; grade = 'B';
} } }
if ( num >= 90 && num <= 100) else else
{ { {
grade = 'A'; grade = 'F'; grade = 'A';
} } }
printf(“Grade: %c\n", grade); printf(“Grade: %c\n", grade); printf(“Grade: %c\n", grade);
return 0; return 0; return 0;
} } }
if-else if statement
#include<stdio.h>
Case conversion of an input
character int main( )
{
char ch;
printf("Enter a character: ");
scanf("%c", &ch);
if( ch>='A' && ch<='Z' )
{
printf("%c\n", (ch - 'A') + 'a');
}
else if( ch>='a' && ch<='z' )
{
printf("%c\n", (ch - 'a') + 'A');
}
else
{
printf("Invalid Character\n");
}
return 0;
}
Example
• Find maximum of three numbers
• Find second maximum of three numbers
• Find minimum of four numbers
Find maximum of three numbers
Code 1 Code 2 Code 3 Code 4
if (a > b) if ( a >= b && a >= c) if ( a >= c && a >= b) max = a;
{ { { if ( b > max)
if (a > c) max = a; max = a; {
{ } } max=b;
max = a; if ( b >= a && b >= c) else if (b >= c) }
} { { if ( b > max)
else max = b; max = b; {
{ } } max=c;
max = c; if ( c >= a && c >= b) else }
} { {
} max = c; max = c;
else } }
{
if (b > c)
{
max = b;
}
else
{
max = c;
}
}
switch case
switch (expression) {
case constant: statements
case constant: statements
default: statements
}
• Use of break
switch case
Determine whether a #include<stdio.h>
number is odd or even
int main( )
{
int a;
printf("Enter a number: ");
scanf("%d", &a);
switch(a%2)
{
case 0:
printf("%d is even\n", a);
break;
case 1:
printf("%d is odd\n", a);
break;
}
return 0;
}
switch case properties
The expression used in switch must be
integral type ( int, char and enum). Any
other type of expression is not allowed.
switch case properties(2)
All the statements following a matching case
execute until a break statement is reached.
switch case properties(3)
The default block can be placed anywhere.
The position of default doesn’t matter, it is
still executed if no match found.
switch case properties(4)
#include<stdio.h> #include<stdio.h>
int main() int main()
{ {
int a,b,max; int a,b,max;
scanf("%d%d",&a,&b); scanf("%d%d",&a,&b);
if(a>b) max=a>b?a:b;
max=a; printf("Max = %d",max);
else return 0;
max=b; }
printf("Max = %d",max);
return 0;
}
Conditional Expressions
Maximum of #include<stdio.h>
three numbers
int main( )
{
int a, b, c;
int max;
printf("Enter three numbers: ");
scanf("%d%d%d", &a, &b, &c);
max = a > b ? ( a > c ? a : c) : ( b > c ? b : c);
printf("Max: %d\n", max);
return 0;
}
Conditional Expressions
Case #include<stdio.h>
Conversion
int main( )
{
char ch;
char x;
printf("Enter a character: ");
scanf("%c", &ch);
x = (ch>='A' && ch<='Z') ? ch+('a'-'A') :
( (ch>='a' && ch<='z') ? ch-('a'-'A') : ch );
printf("%c\n", x);
return 0;
}
Control Statements
Loops
for, while and do-while
for loop
• Allows one or more statements to be repeated
• for (initialization; conditional-test; increment) statement;
• Most flexible loop
for loop
for (initialization; conditional-test; increment) statement;
initialization: conditional-test:
• Give an initial value to the • Tests the loop-control variable against a
variable that controls the loop target value
• loop-control variable • If true the loop repeats
• Executed only once • statement is executed
• Before the loop begins • If false the loop stops
• Next line of code following the loop
increment: will be executed
Executed at the bottom of the loop
for loop
Initialization part is
for(i=1; i<3; i++) executed only once
printf("%d\n", i);
1. i is initialized to 1
for loop
for(i=1; i<3; i++)
printf("%d\n", i);
2. Conditional test i<3 is true as i is 1, so the loop executes
for loop
for(i=1; i<3; i++)
printf("%d\n", i);
3. The value of i will be printed, which is 1
for loop
for(i=1; i<3; i++)
printf("%d\n", i);
3. The value of i will be incremented, so now i is 2.
for loop
for(i=1; i<3; i++)
printf("%d\n", i);
4. Conditional test i<3 is true as i is 2, so the loop executes
for loop
for(i=1; i<3; i++)
printf("%d\n", i);
5. The value of i will be printed, which is 2
for loop
for(i=1; i<3; i++)
printf("%d\n", i);
6. The value of i will be incremented, so now i is 3.
for loop
for(i=1; i<3; i++)
printf("%d\n", i);
7. Conditional test i<3 is false as i is 3, so the loop stops
for loop
int main(){
int i,j;
for(i=1;i<=3;i++){
for(j=1;j<=i;j++){
printf("%d, %d\n",i, j);
}
}
return 0;
}
Nested For Loop Practice Problem #include<stdio.h>
int main() {
List of perfect numbers in a range int i, j, n, s;
printf("Enter n: ");
Input Output scanf("%d", &n);
10 6 printf("List of Perfect Numbers: ");
for ( i = 1; i <=n; i++) {
30 6 28
s = 0;
500 6 28 496 for ( j = 1; j< i; j++) {
if ( i % j == 0) {
s = s + j;
}
}
if ( s == i) {
printf("%d ", i);
}
}
printf("\n");
return 0;
}
Nested For Loop Practice Problem #include<stdio.h>
int main() {
List of prime numbers in a range int i, j, n, isPrime;
printf("Enter n: ");
Input Output scanf("%d", &n);
10 2357 printf("List of Prime Numbers: ");
for ( i = 2; i <=n; i++) {
12 2 3 5 7 11 isPrime = 1;
19 2 3 5 7 11 13 17 19 for ( j = 2; j * j <= i; j++) {
if ( i % j == 0) {
isPrime = 0;
break;
}
}
if ( isPrime == 1) {
printf("%d ", i);
}
}
printf("\n");
return 0;
}
#include<stdio.h>
Nested For Loop Practice Problem
int main() {
int i, j, n, isPrime;
List of prime factors of a number printf("Enter n: ");
scanf("%d", &n);
Input Output printf("List of Prime Factors: ");
10 25 for ( i = 2; i <=n; i++) {
if (n%i == 0) {
24 23
isPrime = 1;
70 257 for ( j = 2; j * j <= i; j++) {
if ( i % j == 0) {
isPrime = 0;
break;
}
}
if ( isPrime == 1) {
printf("%d ", i);
}
}
}
printf("\n");
return 0;
}
Nested for loop
Nested for loop
Use of continue
Practice Problems
• Find sum of series such as
• 1+2N+3N+4N+5N+…+NN
• 1+22+33+44+55+…+NN
• Sine, Cos series
• Write a program to find xm where x and m are inputs
• Write a program to convert a decimal number to a binary number
• Write a program to expand shorthand notation like a-z
Practice Problems
• Print the following patterns for a given N
int main() {
int n;
int i, j;
printf("Enter total lines : ");
scanf("%d", &n);
for ( i = 1; i <=n; i++) {
for ( j = 1; j <= 10; j++) {
printf(" * ");
}
printf("\n");
}
return 0;
}
Practice Problems
#include<stdio.h>
int main() {
int n;
int i, j;
printf("Enter total lines : ");
scanf("%d", &n);
for ( i = 1; i <=n; i++) {
for ( j = 1; j <= i; j++) {
printf(" * ");
}
printf("\n");
}
return 0;
}
Practice Problems
#include<stdio.h>
int main() {
int n;
int i, j;
printf("Enter total lines : ");
scanf("%d", &n);
for ( i = 1; i <=n; i++) {
for ( j = 1; j <= (n-i); j++) {
printf(" ");
}
for ( j = 1; j <= i; j++) {
printf(" * ");
}
printf("\n");
}
return 0;
}
Practice Problems
#include<stdio.h>
int main() {
int n;
int i, j;
printf("Enter total lines : ");
scanf("%d", &n);
for ( i = 1; i <=n; i++) {
for ( j = 1; j <= (n-i); j++) {
printf(" ");
}
for ( j = 1; j <= 2*i-1; j++) {
printf(" * ");
}
printf("\n");
}
return 0;
}
Practice Problems
#include<stdio.h>
int main() {
int n;
int i, j;
printf("Enter total lines : ");
scanf("%d", &n);
for ( i = n; i >= 1; i--) {
for ( j = 1; j <= (n-i); j++) {
printf(" ");
}
for ( j = 1; j <= 2*i-1; j++) {
printf(" * ");
}
printf("\n");
}
return 0;
}
int main() {
Practice Problems
int n;
int i, j;
printf("Enter total lines : ");
scanf("%d", &n);
for ( i = 1; i <n; i++) {
for ( j = 1; j <= (n-i); j++) {
printf(" ");
}
for ( j = 1; j <= 2*i-1; j++) {
printf(" * ");
}
printf("\n");
}
for (; i>=1; i--) {
for ( j = 1; j <= (n-i); j++) {
printf(" ");
}
for ( j = 1; j <= 2*i-1; j++) {
printf(" * ");
}
printf("\n");
}
return 0;
}
while loop
• Syntax for(initialization; conditional-test; increment) statement;
while(expression)
{
statement1;
statement2; initialization;
…. while(conditional-test)
} {
statement;
increment;
};
while loop
for while
while loop
Series Sum Factorial
#include<stdio.h> #include<stdio.h>