Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Modulus % r mod s r % s
Relational operators
> > x > y x is greater than y
< < x < y x is less than y
Equality operators
= == x == y x is equal to y
!= x != y x is not equal to y
Operators
Unary Operator
– ++,--
Binary Operator
Ternary Operator
– conditional operator (?:)
Condition Control Statements
These statements are executed when one or more
conditions is/are satisfied.
C++ supports many such statements.
IF
– The statement following the if statement is executed when
the condition given is true.
– if(condition)
– s1;
– s2;
– When the condition is true statement s1 and then
s2 is executed. If the condition is false only s2 is
executed.
Condition Control Statements
Condition Control Statements
Condition Control Statements
– If we want to execute more than 1 statements when
condition is true then we should write all those statements
within braces {} after if.
– if(condition)
– {
– s1;
– s2;
– }
– s3;
– when the condition is true statement s1, s2 and then s3 is
executed. If the condition is false only s3 is executed.
–
IF ELSE
– Also known as either or. This statement is used to select
one statement and ignore the other statements.
– if(condition)
– s1;
– else
– s2;
– when the condition is true statement s1 is
executed. If the condition is false s2 is executed. Thus one
of the statements, either s1 or s2 is always executed.
–
IF ELSE
IF ELSE
IF ELSE IF ELSE
– It is a branching statement which can choose and execute on of the
statements available depending upon the condition.
– if(condition1)
– s1;
– else if(condition2)
– s2;
– else
– s3;
– when the condition1 is true statement s1 is executed and
rest are ignored. When condition1 is false condition2 is verified. If
condition2 is true statement s2 is executed and other statements
are ignored and so on. Thus only one statement is executed from
the top, depending upon the condition. The statement following
else is executed when all the conditions are false.
IF ELSE IF ELSE
IF ELSE IF ELSE
IF ELSE IF ELSE
While
It is a repeated structure statement which repeats a
statement given as long as the condition is true.
while(condition)
statement1;
statement1 is executed till the condition is true.
While
Lecture:2 Control Statement CSC-115 Programming Fundamentals
While
Do while
– Like while statement it repeats a statement given as long
as the condition is satisfied unlike in while statement the
condition is checked at the end of the structure.
– do
– {
– statement1;
– }
– while(condition);
– statement1 is executed till the condition is true.
–
Do while
Do while
Do while
For
– It is a repeated structure which can repeat a statement as
long as the given condition is satisfied.
– for(statement1; condition;
statement2)
– statement3;
– where statement1 initializes control variable, condition
is used to check whether statement written after loop
can be repeated or not , statement2 is used to modify
the control variable. Statement3 is a simple statement
(having only one statement) or compound statement (a
set of statements written within braces {} ) which can
be repeated as long as condition is true or satisfied.
Switch
– It is a multi branch statement which can be used to select
and execute one of the available statements.
– switch(value)
– {
– case 1: statement 1; break;
– case 2: statement 2; break;
– case n: statement n; break;
– default: statement d;
– }
– Where value can be a variable of type numeric or character.
The case label 1 to n can also be written with constant
identifiers.
Lecture:2 Control Statement CSC-115 Programming Fundamentals
Switch
When the value assigned matches with case label 1
statement 1 is executed.
The break statement written after statement 1 transfers the
control out of the switch statement.
When the value doesn’t match with case label 1 then it
checks with case label 2 and so on.
When the value assigned doesn’t match with any of the case
labels (1 to n) then the default clause is considered and the
statement d is executed.
Default clause is optional like else clause in if-else-if-else
statement.
Switch
Switch
Unconditional
Return
Goto
Break
Continue
The Goto Statement
This system does not require any condition.
This system passes control anywhere in the program without
considering any condition.
Here a label is any valid label either before or after goto.
The label must start with any character and can be constructed
with rules used for forming identifiers.
Avoid using the goto stataemnt.
Goto label;
------
-------
--------
Label:
The Goto Statement
The Goto Statement
The Goto Statement
The Goto Statement
The Break Statement
The break statement allows the programmer to terminate the loop.
The break skips from the loop or the block in which it is defined.
The control then automatically passes onto the first statement
after the loop or the block.
The break can be associated with the conditional statements (
specially switch case)
We can also use break statements in the nested loops.
If we use break statement in the innermost loop, then the control
of the program is terminated from that loop only and resumes at
the next statement following that loop.
The widest use of this statement is in the switch case where it is
used to avoid flow of control from one case to the other.
The Break Statement
The Break Statement
The Break Statement
The Continue Statement
The continue statement works somewhat like the break statement.
Instead of forcing the control to the end of the loop( as it is in the
case of break), the continue case causes the control to pass on to
the beginning of the block / loop.
In the case of for loop the continue case initiates the testing
condition and increment on steps has to be executed (while rest
of the statement following the continue are neglected ).
For while and do while , the continue case causes control to pass
on to conditional tests .
It is useful in a programming situation, where it is required that
particular iterations occur only up to some extent or when some
part of the code has to be neglected.
The Continue Statement
Array
An array is a series of elements of the same type placed in
contiguous memory locations that can be individually
referenced by adding an index to a unique identifier.
An array stores a fixed-size sequential collection of
elements of the same type.
An array is used to store a collection of data, but it is
often more useful to think of an array as a collection of
variables of the same type.
All arrays consist of contiguous memory locations. The
lowest address corresponds to the first element and the
highest address to the last element.
Lecture:2 Control Statement CSC-115 Programming Fundamentals
The End