[go: up one dir, main page]

0% found this document useful (0 votes)
16 views36 pages

Control Styructure in C

The document provides an overview of control structures in C programming, including decision-making statements (if, if-else, switch) and loop constructs (for, while, do-while). It explains the syntax and usage of each control structure, along with examples and explanations of related concepts like break and continue statements. Additionally, it includes questions and answers to test understanding of the material.

Uploaded by

chanchalmajhi69
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views36 pages

Control Styructure in C

The document provides an overview of control structures in C programming, including decision-making statements (if, if-else, switch) and loop constructs (for, while, do-while). It explains the syntax and usage of each control structure, along with examples and explanations of related concepts like break and continue statements. Additionally, it includes questions and answers to test understanding of the material.

Uploaded by

chanchalmajhi69
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Control Structure in C

programming

1
Control Structure
Control statement alter the flow of execution of program.
Types

Decision Making Loop Constructs


statements
1. if statement 1.for loop
2. if_else 2. while loop
3.switch 3.do_ while loop
If statement
Control Structure
Syntax :- 1. if( test _ condition)
statement ;

2. if(test_condition) The statement enclosed within curly brackets is


{ referred to as compound statements. Compound
Statement1 ; statements may consists of other compound
… statement.
Statement2;
}
Control Structure
if_else Statement 2. if(test_condition)
{
Syntax :- 1. if( test _ condition) Statement1 ;

statement ; Statement2;
else }
statement; else
{
Statement1 ;

Statement2;
}
C Tokens
Switch Statement
• Switch statement allows the user to choose a statement among several
alternatives.
• The switch statement is useful when a variable is to be compared with
different constants, a set of statements are to be executed.
• Constants in case statement can be of char or int datatype only.
Switch Statement
switch (n) C Tokens
​{ case constant1:
// code to be executed if n is equal to constant1;
break;
case constant2:
case constant3:
// code to be executed if n is equal to constant2 or constant3;
break;
case constant4:
// code to be executed if n is equal to constant1;
case constant5:
// code to be executed if n is equal to constant 5;
break;
default: // code to be executed if n doesn't match any constant }
Loops
Loop is a set of statements which executes repeatedly while an expression
is true. When the expression becomes false , the loop terminates and the
control passes to the statement following the loop.
Loops having two segments :-

1. Control statement
2. Body of loop
Loops
For loop
Syntax :- for(intial expression ; test_expression ; update expression)
Statement / Compound statements;
Loops
For loop – Some example
1. for( j=0;j<25;j++);

2.for(i=0,j=0;j<25;j++,i++)
printf(“%d%d”, i,j);

3. for( ;j<25;j++)
printf(“%d”, j);
Loops
For loop – Some example
4. for( ;;j++)
printf(“%d”, j);

5.for(;;)
printf(“I can not stop”);
Loops
While loop
1. while(test_condition)
statement;

2. while(test_condition)
{
statement1;
statement2;

}
Loops
While loop- Some exapmles
1. while(n!=0)
statement;

2. while(n< 45)
{
statement1;
statement2;

}
Loops
While loop- Some exapmles
3. while(j--);

4. while(1)
{
printf(“I can not stop” );
}
Loops
do_while loop
The do_while loop evaluates the condition after the execution of the
statements in its body.The statement within do_while are executed atleast
once. So do_while loop is also known as Bottom_tested loop.
1. do
statement;
while(test_condition);
2. do
{ statement1;
statement2;
… } while(test_condition);
Loops
break statement
A break statement terminates the execution of the loop and control is
transferred to the statement immediately following the loop.

Continue statement
The continue statement is used to bypass the remainder of the current pass
through the loop.
The loop does not terminate when a continue statement is encountered.
Which of the following is NOT a control structure in C?
A) if-else
B) switch
C) while
D) define
Which of the following is NOT a control structure in C?
A) if-else
B) switch
C) while
D) define

Answer: D) define
Explanation:
Control structures in C include decision-making (if-else, switch),
loops (while, for, do-while), and jump statements (break, continue, goto).
#define is a preprocessor directive, not a control structure.
What will be the output of the following C code?
c
#include <stdio.h>
int main() { int x = 5; if (x = 0) printf("True"); else printf("False"); return 0; }
A) True
B) False
C) Compilation error
D) Runtime error
What will be the output of the following C code?
c
#include <stdio.h>
int main() { int x = 5;
if (x = 0) printf("True");
else printf("False"); return 0; }
A) True
B) False
C) Compilation error
D) Runtime error

Answer: A) True
Explanation:
The condition if (x = 0) assigns 0 to x instead of comparing it.
Since 0 is treated as false, the else block executes, printing "False".
Which loop is guaranteed to execute at least once in C?
A) for loop
B) while loop
C) do-while loop
D) None of the above
Which loop is guaranteed to execute at least once in C?
A) for loop
B) while loop
C) do-while loop
D) None of the above

Answer: C) do-while loop


Explanation:
The do-while loop executes the loop body first and then checks the condition.
Hence, it always runs at least once, even if the condition is false.
What will be the output of the following code?
c
#include <stdio.h>
int main()
{ int i = 1; while (i < 5)
{ printf("%d ", i); i++;
}
return 0; }
A) 1 2 3 4
B) 1 2 3 4 5
C) 0 1 2 3 4
D) Infinite loop
What will be the output of the following code?
c
#include <stdio.h> int main() { int i = 1; while (i < 5) { printf("%d ", i); i++; } return 0; }
A) 1 2 3 4
B) 1 2 3 4 5
C) 0 1 2 3 4
D) Infinite loop
Answer: A) 1 2 3 4
Explanation:
•while (i < 5) runs as long as i is less than 5.
•i starts at 1 and increments until 4.
•When i = 5, the loop condition fails, stopping execution.
How many times will the following loop execute?
c
int i = 10; while (i < 10) { printf("Hello"); i++; }
A) 0
B) 1
C) 10
D) Infinite times
How many times will the following loop execute?
c
int i = 10; while (i < 10) { printf("Hello"); i++; }
A) 0
B) 1
C) 10
D) Infinite times
Answer: A) 0
Explanation:
Since i is initialized to 10 and the condition is while (i < 10),
the condition is false at the start, so the loop never executes.
What is the correct syntax for a for loop in C?
A) for (initialization; condition; update) { statements; }
B) for (condition; initialization; update) { statements; }
C) for (initialization, condition, update) { statements; }
D) for (initialization; update; condition) { statements; }
What is the correct syntax for a for loop in C?
A) for (initialization; condition; update) { statements; }
B) for (condition; initialization; update) { statements; }
C) for (initialization, condition, update) { statements; }
D) for (initialization; update; condition) { statements; }

Answer: A) for (initialization; condition; update) { statements; }


What will be the output of the following C code?
c
#include <stdio.h>
int main()
{ int i = 1; do { printf("%d ", i); i++; }
while (i <= 5);
return 0; }
A) 1 2 3 4 5
B) 1 2 3 4
C) 0 1 2 3 4 5
D) Infinite loop
What will be the output of the following C code?
c
#include <stdio.h>
int main()
{ int i = 1; do { printf("%d ", i); i++; }
while (i <= 5);
return 0; }
A) 1 2 3 4 5
B) 1 2 3 4
C) 0 1 2 3 4 5
D) Infinite loop
Answer: A) 1 2 3 4 5
Explanation:
Since it is a do-while loop, the body executes first, then the condition is checked.
The loop runs from 1 to 5 before stopping.
What does the break statement do inside a loop?
A) Skips the current iteration and moves to the next
B) Exits the loop immediately
C) Causes an infinite loop
D) Restarts the loop
What does the break statement do inside a loop?
A) Skips the current iteration and moves to the next
B) Exits the loop immediately
C) Causes an infinite loop
D) Restarts the loop
Answer: B) Exits the loop immediately
Explanation:
The break statement terminates a loop immediately when encountered.
What is the purpose of the continue statement?
A) It stops loop execution completely
B) It moves to the next iteration of the loop
C) It restarts the loop from the beginning
D) It throws a compilation error
What is the purpose of the continue statement?
A) It stops loop execution completely
B) It moves to the next iteration of the loop
C) It restarts the loop from the beginning
D) It throws a compilation error
Answer: B) It moves to the next iteration of the loop
Explanation:
The continue statement skips the remaining statements
in the current loop iteration and moves to the next iteration.
What will be the output of the following switch statement?
c
#include <stdio.h> int main()
{ int x = 2; switch (x)
{ case 1: printf("One"); case 2: printf("Two"); case 3: printf("Three"); d
efault: printf("Default"); } return 0; }
A) Two
B) TwoThreeDefault
C) Default
D) Compilation error
What will be the output of the following switch statement?
c
#include <stdio.h> int main()
{ int x = 2; switch (x)
{ case 1: printf("One"); case 2: printf("Two"); case 3: printf("Three"); d
efault: printf("Default"); } return 0; }
A) Two
B) TwoThreeDefault
C) Default
D) Compilation error
Answer: B) TwoThreeDefault

You might also like