+ All Categories
Home > Documents > Control Statementss

Control Statementss

Date post: 14-Apr-2018
Category:
Upload: er-ashish-baheti
View: 216 times
Download: 0 times
Share this document with a friend

of 36

Transcript
  • 7/29/2019 Control Statementss

    1/36

  • 7/29/2019 Control Statementss

    2/36

    Three control structures

    selection/decision structures:- checks the

    given condition and then execute its sub-

    block.

    if, ifelse,nested if, ladder if, switch

    Repetition/loop structures:- statements

    which are repeatedly executed for certain no

    of times.

    while, dowhile, for

    Jumping statements:-goto,break,continue

  • 7/29/2019 Control Statementss

    3/36

    A simple ifstructure is called a single-selectionstructure because it either selects or ignores asingle action.

    The if / else structure is called a double-selection structure because it selects betweentwo different actions.

    Nested if / else structures test for multiple cases

    by placing if / else structures inside other if /else structures.

    The ladder if /else ifstructure test for multiplecases by placing different conditions in else if.

  • 7/29/2019 Control Statementss

    4/36

    Syntax:- if(condition)

    {

    ------------------;

    -------------------;

    }

  • 7/29/2019 Control Statementss

    5/36

    include

    #include

    void main()

    {

    int age;

    clrscr();

    Coutage;

    if(age>17){

    Cout

  • 7/29/2019 Control Statementss

    6/36

    Syntax:- if(condition){

    -----------------;

    -------------------;

    }

    else

    {

    -----------------;

    -------------------;}

  • 7/29/2019 Control Statementss

    7/36

    #include

    #include

    void main(){

    int age;

    clrscr();

    Coutage;

    if(age>17)

    {

    Cout

  • 7/29/2019 Control Statementss

    8/36

    Syntax:- if(condition)

    {

    If(){

    }

    Else

    {

    }

    }

    else

    {

    If()

    {

    }

    Else

    {

    }

    }

  • 7/29/2019 Control Statementss

    9/36

    Void main()

    {

    Int a=10,b=20,c=30;

    If(a>b)

    {

    If(a>c)

    {

    cout

  • 7/29/2019 Control Statementss

    10/36

    Syntax:-

    if (cond1)

    {

    statement1;

    }

    else if (cond2)

    {

    statement2;

    }

    else if (cond n)

    {statement n;

    }

    else

    statement;

  • 7/29/2019 Control Statementss

    11/36

  • 7/29/2019 Control Statementss

    12/36

    if ( studentGrade >= 90 )

    cout = 80 )cout = 70 )

    cout = 60 )

    cout

  • 7/29/2019 Control Statementss

    13/36

    Compound statement Also called a block Set of statements within a pair of braces Used to include multiple statements in an if body

    Example if ( studentGrade >= 60 )

    cout

  • 7/29/2019 Control Statementss

    14/36

    Similar to if statements

    Can list any number of branches

    Used in place of nested if statements

    Avoids confusion of deeply nested ifsMultiple way branch statement

    Used for multiple selections

    Tests a variable or expression

    Compared against constant integral expressions to

    decide on action to take

  • 7/29/2019 Control Statementss

    15/36

    Syntax:-switch (expression) {

    case value1:statement1;

    break;case value2:

    statement2;break;

    case valuen:statementn;break;

    default:statement;}

  • 7/29/2019 Control Statementss

    16/36

    switch statement

    Controlling expression

    Expression in parentheses after keyword switch

    case labels Compared with the controlling expression

    Statements following the matching case label areexecuted

    Braces are not necessary around multiple statements in a

    case label A break statements causes execution to proceed with the

    first statement after the switch

    Without a break statement, execution will fall throughto the next case label

  • 7/29/2019 Control Statementss

    17/36

    switch statement (Cont.)

    default case

    Executes if no matching case label is found

    Is optional

    If no match and no default case

    Control simply continues after the switch

  • 7/29/2019 Control Statementss

    18/36

    Char grade;

    Cin>>grade;

    switch (grade)

    {

    case A:

    cout

  • 7/29/2019 Control Statementss

    19/36

    * * * * Menu * * * *

    1. add

    2. mul3. div

    4. sub

    Choose either 1, 2, 3 or 4:

  • 7/29/2019 Control Statementss

    20/36

    Int ch,a=10,b=20;cin>>ch;

    switch (ch)

    {

    case 1:

    cout

  • 7/29/2019 Control Statementss

    21/36

    While

    Do while

    for

  • 7/29/2019 Control Statementss

    22/36

    while loop repeats until condition becomesfalse

    int count =1;

    int total = 0;while (count

  • 7/29/2019 Control Statementss

    23/36

    dowhilestatement Similar to while statement

    Tests loop-continuation after performing bodyof loop Loop body always executes at least once

    Code: x=10;

    do

    {

    cout

  • 7/29/2019 Control Statementss

    24/36

    for repetition statement

    Specifies counter-controlled repetition details in

    a single line of code

  • 7/29/2019 Control Statementss

    25/36

    Not Valid:

    X for (j = 0, j < n, j = j + 3) // semicolons needed

    for (j = 0; j < n) // three parts needed

  • 7/29/2019 Control Statementss

    26/36

    for statement examples

    Vary control variable from 1 to 100 in increments of1

    for ( int i = 1; i = 1; i-- )

    Vary control variable from 7 to 77 in steps of7

    for( int i = 7; i = 2; i -= 2 )

    Vary control variable over the sequence: 2, 5, 8, 11,14, 17, 20

    for( int i = 2; i = 0; i -= 11 )

  • 7/29/2019 Control Statementss

    27/36

    Using a comma-separated list of

    expressions

    for ( int number = 2;number

  • 7/29/2019 Control Statementss

    28/36

    Example 3: j = 1;sum = 0;for ( ; ; ){ sum = sum + j; j++;

    printf"\n"

  • 7/29/2019 Control Statementss

    29/36

    General form of the for statement

    for ( initialization; loopContinuationCondition;increment)

    statement;

    Can usually be rewritten as: initialization;while ( loopContinuationCondition)

    {statement;

    increment;}

    If the control variable is declared in the initialization

    expression

    It will be unknown outside the for statement

  • 7/29/2019 Control Statementss

    30/36

    break statement

    Causes immediate exit from the loop

    Used in while, for, dowhile or switchstatements

    continue statement

    Skips remaining statements in loop body and

    start from the beginning of loop

    Used in for loop, while , dowhile loops

  • 7/29/2019 Control Statementss

    31/36

    void main ( ){

    int k;for ( k= -5; k < 25; k= k+5){

    cout

  • 7/29/2019 Control Statementss

    32/36

    void main ( )

    {

    int k;

    for ( k= -5; k < 25; k= k+5)

    {

    cout

  • 7/29/2019 Control Statementss

    33/36

    Void main ( ){

    int k;

    for ( k= -5; k < 25; k= k+5)

    {

    cout

  • 7/29/2019 Control Statementss

    34/36

    int j =50;while (j < 80)

    {j += 10;if (j == 70)break;

    cout

  • 7/29/2019 Control Statementss

    35/36

    #include

    #include int main() {

    int n = 0;loop:

    cout

  • 7/29/2019 Control Statementss

    36/36

    It is used to return the value to the calling

    function.

    It can be used in fallowing ways

    a) return(expression) return(a+b);b) A function may use one or more return

    statement depending upon condition

    if(a>b)

    return(a);

    else

    return(b);


Recommended