Lecture 7

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 26

UNIT 1: CONTROL STRUCTURES

CAP202| Dr. Ajay| Programming Domain


CONTROL STRUCTURES
• If statements.
• Switch Statement.
• Conditional Operator Statement.
• Goto Statement.
• Loop Statements
IF STATEMENT
• It takes an expression in parenthesis and an statement or block of
statements.
• if the expression is true then the statement or block of statements gets
executed otherwise these statements are skipped.
• if(boolean_expression) {
/* statement(s) will execute if the boolean expression is true */
}
IF STATEMENT
#include <iostream>
using namespace std;
int main ()
{ int a = 10;
/* check the boolean condition using if statement */
if( a < 20 )
{ /* if condition is true then print the following */
cout<<“a is less than 20”;
}
cout<<“value of a is “<< a;
}
IF ELSE STATEMENT
• An if statement can be followed by an optional else statement, which
executes when the boolean expression is false.
• if(boolean_expression) {
• /* statement(s) will execute if the boolean expression is true */
• } else {
• /* statement(s) will execute if the boolean expression is false */
• }
• If the Boolean expression evaluates to true, then the if block will be
executed, otherwise, the else block will be executed.
#include <iostream>
using namespace std;
main () {
int a = 100;
if( a < 20 ) {
cout<<"a is less than 20";
} else {
cout<< "a is not less than 20" ;
}
cout<< "value of a is"<< a;
}
SWITCH
• A switch statement allows a variable to be tested for
equality against a list of values.
• Each value is called a case, and the variable being
switched on is checked for each switch case.
switch(expression) {
case constant-expression :
statement(s);
break; /* optional */
case constant-expression :
statement(s);
break; /* optional */
/* you can have any number of case statements */
default : /* Optional */
statement(s);
}
#include <iostream> case 'D' : cout<< "You passed";
using namespace std; break;
main () case 'F’ : cout<< "Better try again" ;
{ char grade = 'B';
break;
switch(grade) {
default : cout<< "Invalid grade"
case 'A' : cout<< "Excellent" ;
<<endl ;
break;
break;
case 'B' : cout<<"great";
}
break;
}
case 'C' : cout<<"Well done" ;
break;
CONDITIONAL OPERATOR
• The conditional operator is also known as a ternary
operator.
• The conditional statements are the decision-making
statements which depends upon the output of the
expression.
• As conditional operator works on three operands, so it is
also known as the ternary operator.
• Expression1? expression2: expression3;
#include <iostream>
using namespace std;
int main()
{ int age; // variable declaration
cout<<"Enter your age";
cin>>age; // taking user input for age variable
(age>=18)? (cout<<"eligible for voting") : (cout<<"not eligible for
voting"); // conditional operator
}
GO TO
The goto statement can be used to jump from anywhere to
anywhere within a function
goto label;
..
.
label: statement;
#include <iostream>
using namespace std;
main() // definition
{
cout<<"before go statement ";
goto mca;
cout<<"after go to statement ";
mca:
cout<<"inside go to";
}
LOOPS
• The looping can be defined as repeating the same process
multiple times until a specific condition satisfies.
• There are three types of loops used in the C language.
1. do while
2. while
3. for
FOR LOOP
• The for loop is used in the case where we need to execute
some part of the code until the given condition is satisfied.
• The for loop is also called as a per-tested loop.
• It is better to use for loop if the number of iteration is
known in advance.
for(initialization;condition;incr/decr){  
//code to be executed  
}  
#include <iostream>
using namespace std;
int main(){
int i=0;
for(i=1;i<=5;i++) {
cout<<"hello"<<endl;
}
}
WHILE LOOP
• While loop is also known as a pre-tested loop.
• Repeats a statement or group of statements while a given
condition is true. It tests the condition before executing the
loop body.
• The while loop is mostly used in the case where the
number of iterations is not known in advance.
SYNTAX
while(condition) {
statement(s);
}
#include <iostream>
using namespace std;
int main()
{ int i=1;
while(i<=5) {
cout<< "hello"<<endl;
i++;
}
}
DO-WHILE
The do while loop is a post tested loop
Unlike for and while loops, which test the loop condition
at the top of the loop, the do...while loop in C
programming checks its condition at the bottom of the
loop.
The do-while loop is mainly used in the case where we
need to execute the loop at least once.
SYNTAX
do{
//code to be executed
}while(condition)
#include <iostream>
using namespace std;
int main()
{ int i=1;
do {
cout<< "hello"<<endl;
i++;
} while(i<=5);
}

You might also like