0% found this document useful (0 votes)
8 views

Note

Uploaded by

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

Note

Uploaded by

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

 Basic information of operators:-

operators in C are symbols that represent operations to be performed on data. They are used to
manipulate values, perform calculations, and make decisions in a program. Here are some of the
basic operators in C:

1. Arithmetic Operators:

 ‘+’ (Addition): Adds two operands.


 ‘-‘ (Subtraction): Subtracts the second operand from the first.
 ‘*’ (Multiplication): Multiplies two operands.
 ‘/’ (Division): Divides the first operand by the second.
 ‘%’ (Modulus): Returns the remainder of the division.
2. Assignment Operators:

 ‘=’ (Assignment): Assigns the value of the right operand to the left operand.
 ‘+=’ (Addition Assignment): Adds the right operand to the left operand and assigns the result
to the left operand.
 ‘-=’ (Subtraction Assignment): Subtracts the right operand from the left operand and assigns
the result to the left operand.
 ‘*=’ (Multiplication Assignment): Multiplies the left operand by the right operand and
assigns the result to the left operand.
 ‘/=’ (Division Assignment): Divides the left operand by the right operand and assigns the
result to the left operand.
 ‘%=’ (Modulus Assignment): Calculates the modulus of the left operand with the right
operand and assigns the result to the left operand.

3. Increment and Decrement Operators:

 ‘++’ (Increment): Increases the value of a variable by 1.


 ‘- - ‘ (Decrement): Decreases the value of a variable by 1.
4. Relational Operators:

 ‘==’ (Equal to): Checks if two operands are equal.


 ‘!=’ (Not equal to): Checks if two operands are not equal.
 ‘<’ (Less than): Checks if the left operand is less than the right operand.
 ‘>’ (Greater than): Checks if the left operand is greater than the right operand.
 ‘<=’ (Less than or equal to): Checks if the left operand is less than or equal to the right
operand.
 ‘>=’ (Greater than or equal to): Checks if the left operand is greater than or equal to the
right operand.
5. Logical Operators:

 ‘&&’ (Logical AND): Returns true if both operands are true.


 ‘||’ (Logical OR): Returns true if at least one operand is true.
 ‘!’ (Logical NOT): Returns the opposite of the operand's truth value.
6. Bitwise Operators:

 ‘&’ (Bitwise AND): Performs a bitwise AND operation.


 ‘|’ (Bitwise OR): Performs a bitwise OR operation.
 ‘^’ (Bitwise XOR): Performs a bitwise exclusive OR operation.
 ‘~’ (Bitwise NOT): Flips the bits of the operand.
7. Conditional (Ternary) Operator:
 ‘condition ? expr1 : expr2’: Evaluates the condition and returns expr1 if true, otherwise
returns expr2.

These are some of the fundamental operators in C

Loop:

In programming, a loop is used to repeat a block of code until the specified condition is met.

C programming has three types of loops:

 for loop
 while loop
 do...while loop

(i)The for statement:

The syntax of the for loop is:

for (initializationStatement; testExpression; updateStatement)

// statements inside the body of loop

How for loop works?

 The initialization statement is executed only once.


 Then, the test expression is evaluated. If the test expression is evaluated to false, the for loop is
terminated.
 However, if the test expression is evaluated to true, statements inside the body of the for loop
are executed, and the update expression is updated.
 Again the test expression is evaluated.

This process goes on until the test expression is false. When the test expression is false, the loop
terminates.

Flowchart of for loop

(ii) The while statement:

The syntax of the while loop is:

while (testExpression) {

// the body of the loop

How while loop works?

 The while loop evaluates the testExpression inside the parentheses ().
 If testExpression is true, statements inside the body of while loop are executed.
Then, testExpression is evaluated again.
 The process goes on until testExpression is evaluated to false.
 If testExpression is false, the loop terminates (ends).
Flowchart of while loop

(iii) The do….while statement:

The do..while loop is similar to the while loop with one important difference. The body of do...while loop
is executed at least once. Only then, the test expression is evaluated.

The syntax of the do...while loop is:

do {

// the body of the loop

while (testExpression);

How do...while loop works?

 The body of do...while loop is executed once. Only then, the testExpression is evaluated.
 If testExpression is true, the body of the loop is executed again and testExpression is evaluated
once more.
 This process goes on until testExpression becomes false.
 If testExpression is false, the loop ends.

Flowchart of do...while Loop


C if statement :

The syntax of the if statement in C programming is:

If ( test expression )

// code

How if statement works?

The if statement evaluates the test expression inside the parenthesis ().

 If the test expression is evaluated to true, statements inside the body of if are executed.
 If the test expression is evaluated to false, statements inside the body of if are not executed.

C if……else statement :

The if statement may have an optional else block.

The syntax of the if..else statement is:

if ( test expression )

// run code if text expression is true

else

// run code if text expression is false


}

How if……else statement works?

If the test expression is evaluated to true,

 statements inside the body of if are executed.


 statements inside the body of else are skipped from execution.

If the test expression is evaluated to false,

 statements inside the body of else are executed


 statements inside the body of if are skipped from execution.

C if……else Ladder:

The if...else statement executes two different codes depending upon whether the test expression is true
or false. Sometimes, a choice has to be made from more than 2 possibilities.

The if...else ladder allows you to check between multiple test expressions and execute different
statements.

The syntax of the if..else statement is:

if ( test expression 1)

// Statement (s)

else if ( test expression 2)

{
// Statement (s)

else if ( test expression 3)

// Statement (s)

else

// Statement (s)

Nested if...else:

Nested if statement:

The ANSI standard specifies that 15 levels of nesting must be supported. In C, an else statement always
refers to the nearest if statement in the same block and not already associated with if.

You might also like