the various control structures in C

Control structures are the statements which controls the overall any program. Control structures are basically of three types –

  • Sequence statements
  • Iterative statements
  • Selection statements

Sequence Statements : All the State in a program except the iterative & statements. They are generally the individual statements which performs the task of input, output, assignment declaration etc.

Iterative Statement are those repeated execution of a particular set of instructions desired number of times. These statements are generally called loops for their execution nature.  Loops are basically of two types –

    • Count Loop
    • Event Loop

      Count Loop :- Those loops whose no. of times of execution is known prior to the execution of loop are termed as count loops i.e they care countable in nature.

      Event Loop :- Those loop whose execution any certain event are called event before the execution.
      A loop basically consists of a loop variable which has for the completion of any iterative   statement, i.e a loop consists of three main statements:-

  1.  

    (1) Initialization (2) Condition (3) Incrementation

Initialization is used to start any loop i.e. it gives some initial value to the loop variable. Which defines that from which value the loop has to get start. Condition is provided to give the final value to the loop variable so that how many times the loop has to get executed. For reaching the loop variable from initial value to the final value there should be some sort of increamentation and that provided by the third component statement of loop and that is incrementation.

If in any loop the incramentation or the final value is not provided then the loop becomes infinite. If the initialization is not done then the garbage value of the loop variable becomes its initial value.

In C language the iterative statements (loops) can be implemented in the three loops and they are

The for loop

Syntax of For Loop –
for (initialization; condition; incrementation)
{ —————– body of loop ————-
}

For Loop will perform its execution until the condition remains satisfied. If the body of the loop consists of more than one statement then these statements are made compound by placing the open and closed curly brackets around the body of the loop. For loop is a count loop. The initialization condition and increamentation may be done in the same statement. For loop will not execute at least once also if the condition is false at the first time itself.

With loops you also have to put commands between curly brackets if there is more than one of them. Here is the solution to the problem that we had with the 24 printf commands:

 

Code: 

#include 
int main()
{
int i;
for (i = 1;i <= 24;i++)
printf("H\n");
return 0;
}

 

A for loop is made up of 3 parts inside its brackets which are separated by semi-colons. The first part initializes the loop variable. The loop variable controls and counts the number of times a loop runs. In the example the loop variable is called i and is initialized to 1. The second part of the for loop is the condition a loop must meet to keep running. In the example the loop will run while i is less than or equal to 24 or in other words it will run 24 times. The third part is the loop variable incremental. In the example i++ has been used which is the same as saying i = i + 1. This is called incrementing. Each time the loop runs i has 1 added to it. It is also possible to use i– to subtract 1 from a variable in which case it’s called decrementing.

The while loop

Syntax –
Initialization;
While (condition)
{
Body of loop;
Incrementation;
}

In this loop, initialisation, condition and incrementation is done in the three different statements. This loops is count as well as event loop. In case of while loops the body of the loop will consist of more than one statements because each time one statement will be of incrementation. Hence the open and closed curly brackets are required.

 

Code: 

#include<stdio.h> 
int main()
{
int i,times;
scanf("%d",&times);
i = 0;
while (i <= times)
{
i++;
printf("%d\n",i);
}
return 0;
}

 

The do while loop

The third loop statement available in C is do-while statement syntax :-
Initialization;
Do
{
Body of loop;
Increamentation;
}
While (condition);

In this loop the condition is checked at the end, and for this reason this loop will execute at least once whether the condition may be satisfying and unsatisfying.

 

Code: 

#include<stdio.h> 
int main()
{
int i,times;
scanf("%d",&times);
i = 0;
do
{
i++;
printf("%d\n",i);
}
while (i <= times);
return 0;
}

 

Break and continue

You can exit out of a loop at any time using the break statement. This is useful when you want a loop to stop running because a condition has been met other than the loop end condition.

 

Code: 

#include<stdio.h> 
int main()
{
int i;
while (i < 10)
{
i++;
if (i == 5)
break;
}
return 0;
}

 

You can use continue to skip the rest of the current loop and start from the top again while incrementing the loop variable again. The following example will never print “Hello” because of the continue.

 

Code: 

#include<stdio.h> 
int main()
{
int i;
while (i < 10)
{
i++;
continue;
printf("Hello\n");
}
return 0;
}

 

No tags Hide

● Every application has at least one thread — orseveral, if you count “system” threads that dothings like memory management and signal handling

● But from the application programmer’s point ofview, you start with just one thread, called the mainthread. This thread has the ability to createadditional thread

THREAD STATES

● A thread can in one of several possible states:

1.Running

● Currently running

●In control of CPU

2.Ready to run

● Can run but not yet given the chance

3.Resumed

● Ready to run after being suspended or block

4.Suspended

● Voluntarily allowed other threads to run

5.Blocked

● Waiting for some resource or event to occur

THREAD PRIORITY

Why priorities?– Determine which thread receives CPU control and getsto be execute

● Definition:– Integer value ranging from 1 to 10– Higher the thread priority → larger chance of beingexecuted first–

Example:

● Two threads are ready to run

● First thread: priority of 5, already running

● Second thread = priority of 10, comes in while first threadis running

More than one highest priority thread that is readyto run– Deciding which receives CPU control depends on the operating system– Windows 95/98/NT: Uses time-sliced round-robin– Solaris: Executing thread should voluntarily relinquish CPU control

maximum priority of thread is  10

minimum priority of thread is 1

default priority of thread is 5

methods of thread:-

getName(),setName(),currentThread(),getPriority()

two way to creating and starting a thread:-

1.Extending the Thread class

2.Implementing the Runnable interface

● The subclass extends Thread class– The subclass overrides the run() method of Thread class

● An object instance of the subclass can then becreated

● Calling the start() method of the object instancestarts the execution of the thread– Java runtime starts the execution of the thread bycalling run() method of object instance.

Two Schemes of starting a threadfrom a subclass

1.The start() method is not in the constructor of thesubclass– The start() method needs to be explicitly invoked afterobject instance of the subclass is created in order to startthe thread

2.The start() method is in the constructor of thesubclass– Creating an object instance of the subclass will start the thread.

class PrintNameThread extends Thread {

2 PrintNameThread(String name) {

3 super(name);

4 }

5 public void run() {

6 String name = getName();

7 for (int i = 0; i < 100; i++) {

8 System.out.print(name);

9 }

10 }

11 }

using RUNNABLE INTERFACE

The Runnable interface should be implemented byany class whose instances are intended to beexecuted as a thread

● The class must define run() method of noarguments– The run() method is like main() for the new thread

● Provides the means for a class to be active whilenot subclassing Thread– A class that implements Runnable can run withoutsubclassing Thread by instantiating a Thread instanceand passing itself in as the target

1.Caller thread creates Thread object and starts itexplicitly after an object instance of the class thatimplements Runnable interface is created– The start() method of the Thread object needs to beexplicitly invoked after object instance is created

2.The Thread object is created and started within theconstructor method of the class that implementsRunnable interface– The caller thread just needs to create object instances ofthe Runnable class

class PrintNameRunnable implements Runnable {

String name;

PrintNameRunnable(String name) {

this.name = name;

}

// Implementation of the run() defined in the

// Runnable interface.

public void run() {

for (int i = 0; i < 10; i++) {

System.out.print(name);}}}



No tags Hide

Constructor and Destructor Order

The process of creating and deleting objects in C++ is not a trivial task. Every time an instance of a class  is created the constructor method is called. The constructor has the same name as the class and it doesn’t return any type, while the destructor’s name it’s defined in the same way, but with a ‘~’ in front:

 

class String
{

public:

String() //constructor with no arguments
    :str(NULL),
    size(0)
{

}

String(int size) //constructor with one argument
    :str(NULL),
    size(size)

{
    str = new char[size];
}

~String() //destructor
{
    delete [] str;
};

private:

    char *str;

    int size;

}
Even if a class is not equipped with a constructor,
the compiler will generate code for one, called the
implicit default constructor. This will typically
call the default constructors for all class members,
if the class is using virtual methods it is used to
initialize the pointer to the virtual table, and,
in class hierarchies, it calls the constructors of
the base classes.


 

 

No tags Hide

All the parts we have reviewed so far are usually required for the computer to function. Some other parts, not required, can also be connected to the computer to complement it. A peripheral is an object attached to the computer to help it perform some necessary assignments none of the other parts can handle. In most scenarios, no peripheral is required but nowadays, it is unusual for a computer not to have any peripheral at all. The most used peripherals are the printer, a digital camera, a scanner, a projector, an external drive (such as an external CD burner for an old computer or an external hard drive), etc.

No tags Hide

A sequence is a database item that generates a sequence of integers.
You typically use the integers generated by a sequence to populate a numeric primary key column.
You create a sequence using the CREATE SEQUENCE statement.

syntax:-

CREATE SEQUENCE sequence_name
[START WITH start_num]
[INCREMENT BY increment_num]
[ { MAXVALUE maximum_num | NOMAXVALUE } ]
[ { MINVALUE minimum_num | NOMINVALUE } ]
[ { CYCLE | NOCYCLE } ]
[ { CACHE cache_num | NOCACHE } ]
[ { ORDER | NOORDER } ];

 

  1. The default start_num is 1.
  2. The default increment number is 1.
  3. The absolute value of increment_num must be less than the difference between maximum_num and minimum_num.
  4. minimum_num must be less than or equal to start_num, and minimum_num must be less than maximum_num.
  5. NOMINVALUE specifies the maximum is 1 for an ascending sequence or -10^26 for a descending sequence.
  6. NOMINVALUE is the default.
  7. maximum_num must be greater than or equal to start_num, and maximum_num must be greater than minimum_num.
  8. NOMAXVALUE specifies the maximum is 10^27 for an ascending sequence or C1 for a descending sequence.
  9. NOMAXVALUE is the default.
  10. CYCLE specifies the sequence generates integers even after reaching its maximum or minimum value.
  11. When an ascending sequence reaches its maximum value, the next value generated is the minimum.
  12. When a descending sequence reaches its minimum value, the next value generated is the maximum.
  13. NOCYCLE specifies the sequence cannot generate any more integers after reaching its maximum or minimum value.
  14. NOCYCLE is the default.
  15. CACHE cache_num specifies the number of integers to keep in memory.
  16. The default number of integers to cache is 20.
  17. The minimum number of integers that may be cached is 2.
  18. The maximum integers that may be cached is determined by the formula CEIL(maximum_num – minimum_num)/ABS(increment_num).
  19. NOCACHE specifies no integers are to be stored.
  20. ORDER guarantees the integers are generated in the order of the request.
  21. You typically use ORDER when using Real Application Clusters.
  22. NOORDER doesn’t guarantee the integers are generated in the order of the request.
  23. NOORDER is the default.

 

No tags Hide

Next »

Managed by

MNP INFOTECH