|< C & C++ Program Controls 1 | Main | C & C++ Program Controls 3 >| Site Index | Download |


 

 

 

 

MODULE 6_1

THE C/C++ PROGRAM CONTROL/LOOP PROGRAMMING 2

 

 

 

 

 

 

 

 

 

My Training Period:  xx hours

 

The source code for this Module is: C/C++ loops source codes. The lab worksheets for your practice are: C/C++ program control repetition 1, C/C++ program control repetition 2, C/C++ program control selection 1, C/C++ program control selection 2 and C/C++ program control selection 3.

 

6.3.3           Selection-The switch-case-break Statement

  • The most flexible program control statement in selection structure of program control.

  • Enables the program to execute different statements based on an expression that can have more than two values.  Also called multiple choice statements.

  • Before this, such as if statement, were limited to evaluating an expression that could have only two values: TRUE or FALSE.

  • If more than two values, have to use nested if statements.

  • The switch statement makes such nesting unnecessary.

  • Used together with case and break.

  • The switch constructs has the following form:

switch(expression)

{

case  template_1  : statement(s);

break;

case  template_2  : statement(s);

break;

...................

...................

case  template_n : statement(s);

break;

default : statement(s);

}

next_statement;

  • Evaluates the (expression) and compares its value with the templates following each case label.

  1. If a match is found between (expression) and one of the templates, execution is transferred to the statement(s) that follows the case label.

  2. If no match is found, execution is transferred to the statement(s) following the optional default label.

  3. If no match is found and there is no default label, execution passes to the first statement following the switch statement closing brace, the next_statement.

  4. To ensure that only the statements associated with the matching template are executed, include a break statement where needed, which terminates the entire switch statement.

  5. As usual the statement(s) can also be a block of code put in curly braces.

  • Another program example:

#include <iostream>

using namespace std;

 

int main()

{

    char   selection;

    cout<<"\n Menu";

    cout<<"\n========";

    cout<<"\n  A - Append";

    cout<<"\n  M - Modify";

    cout<<"\n  D - Delete";

    cout<<"\n  X - Exit";

    cout<<"\n Enter selection: ";

    cin>>selection;

     switch(selection)

{

    case 'A' : {cout<<"\n To append a record\n";}

               break;

    case 'M' : {cout<<"\n To modify a record";}

              break;

    case 'D' : {cout<<"\n To delete a record";}

              break;

    case 'X' : {cout<<"\n To exit the menu";}

              break;

    // other than A, M, D and X...

    default : cout<<"\n  Invalid selection";

    // no break in the default case

  }

  cout<<"\n";

  return 0;

}

 

Output:

 

C C++ program control switch-case-break statement selection

 

switch(selection)

{

case 'A' :

case 'M' :

case 'D' : cout<<"\n To Update a file";

              break;

case 'X' : cout<<"\n To exit the menu";

              break;

default  : cout<<"\n Invalid selection";

}

next_statement;

cin>>choice;

switch(choice)

{

    case  1  :  cout<<"\n  Value of choice = 1";

       break;

    case  2  :  cout<<"\n  Value  of choice = 2";

    case  3  :  cout<<"\n  Value of choice = 3";

       break;

    default  :  cout<<"\n  Wrong choice";

}

printf("...");

  1. The switch permits the execution of more than one alternative (by not placing break statements) whereas the if statement does not.  In other words, alternatives in an if statement are mutually exclusive whereas they may or may not be in the case of a switch.

  2. A switch can only perform equality tests involving integer (or character) constants, whereas the if statement allows more general comparison involving other data types as well.

C C++ loops program control if and if-else variation statement single multiple selection

 

C++ loop program control switch-case-break statement selection

 

6.3.4  The for Statement – Repetition Control Structure, Iteration

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

--------------------------------------------------------------------------------

C++ Program control for statement repetition loop

 

  1. The expression initial_value1 is evaluated, usually an assignment statement that sets a variable to a particular value.

  2. The expression condition is evaluated.  It is typically a relational expression.

  3. If condition evaluates as false (zero), the for statement terminates and execution passes to the first statement following the for statement that is the next_statement.

  4. If condition evaluates as true (non zero), the subsequent C/C++ statements are executed, in this case the statement(s).

  5. The expression increment is executed, and execution returns to step no. 2.

 

C++ program control for statement repetition loop flow chart

// a simple for statement

#include <stdio.h>

 

void main()

{

       int count;

       // display the numbers 1 through 10

       for(count = 1; count <= 10; count++)

         printf("%d   ", count);

       printf("\n");

}

 

Output:

 

C++ program control for statement repetition loop simple example

C++ program control for statement repetition loop the flow chart

for(count = 100; count > 0; count--)

for(count = 0; count < 1000; count += 3)

count=1;

for( ; count < 1000; count++)

count=1;

for(printf("Now sorting the array…"); count < 1000; count++)

for(counter=0; counter < 100; )

    printf("%d", counter++);

for(count =0; count < 1000 && name[count] != 0; count++)

printf("%d", name[count]);

for(count = 0; count < 1000 && list[count];)

printf("%d", list[count++]);

for(count = 0; count < 20000; count++)

;

b[0], b[1], b[2],… and  a[999], a[998], a[997],… and so on, the coding is

 

for(i = 0, j = 999; i < 1000; i++, j--)

    b[j] = a[i];

sum = 0;

for(i = 1; i <=20; i++)

    sum = sum + i;

cout<<"\n Sum of the first 20 natural numbers = ";

cout<<sum;

for(i = 1, sum = 0; i <= 20; i++)

    sum = sum + i;

cout<<"\nSum of the first 20 natural numbers = "<<sum;

for(i = 2, sum=0, sum2 = 0; i <= 20; i = i + 2)

{

    sum = sum + i;

    sum2 = sum2 + i*i;

}

  cout<<"\nSum of the first 20 even natural numbers=";

  cout<<sum<<"\n";

  cout<<"sum of the squares of first 20 even natural numbers=";

  cout<<sum2;

for( ; ; )

    cout<<"\n This is an infinite loop";

for( ; 1 ; )

    cout<<"\n This is an infinite loop";

// program to show the nested loops

#include <iostream>

using namespace std;

 

int main()

{

       // variables for counter…

       int   i, j;

       // outer loop, execute this first...

       for(i=1; i<11; i++)

       {

              cout<<"\n"<<i;

              // then...execute inner loop with loop index j

              // the initial value of j is i + 1

              for(j=i+1; j<11; j++)

              // display the result…         

              cout<<j;

              // increment counter by 1 for inner loop…

       }

       // increment counter by 1 for outer loop…

       cout<<"\n";

       return 0;

}

 

Output:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

C++ program control for statement printing pattern

/* nesting two for statements */

#include <stdio.h>

 

// function prototype

void  DrawBox(int, int);

 

void  main()

{

    // row = 10, column = 25...

    DrawBox(10, 25);

}

 

void  DrawBox(int row, int column)

{

   int  col;

   // row, execute outer for loop...

   // start with the preset value and decrement until 1

   for( ; row > 0; row--)

   {

          // column, execute inner loop...

          // start with preset col, decrement until 1

          for(col = column; col > 0; col--)

          // print #....

          printf("#");

          // decrement by 1 for inner loop...

          // go to new line for new row...

          printf("\n");

   }

   // decrement by 1 for outer loop...repeats

}

 

Output:

 

C++ program control for statement printing pattern example

 

C & C++ programming tutorials

 

 

 

 

 

 

 

 

 

 

 

Further reading and digging:

 

  1. Check the best selling C / C++ books at Amazon.com.
  2. The source code for this Module is: C/C++ loops source codes.
  3. The lab worksheets for your practice are: C/C++ program control repetition 1, C/C++ program control repetition 2, C/C++ program control selection 1, C/C++ program control selection 2 and C/C++ program control selection 3.

 

 

 

 

 

|< C & C++ Program Controls 1 | Main | C & C++ Program Controls 3 >| Site Index | Download |


C Formatted Input/Output:  Part 1 | Part 2 | Part 3 | Part 4 |