Section 3 - 4 Program Contral Statements
Section 3 - 4 Program Contral Statements
Decision making and branching. if and if-else statements, other control statements, switch and the ‘?:’ operator,
Decision making and looping. While looping, Do-while and for looping statements, Jump statement. goto, break
and continue.
Page 1
Course Title: Computer Fundamentals and Programming Course Code: CCE-1105
1. Conditional control statement: if, if-else, if-else-if ladder, nested if, switch
2. Loop control statement: for, while, do-while
3. Jump statement: break, continue, goto
C if else Statement
The if-else statement in C is used to perform the operations based on some specific condition. The
operations specified in if block are executed if and only if the given condition is true.
1. If statement
2. If-else statement
3. If-else-if ladder
4. Nested if statement
1. If Statement
The if statement is used to check some given condition and perform some operations depending upon the
correctness of that condition. It is mostly used in the scenario where we need to perform the different
operations for the different conditions. The syntax of the if statement is given below.
if(expression)
{
//code to be executed
}
Flowchart of if statement in C
Page 2
Course Title: Computer Fundamentals and Programming Course Code: CCE-1105
Example 1: Write a C program that take an integer number from the keyboard and check whether the input
is positive using if statement.
Example 2: Write a C program that take Three integer numbers from keyboard and find out the largest
number of the three using if statement.
#include <stdio.h>
int main()
{
int a, b, c;
printf("Enter three numbers:");
scanf("%d %d %d", &a, &b, &c);
if(a>b && a>c)
{
printf("%d is largest", a);
}
if(b>a && b > c) How to find out the smallest number
{ of the three using if statement?
printf("%d is largest", b);
}
if(c>a && c>b)
{
printf("%d is largest", c);
} Output
if(a == b && a == c) Enter three numbers?
{ 12 23 34
printf("All are equal"); 34 is largest
}
}
Page 3
Course Title: Computer Fundamentals and Programming Course Code: CCE-1105
Exercise: if statement
1. Write a C program that take your age from keyboard and find out whether you are eligible to vote in the
national election using if statement.
2. Write a C program that take your age from keyboard and find out whether you are not eligible to vote in the
national election using if statement.
3. Write a C program that take Two integer numbers from keyboard and find out the largest number of the
two using if statement.
4. Write a C program that take Two integer numbers from keyboard and find out the smallest number of the
two using if statement.
5. Write a C program that take an integer number from keyboard and find out whether the number is
EVEN number using if statement.
6. Write a C program that take an integer number from keyboard and find out whether the number is ODD
number using if statement.
7. Write a C program that enable you to enter your Final marks of CCE-1201 course from keyboard and find out
whether you pass this course using if statement.
8. Write a C program that enable you to enter your Final marks of CCE-1201 course from keyboard and find out
whether you fail this course using if statement.
9. Write a C program that enable you to enter your Final marks of CCE-1201 course from keyboard and find out
whether you have achieved A+ in this course using if statement.
10. Write a C program that enable you to enter your Final marks of CCE-1201 course from keyboard and find
out whether you have not achieved A+ in this course using if statement.
11. Write a C program that take an integer number (year) from keyboard and find out whether year is leap
year using if statement.
12. Write a C program that take a character/letter (English Alphabet) from keyboard and check whether letter is
Capital using if statement.
13. Write a C program that take a character/letter (English Alphabet) from keyboard and check whether letter is
Small using if statement.
14. Write a C program that take a character/letter (English Alphabet) from keyboard and check whether letter is
Vowel using if statement.
15. Write a C program that take a character/letter (English Alphabet) from keyboard and check whether letter is
Consonant using if statement.
Page 4
Course Title: Computer Fundamentals and Programming Course Code: CCE-1105
2. If-else Statement
The if-else statement is used to perform two operations for a single condition. The if-else statement is an
extension to the if statement using which, we can perform two different operations, i.e., one is for the
correctness of that condition, and the other is for the incorrectness of the condition. Here, we must notice that if
and else block cannot be executed simultaneously. Using if-else statement is always preferable since it always
invokes an otherwise case with every if condition. The syntax of the if-else statement is given below.
if(expression){
//code to be executed if condition is true
}else{
//code to be executed if condition is false
}
Example 1: Write a C program that take an integer number from the keyboard and check whether the input is
positive or negative using if-else statement.
#include<stdio.h>
main()
{
int number;
printf("Enter an integer number:
"); scanf("%d", &number);
Output
if(number>=0)
{ Enter an in integer number: 7
printf("Given number is positive"); Given number is positive
}
else
{
printf("Given number is negative");
}
}
Page 5
Course Title: Computer Fundamentals and Programming Course Code: CCE-1105
Example 2: Write a C program that take your age from keyboard and find out whether you are eligible to
vote or not in the national election using if-else statement.
#include <stdio.h>
int main()
{
int age;
printf("Enter your age: ");
scanf("%d", &age);
if(age>=18)
{
printf("You are eligible to vote...");
}
else
{
printf("Sorry ... you can't vote");
} Output
return 0; Enter your age: 19
} You are eligible to vote...
1. Write a C program that take Two integer numbers from keyboard and find out the largest number of the
two using if-else statement.
2. Write a C program that take an integer number from keyboard and find out whether the number is EVEN or
ODD number using if-else statement.
3. Write a C program that enable you to enter your Final marks of CCE-1201 course from keyboard and find out
whether you pass or fail in this course using if-else statement.
4. Write a C program that enable you to enter your Final marks of CCE-1201 course from keyboard and find out
whether you have achieved A+ or not in this course using if-else statement.
5. Write a C program that take an integer number (year) from keyboard and find out whether year is leap year or
not using if-else statement.
6. Write a C program that take a character/letter (English Alphabet) from keyboard and check whether letter is
Capital or not using if-else statement.
7. Write a C program that take a character/letter (English Alphabet) from keyboard and check whether letter is
Small or not using if-else statement.
8. Write a C program that take a character/letter (English Alphabet) from keyboard and check whether letter is
Vowel or not using if-else statement.
9. Write a C program that take a character/letter (English Alphabet) from keyboard and check whether letter is
Consonant or not using if-else statement.
Page 6
Course Title: Computer Fundamentals and Programming Course Code: CCE-1105
if(condition1)
{
//code to be executed if condition1 is true
}
else if(condition2)
{
//code to be executed if condition2 is true
}
...
...
else
{
//code to be executed if all the conditions are false
}
Page 7
Course Title: Computer Fundamentals and Programming Course Code: CCE-1105
Example 1: Write a C program that take an integer number from the keyboard and check whether the input is
zero, positive or negative using if-else-if statement.
#include<stdio.h>
main()
{
int number;
printf("Enter an integer number: ");
scanf("%d", &number);
if(number == 0)
{
printf("Given number is equal to Zero");
}
else if(number > 0)
{
printf("Given number is Positive");
}
else
{ Output
printf("Given number is Negative"); Enter an in integer number: -7
}
} Given number is Negative
Example 2: Write a C program that take your final mark of CCE-1201 from the keyboard and calculate the
grade according to the your specified mark using if-else-if ladder
#include <stdio.h>
main()
{
int mark;
printf("Enter your mark in CCE-1201: ");
scanf("%d", &mark);
Page 8
Course Title: Computer Fundamentals and Programming Course Code: CCE-1105
{
printf("You scored grade B+ ...");
}
else if (marks >=60 && marks =< 64)
{
printf("You scored grade B ...");
}
else if (marks >=55 && marks =< 59)
{
printf("You scored grade B- ...");
}
else if (marks >=50 && marks =< 54)
{
printf("You scored grade C+ ...");
}
else if (marks >=45 && marks =< 49)
{
printf("You scored grade C ...");
}
else if (marks >=40 && marks =< 44)
{
printf("You scored grade D ..."); Output
}
Enter your mark in CCE-1201: 95
Congrats ! you scored grade A+ ...
else
{
printf("Sorry you have failed in CCE-1201...See You Next Time!!");
}
}
1. Write a C program that take Three integer numbers from keyboard and find out the largest number of
the three using if-else-if statement.
2. Write a C program that take Three integer numbers from keyboard and find out the smallest number of
the three using if-else-if statement.
3. Write a C program that enable you to enter your Final marks of CCE-1201 course from keyboard and find out
whether you got A+ or D or fail in this course using if-else-if statement.
4. Write a C program that take an integer number (year) from keyboard and find out whether year is leap year or
not using if-else-if statement.
5. Write a C program that take a character/letter (English Alphabet) from keyboard and check whether letter is
Capital or Small using if-else-if statement.
6. Write a C program that take a character/letter (English Alphabet) from keyboard and check whether letter is
Vowel or Consonant using if-else-if statement.
Page 9
Course Title: Computer Fundamentals and Programming Course Code: CCE-1105
4. Nested if statements
Nested if-else statements means you can use one if or else if statement inside another if or else if
statement(s). The syntax for a nested if statement is as follows −
if( boolean_expression 1)
{
/* Executes when the boolean expression 1 is true */
if(boolean_expression 2)
{
/* Executes when the boolean expression 2 is true */
}
}
You can nest else if...else in the similar way as you have nested if statements.
Example: Write a C program that take an integer number from the keyboard and check whether the input
is zero, positive or negative using nested if statement.
#include<stdio.h>
main()
{
int number;
printf("Enter an integer number: ");
scanf("%d", &number);
Output
if(number >= 0)
{ Enter an in integer number: -7
Given number is Negative
if(number > 0)
{
printf("Given number is Positive");
}
else
{
printf("The number is equal to Zero");
}
else
{
printf("Given number is Negative");
}
}
1. Write a C program that take Two integer numbers (suppose x and y) from keyboard and Check whether x
is equal to y or x is greater than y or x is less than y using nested if statement.
Page 10
Course Title: Computer Fundamentals and Programming Course Code: CCE-1105
C Switch Statement
The switch statement in C is an alternate to if-else-if ladder statement which allows us to execute multiple
operations for the different possible values of a single variable called switch variable. Here, we can
define various statements in the multiple cases for the different values of a single variable.
switch(expression){
case value1:
//code to be executed;
break; //optional
case value2:
//code to be executed;
break; //optional
......
default:
code to be executed if all cases are not matched;
}
3) The case value can be used only inside the switch statement.
4) The break statement in switch case is not must. It is optional. If there is no break statement found in the
case, all the cases will be executed present after the matched case. It is known as fall through the state of C
switch statement.
Page 11
Course Title: Computer Fundamentals and Programming Course Code: CCE-1105
Example: Write a C program that take a character/letter (English Alphabet) from keyboard and check
whether letter is Vowel or Consonant using switch statement.
# include <stdio.h>
main( )
{
char ch;
printf("Enter a Letter:");
scanf("%c", &ch);
switch(ch)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'o':
case 'O':
case 'i':
case 'I':
case 'u':
case 'U':
printf("Vowel");
break;
default:
printf("Consonant");
}
}
1. Write a C program that take a character/letter (English Alphabet) from keyboard and check whether letter is
Capital or Small using switch statement.
Page 12
Course Title: Computer Fundamentals and Programming Course Code: CCE-1105
Advantage of loops in C
1) It provides code reusability.
2) Using loops, we do not need to write the same code again and again.
2) Using loops, we can traverse over the elements of data structures (array or linked lists).
Types of C Loops
Based on loop body and loop condition there are two types of loops in C language that is given below:
Page 13
Course Title: Computer Fundamentals and Programming Course Code: CCE-1105
1. do-while loop in C
The do while loop is a post tested loop. Using the do-while loop, we can repeat the execution of several parts of
the statements. The do-while loop is mainly used in the case where we need to execute the loop at least once.
The do-while loop is mostly used in menu-driven programs where the termination condition depends upon the
end user.
do{
//code to be executed
}while(condition);
#include<stdio.h>
main()
{
int i=1;
do{
printf("%d \t", i);
i++;
}while(i<=10); Output
} 1 2 3 4 5 6 7 8 9 10
Page 14
Course Title: Computer Fundamentals and Programming Course Code: CCE-1105
2. while loop in C
While loop is also known as a pre-tested loop. In general, a while loop allows a part of the code to be executed
multiple times depending upon a given boolean condition. It can be viewed as a repeating if statement. The
while loop is mostly used in the case where the number of iterations is not known in advance.
while(condition){
//code to be executed
}
#include<stdio.h>
main()
{
int i=1;
while(i<=10)
{
printf("%d \t", i);
i++;
} Output
} 1 2 3 4 5 6 7 8 9 10
Page 15
Course Title: Computer Fundamentals and Programming Course Code: CCE-1105
o A conditional expression is used to check the condition. The statements defined inside the while loop
will repeatedly execute until the given condition fails.
o The condition will be true if it returns 0. The condition will be false if it returns any non -zero
number. o In while loop, the condition expression is compulsory.
o Running a while loop without a body is possible.
o We can have more than one conditional expression in while loop.
o If the loop body contains only one statement, then the braces are optional.
#include<stdio.h>
void main ()
{
int j = 1;
while(j+=2,j<=10)
{
printf("%d ",j);
}
printf("%d",j);
}
Output
357911
3. for loop in C
The for loop is used in the case where we need to execute some part of the code until the given condition is
satisfied. The for loop is also called as a per-tested loop. It is better to use for loop if the number of iteration
is known in advance.
for(initialization;condition;incr/decr)
//code to be executed
Page 16
Course Title: Computer Fundamentals and Programming Course Code: CCE-1105
#include<stdio.h>
main()
{
Output
1 2 3 4 5 6 7 8 9 10
Page 17
Course Title: Computer Fundamentals and Programming Course Code: CCE-1105
Properties of Expression 1
o The expression represents the initialization of the loop
variable. o We can initialize more than one variable in Expression 1.
o Expression 1 is optional.
o In C, we cannot declare the variables in Expression 1. However, It can be an exception in some compilers.
Properties of Expression 2
Page 18
Course Title: Computer Fundamentals and Programming Course Code: CCE-1105
#include <stdio.h>
int main()
{
int i;
for(i=0;i<=4;i++)
{
printf("%d ",i);
}
}
Output
0 1 23 4
#include <stdio.h>
int main()
{
int i,j,k;
Output
000
123
246
369
4812
Page 19
Course Title: Computer Fundamentals and Programming Course Code: CCE-1105
Properties of Expression 3
#include<stdio.h>
void main ()
{
int i=0,j=2;
for(i = 0; i<5; i++,j=j+2)
{
printf("%d %d\n",i,j);
}
}
Output
02
14
26
38
4 10
To make a for loop infinite, we need not give any expression in the syntax. Instead of that, we need to
provide two semicolons to validate the syntax of the for loop. This will work as an infinite for loop.
#include<stdio.h>
void main ()
{
for(;;)
{
printf("welcome to C programming Class");
}
}
If you run this program, you will see above statement infinite times.
Page 20
Course Title: Computer Fundamentals and Programming Course Code: CCE-1105
1. Write a C program that will print EVEN numbers from 1 to 20 using do-while, while and for loop.
2. Write a C program that will print ODD numbers from 1 to 20 using do-while, while and for loop.
3. Write a C program that will print EVEN numbers from 1 to n (get the value of n from keyboard) using
do-while, while and for loop.
4. Write a C program that will print ODD numbers from 1 to n (get the value of n from keyboard) using do-
while, while and for loop.
5. 1+2+3+------------------------------------ +20
6. 1+2+3+ +n
Use multiplication as well for series
7. 1+3+5+ +99
8. 2+4+6+------------------------------------ +100
9. 1+3+5+------------------------------------ +n (ODD number from keyboard)
10. 2+4+6+------------------------------------ +n (EVEN number from keyboard)
2 2 2 2
11. 1 +2 +3 +------------------------------------ +n
12. 1.2+2.3+3.4+------------------------------------ +n(n+1)
13. Write a C program that will calculate nth Factorial using do-while, while and for loop.
14. Write a C program that will calculate nth Fibonacci series using do-while, while and for loop.
15. Write a C program that will take an integer input and find out whether the number is prime or not using do-
while, while and for loop.
16. Write a C program that will take two integer inputs and calculate GCD using do-while, while and for loop.
17. Write a C program that will take two integer inputs and calculate LCM using do-while, while and for loop.
Engr. MD Jiabul Hoque, Lecturer, CCE, IIUC Page 21
Course Title: Computer Fundamentals and Programming Course Code: CCE-1105
C break statement
The break is a keyword in C which is used to bring the program control out of the loop. The break statement is
used inside loops or switch statement. The break statement breaks the loop one by one, i.e., in the case of
nested loops, it breaks the inner loop first and then proceeds to outer loops. The break statement in C can be
used in the following two scenarios:
Flowchart of break in c
Page 22
Course Title: Computer Fundamentals and Programming Course Code: CCE-1105
if(i == 5)
break;
}
printf("came outside of loop i = %d",i);
}
Note: You will have to answer 3 sets of Questions out of 4 sets of Questions
Page 24