Sei sulla pagina 1di 37

Introduction to Computer

Programming
Control Structures
Muhammad Adnan Talib

Lec#06

29th March,2012

Computer Science Department


COMSATS Institute of Information Technology
Sahiwal
Control Structures
Sequential execution
Statements executed in order (from first to last
sequence).
Transfer of control
Next statement executed not next one in
sequence.
3 control structures
 Sequence structure
 Programs executed sequentially by default
 Selection structures (conditional instructions)
 if, if/else, switch
 Repetition structures (loop/jump instructions)
 while, do/while, for
Loops
 The versatility of computer lies in its ability to
perform a set of instructions repeatedly

 This involves repeating some portion of the


program either a specific number of time or
until a particular condition is being satisfied

 This is done through a loop control structure


Loops
 C supports three methods to repeat a set of
statements

 while loop
 for loop
 do while loop
while Repetition Structure
Repetition structure
Action repeated while some condition remains true
Pseudocode
while there are more items on my shopping list
Purchase next item and cross it off my list
  while loop repeats until condition becomes false
C++ Language Example
int product = 2;
while ( product <= 1000 )
product = 2 * product;
Flow Chart of while loop
Start

Initialize

Test False
Stop

True

Body of Loop

Increment
 /* 
 * C 
 * Using a while loop to ask the user to input a number 
 * between 1 and 10 (inclusive). 
 * 
 *   Variables: 
 *        value  : variable to store the input  
 */ 
cout<<"Please Enter a Number between 1 and 10 (1-10): "; 
cin>>value; 
 
while ( value < 1 || value > 10) 
  { 
 
    cout<<"Incorrect input, please try again.\n"; 
    cout<<"Enter a Number between 1 and 10 (1-10): "; 
    cin>>value; 
 
  }
Fictorial…

int n ;
int j =1; int i=1; / ∗ initialization ∗ /
while (i <= n) / ∗ condition ∗ /
{
j ∗= i;
i ++; / ∗ increment ∗ /
}
So how to print

******
*****
****
***
**
*
by using while loop?
num=6
>>> while num>0:
print ‘*’ * num
num = num-1
 //#include <stdio.h>
 #include <conio.h>
 #include <iostream.h>
 int main()
 {
 int num = 6;
 int i = num;
 while (num > 0)
 {
 while(i!=0)
 {
 cout<<"*";
 i--;
 }
 cout<<endl;
 num--;
 i = num;
 }
 getche();
 return 0;
 }
Then, how to print
*
**
***
****
*****
******
by using while loop?
Solution…
 //#include <stdio.h>
 #include <conio.h>
 #include <iostream.h>
 int main()
 {
 int num = 1;
 int i = 1;
 while (num != 7)
 {
 while(i<=num)
 {
 cout<<"*";
 i++;
 }
 cout<<endl;
 num++;
 i = 1;
 }
 getche();
 return 0;
 }
Solution…for loop
 //#include <stdio.h>
 #include <conio.h>
 #include <iostream.h>
 int main()
 {
 int num = 1;
 int i = 1;
 for (int num = 1; num != 7; num++)
 {
 for(int i = 1;i<=num; i++)
 {
 cout<<"*";
 }
 cout<<endl;
 }
 getche();
 return 0;
 }
for Loop
Start

Initialize

Test False
Stop

True

Body of Loop

Increment
General form of for loop
Example
 Fictorial…

int factorial ( int n) {


int i, j=1;
for (i =1; i<= n; i++)
j ∗= i;
return j;
}
Multiple Initializations in for
loop
for (int i=1,j=2;j<=10;j++)
do while Loop
Similar to while but condition is checked
at the end
Format of do-while loop is
 A do-while loop
is executed at
least once
Start

Initialize

Body of Loop

Increment

True
Test

False

Stop
Infinite Loop
This loop will never
terminate
Since you are not
updating value of i
therefore condition will
never become false
Infinite Loops
Sometimes we intentionally use infinite
loops
In this case we terminate by other means
e.g. by exiting the program
Nested Loops
The break Statement
We often come across situations where we
want to jump out of a loop instantly,
without waiting to get back to conditional
test

The keyword break allows this

When the keyword break is encountered


inside a loop, control automatically passes
to the first statement after the loop
Example:
Write a Program to determine whether a
number is Prime or not.
A prime number is one which is divisible
only by 1 or itself
How you can solve this problem?
There are multiple ways to solve a problem
Further Optimization???

mber is not prime it will always be divisible


umber that is less than or equal to its square r
break statement
Reference
Ch.5 of the Text Book
Control Statements: Part 2

Potrebbero piacerti anche