Control Structure-1 (202302IGS1232001)
Control Structure-1 (202302IGS1232001)
(IGS1232)
Instructor:
Choonwoo Ryu, Ph.D.
How to compute summation from
1 to 10?
▀ int n1 = 1;
▀ int n2 = 2;
2
How to compute summation from
1 to 1000?
▀ int n1 = 1;
▀ int n2 = 2;
3
Contents
▀ The while statement
▀ The for statement
▀ The do-while statement
▀ The if statement
▀ The switch statement
4
Control statement
▀ Loop control statement
➢ while
➢ for
➢ do-while
5
if statement
false
Conditional
expression
true
6
if statement
▀ if (x > y) // if x is greater than y
▀ if (x < y) // if x is smaller than y
▀ if (x >= y) // if x is greater than or equal to y
▀ if (x <= y) // if x is smaller than or equal to y
▀ if (x == y) // if x is equal to y
▀ if (x != y) // if x is not equal to y
▀ if ( (x > y) && (x > z) ) // if x> y AND x > z
▀ if ( (x > y) || (x > z) ) // if x>y OR x > z
7
if statement
if( sales > 2000 )
bonus = 200;
8
if statement
▀ Compound statement
9
if statement
// Distinguish negative and positive numbers using if statement
#include <stdio.h>
int main(void)
{
int number;
if (number > 0)
printf("The value is positive \n");
if (number == 0)
printf("The value is zero\n");
if (number < 0)
printf("The value is negative\n");
return 0; 25
} the value is positive
10
if-else statement
if(conditional expression )
do this 1;
else
do this 2;
11
if-else statement
#include <stdio.h>
int main(void)
{
int year;
return 0;
}
12
Nested if
if( conditional expression 1 )
if(conditional expression 2 )
do this;
13
Multi-way if-else statement
14
Multi-way if-else statement
15
while statement
▀ The general form of while is as shown below:
16
while statement
#include <stdio.h>
int main(){
int i = 1; Initialize loop counter
while (i <= 10) Test loop counter using
condition
{
printf("%d\n", i);
i = i + 1; Increment loop counter
}
}
17
for statement
▀ The general form of for is as shown below:
19
for statement
#include <iostream>
using namespace std;
int main(){
int p, n, count;
float r, si;
for (count = 1; count <= 3; count = count + 1) {
cout << "Enter values of p, n, and r: ";
cin >> p >> n >> r;
si = p * n * r / 100;
cout << "Computed result = " << si << endl;
}
return 0;
Enter values of p, n, and r: 10 10 2
}
Computed result = 2
Enter values of p, n, and r: 20 30 3.5
Computed result = 21
Enter values of p, n, and r: 1 1 0.5
Computed result = 0.005
20
for statement #include <stdio.h>
int main(){
int p, n, count;
▀ Let us now examine how the for float r, si;
for (count = 1; count <= 3; count = count + 1) {
statement gets executed: printf("Enter values of p, n, and r: ");
scanf("%d %d %f", &p, &n, &r);
➢ When the for statement is si = p * n * r / 100;
executed for the first time, the }
printf("Computed result = %f\n", si);
➢ Now the condition count <= 3 is tested. Since count is 1 the condition
is satisfied, and the body of the loop is executed for the first time.
➢ Upon reaching the closing brace of for, control is sent back to the for
statement, where the value of count gets incremented by 1.
➢ Again the test is performed to check whether the new value of count
exceeds 3.
➢ If the value of count is still within the range 1 to 3, the statements
within the braces of for are executed again.
➢ The body of the for loop continues to get executed till count doesn’t
exceed the final value 3.
➢ When count reaches the value 4 the control exits from the loop and is
transferred to the statement (if any) immediately after the body of for.
21
Nesting for loops
▀ Concept of the nesting for loop
Outer
loop
Inner
loop
22
Nesting for loops
#include <stdio.h>
int main() {
int i, j, sum = 0;
for (i = 1; i <= 3; i = i + 1) {
for (j = 0; j < 2; j++) {
sum = i + j; i=1, j=0, sum=1
printf("i=%d, j=%d, sum=%d\n", i, j, sum); i=1, j=1, sum=2
} i=2, j=0, sum=2
} i=2, j=1, sum=3
return 0; i=3, j=0, sum=3
} i=3, j=1, sum=4
23
break statement
▀ We often come across situations where we want to jump out of a
loop instantly, without waiting to get back to the conditional test.
▀ The keyword break allows us to do this.
➢ When break is encountered inside any loop, control automatically
passes to the first statement after the loop.
➢ A break is usually associated with an if.
24
break statement
2, 2 #include <stdio.h>
2, 3 int main() {
2, 4 int num, i;
... printf("Enter a number:");
2, 148 scanf("%d", &num);
2, 149
#include <stdio.h> 3, 151 i = 2;
int main() { 3, 152 while (i <= num - 1) {
int i = 1, j = 1; ... if (num % i == 0) {
while (i++ <= 100) { 3, 200 printf("Not a prime number");
while (j++ <= 200) { 3, 201 break;
if (j == 150) } Enter a number:11
break; i++; Prime number
else }
printf("%d, %d\n", i, j); if (i == num)
} printf("Prime number");
} Enter a number:20
return 0; return 0;
Not a prime number
} }
25
continue statement
▀ In some programming situations we want to take the control to the
beginning of the loop, bypassing the remaining statements inside the
loop, which have not yet been executed.
➢ The keyword continue allows us to do this.
▀ When continue is encountered inside any loop, control automatically
moves to the beginning of the loop.
#include <stdio.h>
int main() {
int i, j;
for (i = 1; i <= 2; i++) {
for (j = 1; j <= 2; j++) {
if (i == j) 12
continue;
printf("\n%d %d\n", i, j); 21
}
}
return 0;
}
26
do-while statement
▀ The do-while loop looks like this:
do
{
this;
and this;
and this;
}while(this condition is true);
27
do-while statement
▀ There is a minor difference between the working of while and do-
while loops.
➢ This difference is the place where the condition is tested.
➢ The while tests the condition before executing any of the
statements within the while loop.
➢ As against this, the do-while tests the condition after having
executed the statements in the loop.
28
do-while statement
▀ Comparison between the while and do-while basic concept
29
Summary
▀ The three type of loops available in C or C++ are for, while, and do-
while.
▀ A break statement takes the execution control out of the loop.
▀ A continue statement skips the execution of the statements after it
and takes the control to the beginning of the loop.
▀ A do-while loop is used to ensure that the statements within the loop
are executed at least once.
▀ The ++ operator increments the operand by 1, whereas, the --
operator decrements it by 1.
▀ The operators +=, -=, *=, /=, %= are compound assignment
operators. They modify the value of the operand to the left of them.
➢ +=: a+=b ➔ a = a+b;
30
Exercise #1
▀ What would be the output of the following programs:
#include <stdio.h> #include <stdio.h>
int main() int main()
{ {
int j; int i=1;
while (j <= 10) { while (i <= 10) {
printf("\n%d", j); printf("\n%d", i);
j = j + 1; i++;
} }
return 0; return 0;
} }
31
Exercise #2
▀ What would be the output of the following programs:
#include <stdio.h> #include <stdio.h>
int main() int main()
{ {
int x=1; int x=1;
while (x == 1) { while (x == 1)
x = x - 1; x = x - 1;
printf("\n%d", x); printf("\n%d", x);
}
return 0; return 0;
} }
#include <stdio.h>
int main()
{
char x;
while (x = 0; x <= 255; x++)
printf("\nASCII value %d Character %c", x, x);
return 0;
}
32
Exercise #3
▀ What would be the output of the following programs:
#include <stdio.h> #include <stdio.h>
int main() int main()
{ {
int x = 4, y, z; int x = 4, y = 3, z;
y = --x; z = x-- -y;
z = x--; printf("%d %d %d\n", x, y, z);
printf("%d %d %d\n", x, y, z); return 0;
return 0; }
}
33
Exercise #4
▀ What would be the output of the following programs:
#include <stdio.h> #include <stdio.h>
int main() int main()
{ {
int i; float x = 1.1;
while (i = 10) { while (x == 1.1) {
printf("%d\n", i); printf("%f\n", x);
i = i + 1; x = x - 0.1;
} }
return 0; return 0;
} }
35
Exercise #6
#include <stdio.h>
#define START_DAY (0)
#define DAYS_OF_MONTH (31)
int main()
{
int day, date;
printf("===================================\n");
printf("Sun. Mon. Tue. Wen. Thu. Fri. Sat. \n");
printf("===================================\n");
for (day = 0; day < START_DAY; day++)
printf(" "); // print space
37
HW #3
3. Write a program to calculate the following equations:
30
➢ S1 =
i =1
(i 2 + 1)
30 5
➢ S2 = (i j)
i =10 j = 0
38