ch03 loops and decisions

Upload: nidamah

Post on 02-Jun-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/10/2019 Ch03 Loops and Decisions

    1/42

    Introduction to Programming

    Engr. Rashid Farid [email protected]

    Chapter 03: Loops and Decisions

    International Islamic University H-10, Islamabad, Pakistan

    http://www.iiu.edu.pk

    mailto:[email protected]:[email protected]
  • 8/10/2019 Ch03 Loops and Decisions

    2/42

    Relational Operators //demonstrates relational operators

    #include #include using namespace std;int main(){

    int n;

    cout > n;cout

  • 8/10/2019 Ch03 Loops and Decisions

    3/42

    Number SystemsBase 16 Base 10 Base 8 Base 2

    0 0 0 0000

    1 1 1 0001

    2 2 2 0010

    3 3 3 0011

    4 4 4 0100

    5 5 5 0101

    6 6 6 0110

    7 7 7 0111

    8 8 10 1000

    9 9 11 1001

    A 10 12 1010B 11 13 1011

    C 12 14 1100

    D 13 15 1101

    E 14 16 1110

    F 15 17 1111

  • 8/10/2019 Ch03 Loops and Decisions

    4/42

    Bits and Bytes All data is represented internally by

    computers as sequences of bits .Each bit can assume the value 0 or 1 .4 bits = 1 nibble8 bits = 2 nibble or 1 Byte210 bytes = 1 Kilo Byte or 1 KB220 bytes = 1 Mega Byte or 1 MB230 bytes = 1 Giga Byte or 1 GB240 bytes = 1 Tera Byte or 1 TB250 bytes = 1 Peta Byte or 1 PB260 bytes = 1 Exa Byte or 1 EB270 bytes = 1 Zetta Byte or 1 ZB280 bytes = 1 Yotta Byte or 1 YB290 bytes = 1 Bronto Byte 2100 bytes = 1 Geo Byte

  • 8/10/2019 Ch03 Loops and Decisions

    5/42

    Logic Gates and Bitwise Operators

    Bitwise Operators in C++ areBitwise AND ( & )Bitwise OR ( | )

    Bitwise Exclusive OR ( ^ )Bitwise NOT ( ~ )Left Shift ( > )

    A B A OR B

    A | B

    A AND B

    A & B

    A XOR B

    A ^ B

    NOT A

    ~ A

    NOT B

    ~ B0 0 0 0 0 1 1

    0 1 1 0 1 1 0

    1 0 1 0 1 0 1

    1 1 1 1 0 0 0

  • 8/10/2019 Ch03 Loops and Decisions

    6/42

    Left Shift( > )//demonstrates left shift ()

    #include #include using namespace std;int main(){

    short n;

    cout > n;cout

  • 8/10/2019 Ch03 Loops and Decisions

    7/42

    Bitwise Operators #include

    #include // for setw()#include using namespace std;int main( void ) {

    short var1 = 0x0035 & 0x000F; // bitwise AND

    short var2 = 0x0004 | 0x0068; // bitwise ORshort var3 = 0x0054 ^ 0x00F0; // bitwise XORshort var4 = ~ 0x0055; // bitwise NOT

    cout

  • 8/10/2019 Ch03 Loops and Decisions

    8/42

    Bitwise Operators //swap two numbers without using extra variable

    #include #include using namespace std;int main(){

    int A,B;

    cout > A >> B;cout

  • 8/10/2019 Ch03 Loops and Decisions

    9/42

    Logical Operators: && , || , ! #include #include using namespace std;int main( void ) {

    cout

  • 8/10/2019 Ch03 Loops and Decisions

    10/42

    Decision Making in C++ Often, people base their decisions on certain

    conditions . For example, a person might go toa doctor if he feels sick. The decision ,whether to go the doctor, is based on acertain condition : feeling sick. The same istrue when using programs.

    All of the previous programs had sequentialexecution: each statement in the programexecutes once, and they are executed in thesame order that they are listed.

    You can design your program so that it selectswhich code to execute based on certainconditions.

  • 8/10/2019 Ch03 Loops and Decisions

    11/42

    Decision: Using the if statement The if statement allows conditional execution.

    Its syntax isif (condition)

    statement ;where condition is an integral expression andstatement is any executable statement.

    The statement will be executed only if the valueof the integral expression is nonzero ( TRUE).When we want to run more than one statementsbased on some condition, the syntax looks like

    this:if (condition){

    many_statement_terminated_with ;}

  • 8/10/2019 Ch03 Loops and Decisions

    12/42

    Decision: Using the if statement// This program tests if one positive integer is not

    // divisible by another:#include #include using namespace std;int main(){

    int n,d;cout > n >> d;if (n%d)

    cout

  • 8/10/2019 Ch03 Loops and Decisions

    13/42

    Decision: Using the if statement// demonstrates IF with multiline body

    #include #include using namespace std;int main(){

    int x;

    cout > x;if ( x > 100 ){

    cout

  • 8/10/2019 Ch03 Loops and Decisions

    14/42

    Decision: Using the if statement// The Minimum of Three Integers#include #include using namespace std;int main(){

    int n1,n2,n3,min;

    cout > n1 >> n2 >> n3;min=n1; // now min

  • 8/10/2019 Ch03 Loops and Decisions

    15/42

    Decision: Using the ifelse statement The if..else statement causes one of two

    alternative statements to execute depending uponwhether the condition is true. Its syntax is

    if (condition) statement1 ;else statement2 ;where condition is an integral expression andstatement1 and statement2 are executable state-ments.

    If the value of the condition is nonzero (TRUE)then statement1 will execute; otherwise (FALSE)

    statement2 will execute.

  • 8/10/2019 Ch03 Loops and Decisions

    16/42

    Decision: Using the ifelse statement// demonstrates Ifelse #include #include using namespace std;int main(){

    int x;

    cout > x;if ( x % 2 )

    cout

  • 8/10/2019 Ch03 Loops and Decisions

    17/42

    Decision: Using Nested if// Using Nested Selection Statements#include #include

    using namespace std;int main(){

    int x,y;cout > x ;

    cout > y ;if ( x > 5 ){if ( y > 5 )

    cout

  • 8/10/2019 Ch03 Loops and Decisions

    18/42

    Decision: Using the else if statement// demonstrates Ifelse #include #include using namespace std;int main(){

    int age;

    cout > age;if (age < 18)

    cout

  • 8/10/2019 Ch03 Loops and Decisions

    19/42

    Decision: Using compound Conditions// using compound statements #include #include using namespace std;int main(){

    int x,y; cout > x ;cout > y ;if (( x >= 5) &&( y >= 5))

    cout 5) &&( y < 5))

    cout = 5))

    cout

  • 8/10/2019 Ch03 Loops and Decisions

    20/42

    Decision: Using compound Conditions// using compound statements #include #include using namespace std;int main(){

    int n1,n2,n3;cout > n1 >> n2 >> n3;if (n1

  • 8/10/2019 Ch03 Loops and Decisions

    21/42

    Decision: Using compound Conditions// using compound statements

    #include #include

    using namespace std;

    int main(){

    char ans;cout > ans;

    if (ans == 'Y' || ans == 'y' )

    cout

  • 8/10/2019 Ch03 Loops and Decisions

    22/42

    Decision: Using the else if statement// This program converts a Marks into Grades#include

    #include using namespace std;int main(){ int score;

    cout > score;if (score>100) cout= 80) cout = 75) cout = 70) cout = 65) cout = 60) cout = 55) cout = 50) cout = 0) cout

  • 8/10/2019 Ch03 Loops and Decisions

    23/42

    Decision: Using the switch statement// demonstrates switch ... Case #include

    #include using namespace std;int main(){

    int x,y; char op;cout > x >> y;

    cout > op;switch (op){

    case '+': cout

  • 8/10/2019 Ch03 Loops and Decisions

    24/42

    Decision: Using the ? : statement// demonstrates ? :#include #include

    using namespace std;int main(){

    int m,n;cout > m >> n;

    cout

  • 8/10/2019 Ch03 Loops and Decisions

    25/42

    Loops Loops cause a section of your program to be

    repeated a certain number of times.The repetition continues while a condition istrue.

    When the condition becomes false, the loopends and control passes to the statementsfollowing the loop.

    There are three kinds of loops in C++:

    the for loop,the while loop,

    and the do...while loop.

  • 8/10/2019 Ch03 Loops and Decisions

    26/42

    while loop// Using Compound Statements#include #include using namespace std;int main(){

    int x,loop_count=0;

    cout > x ;while (loop_count

  • 8/10/2019 Ch03 Loops and Decisions

    27/42

    while loop// computes the sum 1 + 2 + 3 + + n,#include #include using namespace std;int main(){

    int last_no, sum = 0, current_no = 1;cout > last_no;cout

  • 8/10/2019 Ch03 Loops and Decisions

    28/42

    Using break to terminate a loop#include #include using namespace std;int main(){

    int last_no,sum=0, current_no=1;cout > last_no;

    cout last_no)break ; // terminates the loop immediately

    sum = sum + current_no;

    cout

  • 8/10/2019 Ch03 Loops and Decisions

    29/42

    Using break to terminate a loop// prints all the Fibonacci numbers up to an input limit:#include #include using namespace std;int main(){

    long bound;cout > bound;

    cout

  • 8/10/2019 Ch03 Loops and Decisions

    30/42

    Using dowhile loop#include #include using namespace std;int main(){

    unsigned char choice=0;do

    {cout>choice;

    }while (choice!='y' && (choice!='Y'));

    cout

  • 8/10/2019 Ch03 Loops and Decisions

    31/42

    Using dowhile loop#include #include using namespace std;int main(){

    long dividend, divisor;char ch;do // start of do loop

    { // do some processingcout > dividend;cout > divisor;cout

  • 8/10/2019 Ch03 Loops and Decisions

    32/42

    Using for loop#include #include using namespace std;int main(){

    int n;

    cout > n; long sum = 0;for ( int i=1; i

  • 8/10/2019 Ch03 Loops and Decisions

    33/42

    Using for loop#include #include #include using namespace std;int main(){

    int n, i=1;cout > n;for ( ; i

  • 8/10/2019 Ch03 Loops and Decisions

    34/42

    Using for loop// Calculating factorial#include #include using namespace std;int main(){

    unsigned long n, i, fact=1;cout > n;if ( n < 1 )

    exit(0);for ( i=1 ; i

  • 8/10/2019 Ch03 Loops and Decisions

    35/42

    Using for loop to calculate prime#include #include using namespace std;int main(){

    long n; cout > n;if (n < 2) cout

  • 8/10/2019 Ch03 Loops and Decisions

    36/42

    Finding Reverse using while loop// Writing in Reverse#include #include using namespace std;int main(){

    long m, d , n = 0;cout > m;while ( m > 0 ){

    d = m % 10; // d will be the right-most digit of mm /= 10; // then remove that digit from m

    n = 10*n + d; // and append that digit to n}cout

  • 8/10/2019 Ch03 Loops and Decisions

    37/42

    Using continue and break// Using Writing in Reverse#include #include using namespace std;int main(){

    int n;for (;;){

    cout > n;if (n%2 == 0)

    continue ; // Stay in Loopif (n%3 == 0)

    break ; // Come out of loopcout

  • 8/10/2019 Ch03 Loops and Decisions

    38/42

    Assignment #21. Write a program that prints the minimum

    of four input integers .2. Write a program that finds the median of

    three input integers .3. Write a program to find the min of 3

    numbers using ? : operator4. Using a while loop write a program to

    Compute a Sum of Reciprocals of first nnumbers This program computes the sum ofreciprocals s = 1 + 1/2 + 1/3 + 1/3 + + 1/n,

    5. Using nested for loops write a programto show all prime numbers in range given

    by user.6. Write a program using do..while loop to

    compute the sum of the first n squares , where n is input.

  • 8/10/2019 Ch03 Loops and Decisions

    39/42

    Assignment #21. Write a program to print out all

    Armstrong numbers between 1 and 500. Ifsum of cubes of each digit of the numberis equal to the number itself, then thenumber is called an Armstrong number.For example:153 = (1*1*1) + (5*5*5) + (3*3*3)

    Write a program to produce the following output usingloops:

    1 2 3 4 5 6 7 6 5 4 3 2 11 2 3 4 5 6 6 5 4 3 2 1

    1 2 3 4 5 5 4 3 2 11 2 3 4 4 3 2 11 2 3 3 2 11 2 2 11 1

  • 8/10/2019 Ch03 Loops and Decisions

    40/42

    Assignment #21. Write a program to produce the following

    output using for loop:- A

    A B A B C

    A B C D

    A B C D E A B C D E F A B C D E F

    A B C D E A B C D

    A B C A B

    A

  • 8/10/2019 Ch03 Loops and Decisions

    41/42

    Assignment #2Write a program to produce the following output using

    loop:- A A B

    A C A D

    A E A F A F

    A E

    A D A C

    A B A

  • 8/10/2019 Ch03 Loops and Decisions

    42/42

    Assignment #2 ** * *

    * * * * *

    * * * * * * ** * * * * * * * *

    * * * * * * * * * * ** * * * * * * * * * * * *

    * * * * * * * * * * * * * * ** * * * * * * * * * * * * * * * ** * * * * * * * * * * * * * * * *

    * * * * * * * * * * * * * * ** * * * * * * * * * * * *

    * * * * * * * * * * ** * * * * * * * *

    * * * * * * ** * * * *

    * * **

    Write a program to produce

    the following outputusing loop:-