[go: up one dir, main page]

0% found this document useful (0 votes)
16 views38 pages

Control Structure-1 (202302IGS1232001)

This document discusses control structures in computer programming, including loops and conditional statements. It provides examples of how to use if, if-else, for, while, do-while, break, continue, switch and nested statements. Key points covered include: - The general forms of for, while, do-while loops and how they are executed - Using if, if-else statements to check conditions and execute code blocks - Multi-way if-else statements and nested if statements - Examples of calculating sums and simple interest using for loops - The concept of nesting loops and breaking out of loops using break

Uploaded by

JAHONGIR
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)
16 views38 pages

Control Structure-1 (202302IGS1232001)

This document discusses control structures in computer programming, including loops and conditional statements. It provides examples of how to use if, if-else, for, while, do-while, break, continue, switch and nested statements. Key points covered include: - The general forms of for, while, do-while loops and how they are executed - Using if, if-else statements to check conditions and execute code blocks - Multi-way if-else statements and nested if statements - Examples of calculating sums and simple interest using for loops - The concept of nesting loops and breaking out of loops using break

Uploaded by

JAHONGIR
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/ 38

Computer Programming

(IGS1232)

3. Control Structure (1)

Instructor:
Choonwoo Ryu, Ph.D.
How to compute summation from
1 to 10?
▀ int n1 = 1;
▀ int n2 = 2;

▀ int n10 = 10;

▀ int nsum = n1 + n2 + … + n10;

2
How to compute summation from
1 to 1000?
▀ int n1 = 1;
▀ int n2 = 2;

▀ int n1000 = 1000;

▀ int nsum = n1 + n2 + … + n1000;

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

▀ Conditional control statement


➢ if or if-else
➢ switch

5
if statement

The condition is true


if( conditional expression )
{ Execute the statements
do this;
}

false
Conditional
expression

true

Statements (inside if statement)

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;

if( score >= 60 )


printf(“passed.\n");

if( height >= 130 && age >= 10 )


printf(“passed \n");

int temperature = -10;


if ( temperature < 0 )
printf(“ Minus degree \n"); // when the condition is true
printf(“the temperature is %d degrees.\n", temperature); // always

8
if statement
▀ Compound statement

if( score >= 60 )


{
printf(“passed \n");
printf(“can get the scholarship\n");
}

9
if statement
// Distinguish negative and positive numbers using if statement
#include <stdio.h>

int main(void)
{
int number;

printf("Insert an integer value : "); If the value


scanf("%d", &number); is 25

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;

printf("insert the year : ");


scanf("%d", &year);

if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)


printf("%d year is a leap year.\n", year);
else
printf("%d year is a normal year.\n", year);

return 0;
}

insert the year : 2020


2020 year is a leap year.

12
Nested if
if( conditional expression 1 )
if(conditional expression 2 )
do this;

if( score > 80 )


if( score > 90 )
printf(“ your score is A.\n");

if( score > 80 )


if( score > 90 )
printf(“your score is A.\n");
else
printf(“your score is B.\n");

13
Multi-way if-else statement

14
Multi-way if-else statement

15
while statement
▀ The general form of while is as shown below:

Initialize loop counter;


while (test loop counter using a condition)
{
do this;
and this;
increment loop counter;
}

concept of execution of the while statement

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:

For(initialize; test counter; increment counter)


{
do this;
and this;
}

Initialize loop counter;


while (test loop counter using a condition)
{
do this;
and this;
increment loop counter;
}
18
concept of execution of the for statement
for statement
#include <stdio.h>
int main(){
int p, n, count;
float r, si;
for (count = 1; count <= 3; count = count + 1) {
printf("Enter values of p, n, and r: ");
scanf("%d %d %f", &p, &n, &r);
si = p * n * r / 100;
printf("Computed result = %f\n", si);
}
return 0;
Enter values of p, n, and r: 10 10 0.5
}
Computed result = 0.500000
Enter values of p, n, and r: 10 10 20.5
Computed result = 20.500000
Enter values of p, n, and r: 1 2 3.5
Computed result = 0.070000

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

value of count is set to an initial return 0;


value 1. }

➢ 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

concept of execution of concept of execution of


the while statement the do-while statement

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

#include <stdio.h> #include <stdio.h>


int main() int main()
{ {
while('a' < 'b’) int i = 10;
printf("Good Morning!\n"); while(i = 20)
return 0; printf("Good Morning!\n");
} 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;
} }

#include <stdio.h> #include <stdio.h>


int main() int main()
{ {
while ('1' < '2’) char x;
printf("in while loop\n"); for(x=0; x<=255; x++)
return 0; printf("ASCII value %d,
} Character %c\n", x, x);
return 0;
} 34
Exercise #5
▀ What would be the output of the following programs:
#include <stdio.h> #include <stdio.h>
int main() int main()
{ {
int x = 4, y = 0, z; int x = 4, y = 0, z;
while (x >= 0) { while (x >= 0) {
x--; if (x == y)
y++; break;
if (x == y) else
continue; printf("%d %d\n", x, y);
else x--;
printf("%d %d\n", x, y); y++;
} }
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

for (date = 1; date <= DAYS_OF_MONTH; date++) {


if (day == 7) {
day = 0;
printf("\n");
}
day++;
printf("%4d ", date);
}
printf("\n===================================\n");
return 0;
}
36
HW #3
1. Write a program to calculate the summation of natural number from 1 to
100 using each of the following statements.
➢ while
➢ do-while
➢ For

2. Write a program to produce the following output(use loop statement):

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

You might also like