0% found this document useful (0 votes)
76 views

C Programing

Decision making statements in programming languages determine the flow of a program's execution. The main decision making statements in C/C++ are if, if-else, if-else-if ladder, nested if, switch, and goto. Loops like for, while, do-while allow repeating execution of code. Break and continue statements can change a loop's flow by skipping iterations or exiting the loop early. Functions use return statements to exit and pass back values.

Uploaded by

shreya Chauhan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views

C Programing

Decision making statements in programming languages determine the flow of a program's execution. The main decision making statements in C/C++ are if, if-else, if-else-if ladder, nested if, switch, and goto. Loops like for, while, do-while allow repeating execution of code. Break and continue statements can change a loop's flow by skipping iterations or exiting the loop early. Functions use return statements to exit and pass back values.

Uploaded by

shreya Chauhan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

UNIT-2

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:

if-else-if ladder in C/C++ :-


Here, a user can decide among multiple options. The C if statements are executed from the top
down. As soon as one of the conditions controlling the if is true, the statement associated with that
if is executed, and the rest of the C else-if ladder is bypassed. If none of the conditions are true,
then the final else statement will be executed. 

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)

// Executes when condition1 is true

if (condition2)

// Executes when condition2 is true

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.

C programming language provides three constructs for performing loop operations.


1. The for loop
2. The while loop
3. The do while loop
1)for Loop
A for loop is a repetition control structure which allows us to write a loop that is executed a
specific number of times. The loop enables us to perform n number of steps together in one
line.
Syntax:
for (initialization expr; test expr; update expr)
{
// body of the loop
// statements we want to execute
}
In for loop, a loop variable is used to control the loop.
First initialize this loop variable to some value, then check whether this variable is less than
or greater than counter value.
If statement is true, then loop body is executed and loop variable gets updated .
Steps are repeated till exit condition comes.

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-

3)do while loop


praveen panchal
 In do while loops also the loop execution is terminated on the basis of test condition.
 The main difference between do while loop and while loop is in do while loop the
condition is tested at the end of loop body,
i.e do while loop is exit controlled whereas the other two loops are entry controlled loops.
Note: In do while loop the loop body will execute at least once irrespective of test condition.

C programming language allows jumping from one statement to another. It also


supports break, continue, return and go to jump statements.

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.

Explain statemens of continue, break, return, and goto as I Expalin


above.

praveen panchal

You might also like