C Programing
C Programing
Decision-making :--
Decision-making statements in programming languages decide the direction of the flow of
program execution.
Decision-making statements available in C or C++ are:
if statement is the most simple decision-making statement. It is used to decide whether a certain
statement or block of statements will be executed or not .
So if a certain condition is true then a block of statement is executed otherwise not.
Flow chart –
praveen panchal
Syntax:
if(condition)
{
// Statements to execute if
// condition is true
}
EXAMPLE-
If-else in C/C++
The if statement alone tells us that if a condition is true it will execute a block of statements and
if the condition is false it won’t. But what if we want to do something else if the condition is false.
Here comes the C else statement. We can use the else statement with if statement to execute a
block of code when the condition is false.
Syntax:
If (condition)
{
// Executes this block if
// condition is true
}
Else
{
praveen panchal
// Executes this block if
// condition is false
}
Flowchart:
Syntax -
if (condition)
( statement);
else if (condition)
( statement);
else
praveen panchal
( statement);
praveen panchal
Nested-if in C/C++
A nested if in C is an if statement that is the target of another if statement. Nested if statements
mean an if statement inside another if statement.
if (condition1)
if (condition2)
Flowchart
praveen panchal
Switch statement --
The switch case statement is used when we have multiple options and we need to perform a
different task for each option.
Following are the rules for Switch statement:
1. Switch case should have at most one default label.
2. Default case is optional.
3. Case labels must be unique, end with colon, integral type and have constant expression.
4. Break statement takes control out of the switch and two or more cases may share one break
statement.
5. Relational operators are not allowed in Switch statement.
6. Macro identifier and Const variable are allowed in switch case statement.
7. Empty switch case is allowed.
8. Nesting switch is allowed.
9. Default case can be placed anywhere in the Switch statement.
syntax
Flowchart- Example-
goto: The goto statement in C/C++ also referred to as unconditional jump statement can be used
to jump from one point to another within a function.
Syntax:
Syntax1 | Syntax2
----------------------------
praveen panchal
goto label; | label:
. | .
. | .
. | .
label: | goto label;
Flowchart-
Example-
Looping
praveen panchal
Looping statements are used to execute a statement or group of statements
repeatedly for a specified number of times or as long as the condition is true/ until the
condition is false.
A loop is a particular area of a program where some executable statements are
written which gets executed by testing one or more conditions. So, in looping, a sequence
of statements is executed until some conditions for termination are satisfied.
A program loop therefore consists of two segments; the body of the loop and the control
statement.
1. „body of the loop‟ consists of set of statements and the other
2. „Control statement‟ .The control statement tests certain conditions and then directs the
repeated
execution of the statements contained in the body of the loop.
Depending on the position of the control statement in the loop, a control structure can be
classified into two types; entry controlled and exit controlled.
1. Entry-controlled loops:-In Entry controlled loop the test condition is checked first
and if that condition is true than the block of statement in the loop body will be
executed
Example: while loop and for loop.
2. Exit controlled loop: -In exit controlled loop the body of loop will be executed first
and at the end the test condition is checked, if condition is satisfied then body of loop
will be executed again.
praveen panchal
Example:do-whileloop.
praveen panchal
Initialization Expression: In this expression we have to initialize the loop counter
to some value. for example: int i=1;
Test Expression: In this expression we have to test the condition. If the condition
evaluates to true then we will execute the body of loop and go to update
expression otherwise we will exit from the for loop. For example: i <= 10;
Update Expression: After executing loop body this expression
increments/decrements the loop variable by some value. for example: i++;
flowchart- Example:-
2) While Loop
praveen panchal
While studying for loop we have seen that the number of iterations is known beforehand,
i.e. the number of times the loop body is needed to be executed is known to us. while loops
are used in situations where we do not know the exact number of iterations of loop
beforehand. The loop execution is terminated on the basis of test condition.
Syntax:
We have already stated that a loop is mainly consisted of three statements – initialization
expression, test expression, update expression.
The syntax of the three loops – For, while and do while mainly differs on the placement of
these three statements.
initialization expression;
while (test_expression)
{
// statements
update_expression; }
Flowchart-
Example-
Break
It is a keyword which is used to terminate the loop (or) exit from the
block.
The control jumps to next statement after the loop (or) block.
Break is used with for, while, do-while and switch statement.
When break is used in nested loops then, only the innermost loop is
terminated.
The syntax for break statement is as follows –
praveen panchal
continue
The syntax for the continue statement is as follows −
return
It terminates the execution of function and returns the control of calling function
The syntax for return statement is as follows −
praveen panchal
NESTED LOOP- C programming allows to use one loop inside another loop.
That is why nested loops are also called as “loop inside loop“.
praveen panchal
Jumping loops --
In C++ there is four jump statement: continue, break, return, and
goto.
Continue: It is used to execute other parts of the loop while
skipping some parts declared inside the condition, rather than
terminating the loop, it continues to execute the next iteration of
the same loop.
praveen panchal