Showing posts with label loops. Show all posts
Showing posts with label loops. Show all posts

Tuesday, 25 October 2016

Arrays Types in C Programming

Arrays are important to C and should need a lot more attention.

Multidimensional Arrays

C programming language allows multidimensional arrays.
Example:- 

Two-Dimensional Array

The simplest form of multidimensional array is the two-dimensional array. A two-dimensional array is, in essence, a list of one-dimensional arrays.
A two-dimensional array a, which contains three rows and four columns.

Initialized Two-Dimensional Array 

Multidimensional arrays may be initialized by specifying bracketed values for each row.
The nested braces, which indicate the intended row, are optional.

Accessed Two-Dimensional Array Elements 

An element in a two-dimensional array is accessed by using the subscripts, i.e., row index and column index of the array.
Example :- 
 

Saturday, 1 October 2016

Loop Control Statements in C Programming

Loop Control Statements  

Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed.

The break statement in C programming has the following two usages:

  • When a break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop.
  • It can be used to terminate a case in the switch statement.

If you are using nested loops, the break statement will stop the execution of the innermost loop and start executing the next line of code after the block.

Syntax
 Flow chart
 Example
Continue Statement

The continue statement in C programming works somewhat like the break statement.
Instead of forcing termination, it forces the next iteration of the loop to take place,skipping any code in between.

For the for loop, continue statement causes the conditional test and increment portions of the loop to execute. For the while and do...while loops, continue statement causes the program control to pass to the conditional tests.

Syntax 
 Flow chart

Goto statements:-

A goto statement in C programming provides an unconditional jump from the ‘goto’ to a labeled statement in the same function.

Syntax

Infinite loop

A loop becomes an infinite loop if a condition never becomes false. The for loop is traditionally used for this purpose. Since none of the three expressions that form the ‘for’ loop are required, you can make an endless loop by leaving the conditional expression empty. 
 

Saturday, 24 September 2016

Loops Types in C Programming

A loop statement allows us to execute a statement or group of statements multiple times.
C programming language provides the following types of loops to handle looping requirements.
  •  while Loop

A while loop in C programming repeatedly executes a target statement as long as a given condition is true.
Syntax
The syntax of a while loop in C programming language is:



  • FOR LOOP

A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.
Syntax
The syntax of a for loop in C programming language is:


  •  do…while Loop

Unlike for and while loops, which test the loop condition at the top of the loop, the do...while loop in C programming checks its condition at the bottom of the loop.
A do...while loop is similar to a while loop, except the fact that it is guaranteed to execute at least one time.

Syntax
The syntax of a do...while loop in C programming language is:



  •  

  •  Nested Loops

C programming allows to use one loop inside another loop. The following section shows a few examples to illustrate the concept.
Syntax
The syntax for a nested for loop statement in C is as follows:

 The syntax for a nested while loop statement in C programming language is as follows:

           The syntax for a nested do...while loop statement in C programming language is as follows:

Sunday, 28 August 2016

Loops and Conditions Statements in C programming

An if statement consists of a Boolean expression followed by one or more statements. 
An if statement can be followed by an optional else statement, which executes when the Boolean expression is false. An if statement can be followed by an optional else if...else statement, which is very useful to test various conditions using single if...else if statement.

It is always legal in C programming to nest if-else statements, which means you can use one if or else if statement inside another if or else if statement(s).
A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case.


A while loop in C programming repeatedly executes a target statement as long as a given condition is true.
Unlike for and while loops, which test the loop condition at the top of the loop, the do...while loop in C programming checks its condition at the bottom of the loop. A do...while loop is similar to a while loop, except the fact that it is guaranteed to execute at least one time.