Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

Structured Programming YIBS Lesson 4

The document covers branching and looping structures in C programming, explaining various conditional statements such as if, if-else, nested if-else, if else-if, and switch statements. It also details looping structures including entry-controlled loops (for and while loops) and exit-controlled loops (do-while loop), along with loop control statements like break and continue. Additionally, it provides assignments for practical implementation of these concepts.

Uploaded by

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

Structured Programming YIBS Lesson 4

The document covers branching and looping structures in C programming, explaining various conditional statements such as if, if-else, nested if-else, if else-if, and switch statements. It also details looping structures including entry-controlled loops (for and while loops) and exit-controlled loops (do-while loop), along with loop control statements like break and continue. Additionally, it provides assignments for practical implementation of these concepts.

Uploaded by

terasapcommunity
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

STRUCTURED PROGRAMMING

4 Branching and Looping Structures


4.1 Branching Structures
Also called condi onal statements or decision control structures, they are used for decision-making
purposes in C programs. They are used to evaluate one or more condi ons and make the decision
whether to execute a set of statements or not. These decision-making statements in programming
languages decide the direc on of the flow of program execu on.
Types of Condi onal Statements in C
4.1.1 if
The if statement is the simplest decision-making statement. It is used to decide whether a certain
statement or block of statements will be executed or not i.e. if a certain condi on is true then a block
of statements is executed otherwise not.
Syntax

If the condi on is true, then execute the statement. If we do not provide the curly braces a er the
if(condi on), then by default the if statement will consider the first statement immediately below to
be inside its block.
Example

4.1.2 if-else
The if statement alone tells us that if a condi on is true it will execute a block of statements and if the
condi on is false it won’t. But what if we want to do something else when the condi on is false? Here
comes the C else statement. We can use the else statement with the if statement to execute a block
of code when the condi on is false. The if-else statement consists of two blocks, one for false
expression and one for true expression.
Syntax

N. MPATCHIE HENSCHEL 21
STRUCTURED PROGRAMMING

Example

4.1.3 Nested if-else


A nested if in C is an if statement inside another if statement. Yes, C allow us to nest if statements
within if statements, i.e, we can place an if statement inside another if statement.
Syntax

Example

N. MPATCHIE HENSCHEL 22
STRUCTURED PROGRAMMING

4.1.4 If else-if
The if else-if statements are used when the user has to decide among mul ple op ons. The C if
statements are executed from the top down. As soon as one of the condi ons 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 condi ons is true, then the final else statement will be executed. If else-if is similar to the
switch statement.
Syntax

Example

4.1.5 switch Statement in C


The switch case statement is an alterna ve to the if else-if that can be used to execute the condi onal
code based on the value of the variable specified in the switch statement. The switch block consists of
cases to be executed based on the value of the switch variable.
Syntax

N. MPATCHIE HENSCHEL 23
STRUCTURED PROGRAMMING

Note: The switch expression should evaluate to either integer or character. It cannot evaluate any
other data type. The keyword break is used to exit the switch statement once a case is executed.
Without break, the program will con nue execu ng the subsequent cases (this is called “fall through”).
The keyword default is executed if none of the case values match the expression.
Example

4.1.6 Condi onal/Ternary Operator in C


The condi onal operator is used to add condi onal code in our program. It is similar to the if-else
statement. It is also known as the ternary operator as it works on three operands.
Syntax: (condi on) ? [true_statements] : [false_statements];
Example

4.1.7 Assignment
a. Write a C program that asks for a number from a user and says whether it is posi ve or nega ve.
b. Write a C program that asks for a number from a user and says whether it is even or odd.
c. Write a C program that asks for 3 numbers from a user and says which is the greatest.
d. Write a C program that asks for 3 numbers from a user and says which is the greatest.
e. Write a C program which checks the value of a day and prints the corresponding day of the week
using if-else or switch. If the value is not between 1 and 7, it prints “Invalid day!”
f. Write a C program which asks for an operator (+,-,*,/) from a user and performs an opera on
based on two numbers given by the user and the chosen operator.

N. MPATCHIE HENSCHEL 24
STRUCTURED PROGRAMMING

4.2 Looping Structures


Loops in programming are used to repeat a block of code un l the specified condi on is met. A loop
statement allows programmers to execute a statement or group of statements mul ple mes without
repe on of code. There are mainly two types of loops in C Programming:
4.2.1 Entry Controlled loops
In Entry controlled loops the test condi on is checked before entering the main body of the loop. For
Loop and While Loop are Entry-controlled loops.
a. for Loop
The for loop in C programming is a repe on control structure that allows programmers to write a
loop that will be executed a specific number of mes. It enables programmers to perform n number
of steps together in a single line.
Syntax

In for loop, a loop variable is used to control the loop. Firstly, we ini alize the loop variable with some
value, then check its test condi on. If the statement is true then control will move to the body and
the body of for loop will be executed. Steps will be repeated ll the exit condi on becomes true. If the
test condi on will be false then it will stop.
 Ini aliza on Expression: In this expression, we assign a loop variable or loop counter to some
value. for example: int i=1;
 Test Expression: In this expression, test condi ons are performed. If the condi on evaluates
to true then the loop body will be executed and then an update of the loop variable is done.
If the test expression becomes false then the control will exit from the loop. for example, i<=9;
 Update Expression: A er execu on of the loop body loop variable is updated by some value
it could be incremented, decremented, mul plied, or divided by any value.
Example

N. MPATCHIE HENSCHEL 25
STRUCTURED PROGRAMMING

b. while loop
While loop does not depend upon the number of itera ons. In for loop the number of itera ons was
previously known to us but in the While loop, the execu on is terminated on the basis of the test
condi on. If the test condi on will become false then it will break from the while loop else body will
be executed.
Syntax

Example

4.2.2 Exit Controlled loops


In Exit controlled loops the test condi on is evaluated at the end of the loop body. The loop body will
execute at least once, irrespec ve of whether the condi on is true or false. do-while Loop is Exit
Controlled loop.
do-while Loop
The do-while loop is similar to a while loop but the only difference lies in the do-while loop test
condi on which is tested at the end of the body. In the do-while loop, the loop body will execute at
least once irrespec ve of the test condi on.
Syntax

N. MPATCHIE HENSCHEL 26
STRUCTURED PROGRAMMING

Example

4.2.3 Loop Control Statements

Loop control statements in C programming are used to change execu on from its normal sequence.

Name Descrip on

the break statement is used to terminate the loop statement. It transfers the
break;
execu on to the statement immediately following the loop or switch.

con nue statement forces the program control to execute the next itera on
con nue; of the loop. As a result, the code inside the loop following the con nue
statement will be skipped and the next itera on of the loop will begin.

labelName: goto statement is a jump statement that transfers the control flow of a
statement; program to a labeled statement within the scope of the same func on,
goto labelName; breaking the normal flow of a program.

4.2.4 Infinite Loop


An infinite loop is executed when the test expression never becomes false and the body of the loop is
executed repeatedly. A program is stuck in an Infinite loop when the condi on is always true. Mostly
this is an error that can be resolved by using Loop Control statements.
Example: for ( ; ; ){statement}; while(1){statement;}

N. MPATCHIE HENSCHEL 27
STRUCTURED PROGRAMMING

4.2.5 Assignment
a. Write a C program that prints all integer numbers from 1 ll a specified number.
b. Write a C program that prints the sum of all integer numbers from 1 ll a specified number.
c. Write a C program that counts to 100 by tens.
d. Write a C program that only prints even numbers between 0 and 100 (inclusive).
e. Write a C program that only prints odd numbers between 0 and 100 (inclusive).
f. Write a C program that prints the powers of a specified number.
g. Write a C program which prints the mul plica on table of a specified number.
h. Write a C program which prints the mul plica on table from 1 ll a specified number.

N. MPATCHIE HENSCHEL 28

You might also like