Showing posts with label statement. Show all posts
Showing posts with label statement. Show all posts

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. 
 

Thursday, 22 September 2016

Decision making in C Programming

C  programming  language  assumes  any non-zero and non-null values  as true, and if it is  either zero or null, then it is assumed as false value.C  programming  language  provides  the  following  types  of  decision-making statements.

  •   if Statement 

An if statement  consists  of  a  Boolean  expression  followed  by  one  or  more statements.









  • if…else Statement 

An if statement  can  be  followed  by  an  optional else statement, which  executes when the Boolean expression is false.
Syntax 
The syntax of an if...else statement in C programming language is:





  •  if...else if...else Statement 

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.
Syntax 

  •  Nested if Statements 

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).
Syntax 
The syntax for a nested if statement is as follows:

                              

  •  Switch Statement 

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.
Syntax 
The syntax for a switch statement in C programming language is as follows: