You are on page 1of 84

CSE109

Computer Programming

Lecture 2:
Control Statements in C

Khaled Mahmud Shahriar


Assistant Professor
Department of CSE, BUET
**Slightly modified by
Ishrat Jahan
Control Statements
• Usually, statements are executed sequentially in a program
• Control statements allow to
• skip/jump instructions
• repeat a set of instructions
• C has the following types of control statements
• If statements
• Switch statement
• Conditional operator statement
• Go to statement
• Loop statement
if statement
• Selection statement/conditional statement
• Operation governed by outcome of a
conditional test
• if(expression) statement;
• expression: Condit tru Conditional
• any valid C expression ion e Code
• If expression is true statement will be executed
• If expression is false statement will be bypassed fal
• true: any nonzero value se
• false: zero
• if(num+1) printf(“nonzero");//num!=-1
statement will execute
• Normally expression consists of relational &
logical operator
if statement
#include<stdio.h> #include<stdio.h>
int main(void)
int main()
{
{
int num; int a, b;
scanf("%d", &num); printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
if(num>=0) printf("num is positive");
if ( a > b )
return 0; {
} printf("a is greater\n");
}
#include<stdio.h> if ( b > a)
{
int main(void)
printf("b is greater\n");
{ }
int num; if ( a == b )
{
scanf("%d", &num);
printf("a equals b\n");
if(num>=0) printf("num is positive"); }
if(num<0) printf("num is negative"); return 0;
}
return 0;
}
if statement #include<stdio.h>

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>

int main() int main()


{ {
int a; int a;
printf("Enter a number: "); printf("Enter a number: ");
scanf("%d", &a); scanf("%d", &a);
if ( a >=0 ) if ( a %2 == 0 )
{ {
printf("a is positive\n"); printf("a is even\n");
} }
else else
{ {
printf("a is negative\n"); printf("a is odd\n");
} }
return 0; return 0;
} }
if-else statement
Checking whether a year is a #include<stdio.h>
Leap Year or not int main() {
int year;
printf("Enter the year: ");
if (year is not divisible by 4) then scanf("%d", & year);
it is not a leap year if (year % 400 == 0)
else if (year is not divisible by 100) then {
printf("%d is Leap Year", year);
it is a leap year }
else if ( (year % 4 == 0) && (year % 100 != 0))
else if (year is not divisible by 400) then {
printf("%d is Leap Year", year);
it is not a leap year }
else
else it is a leap year {
printf("%d is not Leap Year", year);
}
return 0;
}
if-else statement
• else part is optional
• The else is associated with closest else-less if
if(n>0)
if(a>b) z=a; Even though the else is indented
else z=b; with the first if, by rule it will be
associated with the nearest if

• Braces must be used to force association with the first


if(n>0)
{
if(a>b) z=a;
}
else z=b;
Nested if #include<stdio.h>
int main(void)
{
Determine section from last int id;
three digits of student no. printf("Please enter last 3 digits of your id:\n");
scanf("%d", &id);
printf("You are in ");
A1: 1, 3, 5, 7, …, 59 if(id%2)
{
A2: 61, 63, 65, 67, …, 119 if(id<60)
printf("A1\n");
B1: 2, 4, 6, 8, …, 60 else
printf("A2\n");
B2: 62, 64, 66, 68, …, 120 }
else
{
if(id<61)
printf("B1\n");
else
printf("B2\n");
}
return 0;
}
Blocks of code
• Surround the statements in a block with opening and ending curly
braces.
• One indivisible logical unit
• Can be used anywhere a single statement may
• Multiple statements
• Common programming error:
• Forgetting braces of compound statements/blocks
Blocks of code
• if(expression) { #include<stdio.h>
statement1; int main( )
{
statement2; int numOfArg, sum;
… scanf("%d", &numOfArg);
statementN; if(numOfArg==2)
} {
int a, b;
else { scanf("%d %d", &a, &b);
statement1; sum=a+b;
statement2; }
… else
{
statementN;
int a, b, c;
} scanf("%d %d %d", &a, &b, &c);
• If expression is true all the statements with if sum=a+b+c;
}
will be executed printf("The sum is %d\n", sum);
• If expression is false all the statements with return 0;
}
else will be executed
if-else if statement
if(expression) • Multi-way decision
statement; • expressions are evaluated in order
else if (expression)
statement; • If the expression of any if is true
else if (expression) • the statement associated with it is
executed
statement;
else • Multiple statements can be
associated using curly braces
statement;
• the whole chain is terminated
• If none of the expressions are true
• else part is executed
• Handles none of the above/ default case
• Optional
if-else if statement
#include<stdio.h>
Finding maximum of two
int main()
numbers {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
if ( a > b )
{
printf("a is greater\n");
}
else if ( b > a)
{
printf("b is greater\n");
}
else
{
printf("a equals b\n");
}
return 0;
}
#include<stdio.h> #include<stdio.h> #include<stdio.h>

int main() int main() int main()


{ { {
int num; int num; int num;
char grade; char grade; char grade;
scanf("%d", &num); scanf("%d", &num); scanf("%d", &num);
Calculating Grades from Marks

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)

The integral expressions used in labels must


be constant expressions.
switch case properties(5)
After the switch statement, the control transfers to the
matching case, the statements written before/after case are not
executed.
switch case properties(6)
integral expressions used in labels can be
an expression of constants.
switch case properties(7)

Two case labels cannot


have same value.
What is the output of the following code?
Practice Problem: switch case
Operation on two #include<stdio.h>
int main( )
numbers based on {
the given operator int a, b;
int result;
char option;
printf("Enter first number and then operation and then second number: ");
scanf("%d %c %d", &a, &option, &b);
switch(option)
{
case '+':
result = a + b;
break;
case '-':
result = a - b;
break;
case '*':
result = a * b;
break;
default:
printf("Invalid operation\n");
}
printf("Result: %d\n", result);
return 0;
}
Practice Problem: switch case switch(s)
{
case 1:
Determine whether an input switch(ch)
{
character is a vowel or a case 'a':
consonant case 'e':
case 'i':
#include<stdio.h>
case 'o':
int main()
case 'u':
{
case 'A':
char ch;
case 'E':
int s;
case 'I':
printf("Enter a character: ");
case 'O':
scanf(" %c", &ch);
case 'U':
s = (ch >='A' && ch <='Z') || (ch>='a' && ch <='z');
printf("Vowel\n");
break;
default:
printf("Consonant\n");
}
break;
case 0:
printf("Not a letter\n");
}
return 0;
}
Practice Problem: switch case int day;
scanf("%d", &day);
switch(day)
Determine day of week for a given day {
case 1:
printf("Sat\n");
break;
case 2:
printf("Sun\n");
break;
case 3:
printf("Mon\n");
break;
case 4:
printf("Tues\n");
break;
case 5:
printf("Wed\n");
break;
case 6:
printf("Thurs\n");
break;
case 7:
printf("Fri\n");
break;
default:
printf("Invalid\n");
}
Conditional Expressions
• Uses ternary operator “?:”
• expression1?expression2:expression3;
• Equivalent to:
if(expression1)
expression2
else expression3
• Can be used anywhere an expression can be
Conditional Expressions

#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

• for loop can run negatively


• decrement can be used instead of increment
• for(i=20; i>0; i--) ...
• Can be incremented or decremented by more than one
• for(i=1; i<100; i+=5)
for loop
• All of the following loops will print 1 to 99
• for(i=1; i<100; i++)
printf("%d\n", i);
• for(i=1; i<=99; i++)
printf("%d\n", i);
• for(i=0; i<99; i++)
printf("%d\n", i+1);
• for(i=0; i<=98; i++)
printf("%d\n", i+1);
• So selection of initial value and loop control condition is important
for loop
Single Statement Block of Statements
for(i=1; i<100; i++) sum=0;
printf("%d\n", i); prod=1;
for(i=1; i<5; i++)
• Prints 1 to 99 {
sum+=i;
for(i=100; i<100; i++) prod*=i;
printf("%d\n", i); }
printf("sum, prod is %d, %d\
• This loop will not execute n", sum, prod);
for loop
GCD of two #include<stdio.h>
numbers:
int main(){
int a,b, min,i,gcd;
scanf("%d %d",&a, &b);
if(a<b)
min=a;
else
min=b;
for(i=1;i<=min;i++){
if(a%i==0 &&b%i==0)
gcd=i;
}
printf("gcd of %d and %d is %d\n",a, b, gcd);
return 0;
}
for loop
Nth Fibonacci #include<stdio.h>
number:
int main() {
int a, b, c, i, n;
What if n<3?
F0=0 a = 0; // first Fibonacci number
b = 1; // second Fibonacci number
printf("Enter n: ");
F1=1 scanf("%d", &n);
for (i = 3; i<=n; i++) {
Fn= Fn-1+ Fn-2 c = a + b;
a = b;
b = c;
}
printf("%d\n", c);
return 0;
}
for loop #include<stdio.h>

Finding the factors and int main() {


sum of the factors of a int i, n, sum, cnt;
number sum = 0;
cnt = 0;
printf("Enter n: ");
scanf("%d", &n);
printf("The factors are: ");
for ( i = 1; i <= n; i++) {
if ( n % i == 0) {
printf("%d ", i);
sum = sum + i;
cnt++;
}
}
printf("\n");
printf("Total factors: %d\n", cnt);
printf("Sum of factors: %d\n", sum);
return 0;
}
for loop #include<stdio.h>

Determine whether a number is int main() {


a perfect number or not int i, n, sum;
sum = 0;
printf("Enter n: ");
a perfect number is a positive integer that is scanf("%d", &n);
equal to the sum of its positive divisors, for ( i = 1; i < n; i++) {
if ( n % i == 0) {
excluding the number itself. sum = sum + i;
For instance, 6 has divisors 1, 2 and 3 }
}
(excluding itself), and 1 + 2 + 3 = 6, so 6 is a if ( sum == n ) {
perfect number. printf("Perfect Number\n");
}
else {
printf("Not Perfect Number\n");
}
return 0;
}
for loop
Determine whether a number is #include<stdio.h> This will show 1 as
prime or not prime. Change the code
int main() {
int i, n, is_prime;
so that it will print “not
A prime number is a number greater printf("Enter n: "); prime” for 1.
scanf("%d", &n);
than 1, that have only two factors – 1 is_prime = 1; How can we make the
for ( i = 2; i < n; i++) { loop runs smaller
and the number itself. Prime numbers
if ( n % i == 0) {
number of times?
are divisible only by the number 1 or is_prime = 0;
}
itself. } • Change the range of
if ( is_prime == 0) { the loop n/2 -> √n
For example, 29 is a prime number as
printf(“Not Prime\n"); • Use break statement
its factors are only 1 and 29 whereas 28 }
else {
is not a prime number as it has other printf("Prime\n");
}
factors, 2, 4, 7 and 14
return 0;
}
Use of break
#include<stdio.h>
Primality testing
using break int main() {
int i, n, is_prime;
printf("Enter n: ");
scanf("%d", &n);
is_prime = 1;
for ( i = 2; i < n/2; i++) {
if ( n % i == 0) {
is_prime = 0;
break;
}
}
if ( is_prime == 0) {
printf("Not Prime\n");
}
else {
printf("Prime\n");
}
return 0;
}
Loop variation
• for( ; ; ){}
• for(ch=getchar(); ch!=‘q’; ch=getchar()) {}
• for(i=0; i<n; )
{
i++;
}
Nested for loop
#include<stdio.h>

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

N=4 N=3 N=5


1 * 1
12 *** 11
123 ***** 121
1234 *** 1331
* 14641
Pascal’s Triangle
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 <= 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>

int main() { int main() {


int n, i, sum; int n, i, mult;
sum = 0; mult = 1;
printf("Enter n: "); printf("Enter n: ");
scanf("%d", &n); scanf("%d", &n);
i = 1; i = 1;
while ( i <= n) { while ( i <= n) {
sum = sum + i; mult = mult * i;
i++; i++;
} }
printf("Sum: %d\n", sum); printf("Mult: %d\n", mult);
return 0; return 0;
} }
while loop
Digit Sum Continuous Input
#include<stdio.h> #include<stdio.h>

int main() { int main() {


int n, sum, d; int a, b, sum;
sum = 0; char c;
printf("Enter n: "); c = 'y';
scanf("%d", &n); while (c == 'y') {
while (n != 0) { printf("Enter a and b: ");
d = n % 10; scanf("%d%d", &a, &b);
sum = sum + d; printf("Sum: %d\n", a+b);
n = n / 10; printf("More? (y/n): ");
}
printf("DigitSum: %d\n", sum); scanf(" %c", &c);
return 0; }
} return 0;
}
while loop

This will show 1 as


prime. Change the
code so that it will
print “not prime”
for 1.
while loop
• Common error
• Forgetting to increment
• Normally used when increment is not needed
• while(ch!=‘q’)
{

ch=getchar();
}
do while loop
• Syntax for(initialization; conditional-test; increment) statement;
do
{
statement1;
statement2;
initialization;

do
}
{
while(expression);
statement;
• Test is at the bottom increment;
• Will execute at least once } while(conditional-test);
• Common error
• Forgetting the semicolon (;)
after while
do while loop
do while
for
do while loop

It will show that 2 is not


prime

Also, this code will


show 1 as prime.
Change the code so
that it will print
“not prime” for 1.
Acknowledgement
All these slides of this course have been prepared by taking help from numerous
resources. The notable contributors are listed below.
1. Content and organization of many pages have been taken from the lecture slides and
codes of the course CSE110 offered to the Department of EEE that were -
i. primarily created by Johra Muhammad Moosa, Assistant Professor (on leave),
CSE, BUET and
ii. later modified by Madhusudan Basak, Assistant Professor, CSE, BUET
2. Most of the wonderful coding examples have been taken from the course CSE281
offered to the Department of BME instructed by Rifat Shahriyar, Professor, CSE,
BUET (course link).
3. search and all the sites that it made available in response to the course
related queries. Some of the sites are: https://geeksforgeeks.org/,
https://www.tutorialspoint.com, https://www.w3schools.com and the list goes on …
Thank You ☺

You might also like