CP Chapter 2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 30

2nd chapter

Control structure
control structures ” are the structures used to control the flow of a program.
The two main classes are conditionals and loops

Control Structures are just a way to specify flow of control in programs. Any
algorithm or program can be more clear and understood if they use self-
contained modules called as logic or control structures. It basically analyzes
and chooses in which direction a program flows based on certain parameters
or conditions.
There are three types of control structures available in C and C++
1) Sequence structure (straight line paths)
2) Selection structure (one or many branches)
3)Loop structure (repetition of a set of activities)

Sequence structure
Sequence construct means the statements are being executed sequentially. It is a default flow of
statement from top to bottom.
Selection structures
 Selection structures are implemented using If , If
Else and Switch statements.
 Selection structures are used to perform ‘decision making‘ and
then branch the program flow based on the outcome of decision
making. Selection structures are implemented in C/C++ with If, If
Else and Switch statements. If and If Else statements are 2 way
branching statements where as Switch is a multi branching
statement.

 The simple If statement

 Flowchart of if statement in C

 The syntax format of a simple if statement is as shown


below.

 if(expression){
 //code to be executed
 }

 The expression given inside the brackets after if is evaluated first. If the
expression is true, then statements inside the curly braces that follow
if(expression) will be executed. If the expression is false, the statements
inside curly braces will not be executed and program control goes directly to
statements after curly braces.

Example program to demo “If” statement


 #include
void main()
{
int num;
printf("Hello user, Enter a number");
scanf("%d",&num); // Collects the number from user
if(num==1)
{
printf("UNITED STATES");
}
if(num==2)
{
printf("SPAIN");
}
if(num==3)
{
printf("INDIA");
}
}
 The If Else statement.

 Flowchart of the if-else statement in C

 Syntax format for If Else statement is shown below.

 if(expression){
 //code to be executed if condition is true
 }else{
 //code to be executed if condition is false
 }

 The execution begins by evaluation expression 1. If it is TRUE,


then statements inside the immediate curly braces is evaluated.
If it is FALSE, program control is transferred directly to
immediate else if statement. Here expression 2 is evaluated for
TRUE or FALSE. The process continues. If all expressions inside
the different if and else if statements are FALSE, then the
last else statement (without any expression) is executed along
with the statements 1 and 2 inside the curly braces of
last else statement.

Example program to demo “If Else”

#include
void main()
{
int num;
printf("Hello user, Enter a number");
scanf("%d",&num); // Collects the number from user
if(num==1)
{
printf("UNITED STATES");
}
else if(num==2)
{
printf("SPAIN");
}
else if(num==3)
{
printf("INDIA");
}
else
{
printf("WRONG ENTRY"); // See how else is used to output
"WRONG ENTRY"
}
}
 Note:- Notice how the use of If Else statem
Switch statement

Flowchart of switch statement in C

Switch is a multi branching control statement.


Syntax for switch statement is shown below.

1. switch(expression){
2. case value1:
3. //code to be executed;
4. break; //optional
5. case value2:
6. //code to be executed;
7. break; //optional
8. ......
9.
10. default:
11. code to be executed if all cases are not matched;
12. }

Execution of switch statement begins by evaluating the expression


inside the switch keyword brackets. The expression should be an
integer (1, 2, 100, 57 etc ) or a character constant like ‘a’, ‘b’ etc. This
expression’s value is then matched with each case values. There can
be any number of case values inside a switch statements block. If first
case value is not matched with the expression value, program control
moves to next case value and so on. When a case value matches
with expression value, the statements that belong to a
particular case value are executed.

#include
void main()
{
int num;
printf("Hello user, Enter a number");
scanf("%d",&num); // Collects the number from user
switch(num)
{
case 1:
printf("UNITED STATES");
break;
case 2:
printf("SPAIN");
break;
case 3:
printf("INDIA");
default:
printf("WRONG ENTRY");
}
}
Note:- Switch statement is used for multiple branching. The same can
be implemented using nested “If Else” statements. But use of
nested if else statements make program writing tedious and complex.
Switch makes it much easier. Compare this program with above one.

Loop structures

A loop structure is used to execute a certain set of actions for a


predefined number of times or until a particular condition is satisfied.
There are 3 control statements available in C/C++ to implement loop
structures. While, Do while and For statements.
The while statement

Flowchart of while loop in C

Syntax for while loop is shown below:


while(condition)// This condition is tested for TRUE or FALSE.
Statements inside curly braces are executed as long as condition is
TRUE
{
statement 1;
statement 2;
statement 3;
}
The condition is checked for TRUE first. If it is TRUE then all statements
inside curly braces are executed.Then program control comes back to
check the condition has changed or to check if it is still TRUE. The
statements inside braces are executed repeatedly, as long as the
condition is TRUE. When the condition turns FALSE, program control
exits from while loop.

Note:- while is an entry controlled loop. Statement inside braces are


allowed to execute only if condition inside while is TRUE.
Let's see the simple program of while loop that prints table of 1.

1. #include<stdio.h>
2. int main(){
3. int i=1;
4. while(i<=10){
5. printf("%d \n",i);
6. i++;
7. }
8. return 0;
9. }

Output
1
2
3
4
5
6
7
8
9
10

Program to print table for the given number using while


loop in C
1. #include<stdio.h>
2. int main(){
3. int i=1,number=0,b=9;
4. printf("Enter a number: ");
5. scanf("%d",&number);
6. while(i<=10){
7. printf("%d \n",(number*i));
8. i++;
9. }
10. return 0;
11. }
Output
Enter a number: 50
50
100
150
200
250
300
350
400
450
500

Example 1.
1. #include<stdio.h>
2. void main ()
3. {
4. int j = 1;
5. while(j+=2,j<=10)
6. {
7. printf("%d ",j);
8. }
9. printf("%d",j);
10. }

Output
3 5 7 9 11

Example 2
1. #include<stdio.h>
2. void main ()
3. {
4. while()
5. {
6. printf("hello Javatpoint");
7. }
8. }

Output
compile time error: while loop can't be empty
Example 3
1. #include<stdio.h>
2. void main ()
3. {
4. int x = 10, y = 2;
5. while(x+y-1)
6. {
7. printf("%d %d",x--,y--);
8. }
9. }

Output
infinite loop

The do while statement

Flowchart of do while loop


Syntax for do while loop is shown below:
do
{
statement 1;
statement 2;
statement 3;
}
while(condition);
Unlike while, do while is an exit controlled loop. Here the set of
statements inside braces are executed first. The condition inside while
is checked only after finishing the first time execution of statements
inside braces. If the condition is TRUE, then statements are executed
again. This process continues as long as condition is TRUE. Program
control exits the loop once the condition turns FALSE.
do while example
There is given the simple program of c language do while loop where we are
printing the table of 1.

Example

1. #include<stdio.h>
2. int main(){
3. int i=1;
4. do{
5. printf("%d \n",i);
6. i++;
7. }while(i<=10);
8. return 0;
9. }

Output
1
2
3
4
5
6
7
8
9
10
Program to print table for the given number using do while loop
1. #include<stdio.h>
2. int main(){
3. int i=1,number=0;
4. printf("Enter a number: ");
5. scanf("%d",&number);
6. do{
7. printf("%d \n",(number*i));
8. i++;
9. }while(i<=10);
10. return 0;
11. }

Output
Enter a number: 5
5
10
15
20
25
30
35
40
45
50
The for statement

Flowchart of for loop in C

Syntax of for statement is shown below:

for(initialization statements;test condition;iteration statements)


{
statement 1;
statement 2;
statement 3;
}
The for statement is an entry controlled loop. The difference between
while and for is in the number of repetitions. The for loop is used when
an action is to be executed for a predefined number of times. The
while loop is used when the number of repetitions is not predefined.

Let's see the simple program of for loop that prints table of 1.

EXAMPLE

1. #include<stdio.h>
2. int main(){
3. int i=0;
4. for(i=1;i<=10;i++){
5. printf("%d \n",i);
6. }
7. return 0;
8. }

Output

1
2
3
4
5
6
7
8
9
10

C Program: Print table for the given number using C for loop

1. #include<stdio.h>
2. int main(){
3. int i=1,number=0;
4. printf("Enter a number: ");
5. scanf("%d",&number);
6. for(i=1;i<=10;i++){
7. printf("%d \n",(number*i));
8. }
9. return 0;
10. }

Output

Enter a number: 2
2
4
6
8
10
12
14
16
18
20

Properties of Expression 1

o The expression represents the initialization of the loop variable.

o We can initialize more than one variable in Expression 1.

o Expression 1 is optional.

o In C, we can not declare the variables in Expression 1. However, It can be an


exception in some compilers.

1. #include <stdio.h>
2. int main()
3. {
4. int a,b,c;
5. for(a=0,b=12,c=23;a<2;a++)
6. {
7. printf("%d ",a+b+c);
8. }
9. }

Output

35 36

Example 2
1. #include <stdio.h>
2. int main()
3. {
4. int i=1;
5. for(;i<5;i++)
6. {
7. printf("%d ",i);
8. }
9. }

Output

1 2 3 4

Properties of Expression 2

o Expression 2 is a conditional expression. It checks for a specific condition to be


satisfied. If it is not, the loop is terminated.

o Expression 2 can have more than one condition. However, the loop will iterate
until the last condition becomes false. Other conditions will be treated as
statements.

o Expression 2 is optional.

o Expression 2 can perform the task of expression 1 and expression 3. That is, we
can initialize the variable as well as update the loop variable in expression 2
itself.

o We can pass zero or non-zero value in expression 2. However, in C, any non-zero


value is true, and zero is false by default.

Example 1

1. #include <stdio.h>
2. int main()
3. {
4. int i;
5. for(i=0;i<=4;i++)
6. {
7. printf("%d ",i);
8. }
9. }

output

0 1 2 3 4

Example 2

1. #include <stdio.h>
2. int main()
3. {
4. int i,j,k;
5. for(i=0,j=0,k=0;i<4,k<8,j<10;i++)
6. {
7. printf("%d %d %d\n",i,j,k);
8. j+=2;
9. k+=3;
10. }
11. }

Output

0 0 0
1 2 3
2 4 6
3 6 9
4 8 12

Example 3

1. #include <stdio.h>
2. int main()
3. {
4. int i;
5. for(i=0;;i++)
6. {
7. printf("%d",i);
8. }
9. }

Output

infinite loop

Properties of Expression 3
o Expression 3 is used to update the loop variable.

o We can update more than one variable at the same time.

o Expression 3 is optional.

Example 1

1. #include<stdio.h>
2. void main ()
3. {
4. int i=0,j=2;
5. for(i = 0;i<5;i++,j=j+2)
6. {
7. printf("%d %d\n",i,j);
8. }
9. }

Output

0 2
1 4
2 6
3 8
4 10

Loop body
The braces {} are used to define the scope of the loop. However, if the loop contains
only one statement, then we don't need to use braces. A loop without a body is
possible. The braces work as a block separator, i.e., the value variable declared inside
for loop is valid only for that block and not outside. Consider the following example.

1. #include<stdio.h>
2. void main ()
3. {
4. int i;
5. for(i=0;i<10;i++)
6. {
7. int i = 20;
8. printf("%d ",i);
9. }
10. }

Output

20 20 20 20 20 20 20 20 20 20

C break statement
The break is a keyword in C which is used to bring the program control out of the loop.
The break statement is used inside loops or switch statement. The break statement
breaks the loop one by one, i.e., in the case of nested loops, it breaks the inner loop first
and then proceeds to outer loops. The break statement in C can be used in the following
two scenarios:

1. With switch case

2. With loop

Syntax:

1. //loop or switch case


2. break;
Flowchart of break in c

Example

1. #include<stdio.h>
2. #include<stdlib.h>
3. void main ()
4. {
5. int i;
6. for(i = 0; i<10; i++)
7. {
8. printf("%d ",i);
9. if(i == 5)
10. break;
11. }
12. printf("came outside of loop i = %d",i);
13.
14. }
Output

0 1 2 3 4 5 came outside of loop i = 5

Example of C break statement with switch case


Click here to see the example of C break with the switch statement.

C break statement with the nested loop


In such case, it breaks only the inner loop, but not outer loop.

1. #include<stdio.h>
2. int main(){
3. int i=1,j=1;//initializing a local variable
4. for(i=1;i<=3;i++){
5. for(j=1;j<=3;j++){
6. printf("%d &d\n",i,j);
7. if(i==2 && j==2){
8. break;//will break loop of j only
9. }
10. }//end of for loop
11. return 0;
12. }

Output

1 1
1 2
1 3
2 1
2 2
3 1
3 2
3 3

As you can see the output on the console, 2 3 is not printed because there is a break
statement after printing i==2 and j==2. But 3 1, 3 2 and 3 3 are printed because the
break statement is used to break the inner loop only.

SQL CREATE TABLE


break statement with while loop
Consider the following example to use break statement inside while loop.

1. #include<stdio.h>
2. void main ()
3. {
4. int i = 0;
5. while(1)
6. {
7. printf("%d ",i);
8. i++;
9. if(i == 10)
10. break;
11. }
12. printf("came out of while loop");
13. }

Output

0 1 2 3 4 5 6 7 8 9 came out of while loop

break statement with do-while loop


Consider the following example to use the break statement with a do-while loop.

1. #include<stdio.h>
2. void main ()
3. {
4. int n=2,i,choice;
5. do
6. {
7. i=1;
8. while(i<=10)
9. {
10. printf("%d X %d = %d\n",n,i,n*i);
11. i++;
12. }
13. printf("do you want to continue with the table of %d , enter any non-zero value to c
ontinue.",n+1);
14. scanf("%d",&choice);
15. if(choice == 0)
16. {
17. break;
18. }
19. n++;
20. }while(1);
21. }

Output

2 X 1 = 2
2 X 2 = 4
2 X 3 = 6
2 X 4 = 8
2 X 5 = 10
2 X 6 = 12
2 X 7 = 14
2 X 8 = 16
2 X 9 = 18
2 X 10 = 20
do you want to continue with the table of 3 , enter any non-zero value to
continue.1
3 X 1 = 3
3 X 2 = 6
3 X 3 = 9
3 X 4 = 12
3 X 5 = 15
3 X 6 = 18
3 X 7 = 21
3 X 8 = 24
3 X 9 = 27
3 X 10 = 30
do you want to continue with the table of 4 , enter any non-zero value to
continue.0

C continue statement
The continue statement in C language is used to bring the program control to the
beginning of the loop. The continue statement skips some lines of code inside the loop
and continues with the next iteration. It is mainly used for a condition so that we can
skip some code for a particular condition.
Syntax:

1. //loop statements
2. continue;
3. //some lines of the code which is to be skipped

Continue statement example 1


1. #include<stdio.h>
2. void main ()
3. {
4. int i = 0;
5. while(i!=10)
6. {
7. printf("%d", i);
8. continue;
9. i++;
10. }
11. }

Output

infinite loop

Continue statement example 2


1. #include<stdio.h>
2. int main(){
3. int i=1;//initializing a local variable
4. //starting a loop from 1 to 10
5. for(i=1;i<=10;i++){
6. if(i==5){//if value of i is equal to 5, it will continue the loop
7. continue;
8. }
9. printf("%d \n",i);
10. }//end of for loop
11. return 0;
12. }

Output

1
2
3
4
6
7
8
9
10

As you can see, 5 is not printed on the console because loop is continued at i==5.

C continue statement with inner loop


In such case, C continue statement continues only inner loop, but not outer loop.

1. #include<stdio.h>
2. int main(){
3. int i=1,j=1;//initializing a local variable
4. for(i=1;i<=3;i++){
5. for(j=1;j<=3;j++){
6. if(i==2 && j==2){
7. continue;//will continue loop of j only
8. }
9. printf("%d %d\n",i,j);
10. }
11. }//end of for loop
12. return 0;
13. }

Output

Triggers in SQL (Hindi)


1 1
1 2
1 3
2 1
2 3
3 1
3 2
3 3

As you can see, 2 2 is not printed on the console because inner loop is continued at
i==2 and j==2.

C goto statement
The goto statement is known as jump statement in C. As the name suggests, goto is
used to transfer the program control to a predefined label. The goto statment can be
used to repeat some part of the code for a particular condition. It can also be used to
break the multiple loops which can't be done by using a single break statement.
However, using goto is avoided these days since it makes the program less readable
and complecated.

Syntax:

1. label:
2. //some part of the code;
3. goto label;

goto example
Let's see a simple example to use goto statement in C language.

1. #include <stdio.h>
2. int main()
3. {
4. int num,i=1;
5. printf("Enter the number whose table you want to print?");
6. scanf("%d",&num);
7. table:
8. printf("%d x %d = %d\n",num,i,num*i);
9. i++;
10. if(i<=10)
11. goto table;
12. }

Output:

Enter the number whose table you want to print?10


10 x 1 = 10
10 x 2 = 20
10 x 3 = 30
10 x 4 = 40
10 x 5 = 50
10 x 6 = 60
10 x 7 = 70
10 x 8 = 80
10 x 9 = 90
10 x 10 = 100

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy