Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
14 views26 pages

Section 3 - 4 Program Contral Statements

nothing
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views26 pages

Section 3 - 4 Program Contral Statements

nothing
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Course Title: Computer Fundamentals and Programming Course Code: CCE-1105

Section III: Program Control Statements

Lesson 16, 17, 18, 19, 20 and 21

yellows are section 3


Syllabus

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.

green are section 4 but nested loop missing

Page 1
Course Title: Computer Fundamentals and Programming Course Code: CCE-1105

Program Control Statement


There are three types of program control statement:

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

Lesson 16 and 17: Conditional Control Statement


if, if-else, if-else-if ladder, nested if, switch

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.

There are the following variants of if statement in C language.

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.

How to check whether the input is


negative 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
}

Flowchart of the if-else statement in C

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...

Exercise: if-else statement

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

3. If-else-if ladder Statement


The if-else-if ladder statement is an extension to the if-else statement. It is used in the scenario where there
are multiple cases to be performed for different conditions. In if-else-if ladder statement, if a condition is true
then the statements defined in the if block will be executed, otherwise if some other condition is true then the
statements defined in the else-if block will be executed, at the last if none of the condition is true then the
statements defined in the else block will be executed. There are multiple else-if blocks possible. It is similar to
the switch case statement where the default is executed instead of else block if none of the cases is matched.

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
}

Flowchart of else-if ladder statement in C

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);

if(marks >=80 && marks <= 100)


{
printf("Congrats ! you scored grade A+ ...");
}
else if (marks >=75 && marks =< 79)
{
printf("You scored grade A ...");
}
else if (marks >=70 && marks =< 74)
{
printf("You scored grade A- ...");
}
else if (marks >=65 && marks =< 69)

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!!");
}
}

Exercise: if-else-if ladder statement

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");
}
}

Exercise: Nested if statement

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.

The syntax of switch statement in c language is given below:

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;
}

Rules for switch statement in C language

1) The switch expression must be of an integer or character type.

2) The case value must be an integer or character constant.

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");
}
}

Exercise: switch statement

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

Lesson 18 and 19: Loops (for, while, do-while)


Loops
The looping can be defined as repeating the same process multiple times until a specific condition satisfies.

Why use loops in C language?


The looping simplifies the complex problems into the easy ones. It enables us to alter the flow of the program
so that instead of writing the same code again and again, we can repeat the same code for a finite number of
times. For example, if we need to print the first 10 natural numbers then, instead of using the printf statement 10
times, we can print inside a loop which runs up to 10 iterations.

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:

1. Entry control loop (for, while)


2. Exit control loop (do-while)

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.

The syntax of do-while loop in c language is given below:

do{
//code to be executed
}while(condition);

Flowchart of do-while loop

Example 1: Write a C program that print 1 to 10 using do-while loop

#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.

The syntax of while loop in c language is given below:

while(condition){
//code to be executed
}

Flowchart of while loop

Example 1: Write a C program that print 1 to 10 using while loop

#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

Properties of while loop

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.

Example 2: while loop with more than conditional expression

#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.

The syntax of for loop in c language is given below:

for(initialization;condition;incr/decr)

//code to be executed

Page 16
Course Title: Computer Fundamentals and Programming Course Code: CCE-1105

Flowchart of for loop in C

Example 1: Write a C program that print 1 to 10 using for loop

#include<stdio.h>
main()
{

for(int i=1; i<=10; i++)


{
printf("%d \t", i);
}
}

Output
1 2 3 4 5 6 7 8 9 10

Syntax of for loop : Explanation

for(Expression 1; Expression 2; Expression 3)


{
//code to be executed
}

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.

Example 1: Expression 1 of for loop


#include <stdio.h>
int main()
{
int a,b,c;
for(a=0,b=12,c=23; a<2; a++)
{
printf("%d ",a+b+c);
}
}
Output
35 36

Example 2: Expression 1 of for loop


#include <stdio.h>
int main()
{
int i=1;
for(;i<5;i++)
{
printf("%d ",i);
}
}
Output
1234

Properties of Expression 2

o Expression 2 is a conditional expression. It checks for a specific condition to be satisfied. If it is not,


the loop is terminated.
o Expression 2 can have more than one condition. However, the loop will iterate until the last
condition becomes false. Other conditions will be treated as statements.
o Expression 2 is optional.
o Expression 2 can perform the task of expression 1 and expression 3. That is, we can initialize the
variable as well as update the loop variable in expression 2 itself.
o We can pass zero or non-zero value in expression 2. However, in C, any non-zero value is true, and
zero is false by default.

Page 18
Course Title: Computer Fundamentals and Programming Course Code: CCE-1105

Example 1: Expression 2 of for loop

#include <stdio.h>
int main()
{
int i;
for(i=0;i<=4;i++)
{
printf("%d ",i);
}
}
Output
0 1 23 4

Example 2: Expression 2 of for loop

#include <stdio.h>
int main()
{
int i,j,k;

for(i=0,j=0,k=0; i<4,k<8,j<10; i++) only one true


{
printf("%d %d %d\n",i,j,k);
j+=2;
k+=3;
}
}

Output
000
123
246
369
4812

Example 3: Expression 2 of for loop


#include <stdio.h>
int main()
{
int i;
for(i=0; ;i++)
{
printf("%d",i);
}
}
Output
infinite loop

Page 19
Course Title: Computer Fundamentals and Programming Course Code: CCE-1105

Properties of Expression 3

o Expression 3 is used to update the loop variable.


o We can update more than one variable at the same
time. o Expression 3 is optional.

Example 1: Expression 3 of for loop

#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

Infinitive for loop in C

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

Exercise: do-while, while and for loop

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

Lesson 20: Jump Statement (break, continue, goto)

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:

1. With switch case


2. With loop
Syntax:
//loop or switch case
break;

Flowchart of break in c

Page 22
Course Title: Computer Fundamentals and Programming Course Code: CCE-1105

Example 1: break statement with switch


case #include<stdio.h>
main(){
int number=0;
printf("enter a number:");
scanf("%d",&number);
switch(number)
{
case 10:
printf("number is equals to
10"); break;
case 50:
printf("number is equal to
50"); break;
case 100: Output
printf("number is equal to enter a number:4
100"); break; number is not equal to 10, 50 or
default: 100 enter a number:50
printf("number is not equal to 10, 50 or number is equal to 50
100"); } }

Example 2: break statement inside for loop


#include<stdio.h>
void main ()
{
int i; Output
for(i = 0; i<10; i++)
{ 0 1 2 3 4 5 came outside of loop i = 5
printf("%d ",i);

if(i == 5)
break;
}
printf("came outside of loop i = %d",i);
}

Example 3: break statement with while loop


#include<stdio.h>
void main () {
int i = 0;
while(1)
{
printf("%d ",i); Output
i++;
0 1 2 3 4 5 6 7 8 9 came out of while loop
if(i == 10)
break;
}
printf("came out of while loop");
}
Engr. MD Jiabul Hoque, Lecturer, CCE, IIUC Page 23
Course Title: Computer Fundamentals and Programming Course Code: CCE-1105

Lesson 21: Review class

Content: Review of Midterm Examination (30 Marks)

Section 1: Computer Organization (1/2 - 1 Question)


Section 2: Introduction to Programming (2 Questions)
Section 3: Program Control Statements (1-3/2 Questions)

Note: You will have to answer 3 sets of Questions out of 4 sets of Questions

Review Questions for Section III


Q1. Write a C program to find the largest number of the three using if statement.
Q2. Write a C program to check whether a number is even or odd using if-else statement in C language.
Q3. Write a C Program to check whether a person is eligible to vote or not using if-else.
Q4. Explain if-else ladder statement.
Q5. Write a C Program to calculate the grade of the student according to the specified marks using if-else-if
ladder.
Q6. Define switch statement.
Q7. Explain the rules for switching a statement in C.
Q8. Define loops.
Q9. State the reasons for using loops in C language.
Q10. What are the advantages of using loops in C language?
Q11. Explain the differences between while and do-while loop.
Q12. Explain while loop with example.
Q13. Explain do-while loop with example.
Q14. Explain for loop with example.
Q15. Explain the properties of while loop.
Q16. Plus all lectures.

Page 24

You might also like