C Programming: Operators, Control Structures and I/O, Slides of Computer Programming

An overview of various operators in c programming, including comparison, logical, compound, and assignment operators. It also covers control structures such as if statements, switch statements, and loops (for, while, do-while). Additionally, it explains input and output operations using printf and scanf functions.

Typology: Slides

2011/2012

Uploaded on 07/23/2012

paravi
paravi 🇮🇳

5

(1)

65 documents

1 / 32

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Operators
== test equality x == y
= assignment x = y
!= not equal
>= greater than or equal
<= less than or equal
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20

Partial preview of the text

Download C Programming: Operators, Control Structures and I/O and more Slides Computer Programming in PDF only on Docsity!

Operators

^ == test equality

x == y

^ =

assignment

x = y

^ !=

not equal ^ >=

greater than or equal ^ <=

less than or equal

More operators

^ >

greater than ^ <

less than ^ no space between operators ^ = = is invalid

== OK

The If Statement

^ Syntax:

if (expression) statement; ^ If the expression is true (not zero), thestatement is executed. ^ If the expression is false, it is notexecuted. ^ You can group multiple expressionstogether with braces:

if (expression) {statement 1;statement 2;statement 3;}

Logic Operators in ‘if’

^ Equal to

if (x==10)

^ Not equal to

if (x!=10)

^ Less than

if (x<10)

^ Greater than

if (x>10)

^ Less than / equal to

if (x<=10)

^ Greater than / equal to

if (x>=10)

If else

if(income

>^

printf(“pay

tax”);

else

printf(“find

a^

better

job”);

one

of

these

statements

always

execute

Single and Compound Statements

Single statements:^ if^

( condition)true_statement; else false_statement

;

Multiple statements:^ if^

( condition)

{

……} else^ { …… }

Switch Statements

^ Switch statements look like thisexample:

switch

(expression)

{

value_

:^ statements_1;

break;

value_

:^ statements_2;

break;

...value_n

:^ statements_n;

break

default

C Operators

^ The increment Operator^ 

Increment

x++ or ++x

^ Decrement

x–- or –-x

^ Shorthand Assigns^ 

x*=

(multiply x by 3; x = x * 3) ^ x+=

(add 5 to x) ^ x-=10 (subtract 10 from 6) ^ x/=

(halve x)

Precedence rules

^ set rules for precedence ^ use parenthesis

Conditional operator

marks

>^

printf(“passed”)

printf(“failed”);  condition^ ^ marks >50  Execute^ ^ 1st statement if condition is true^ ^ 2nd statement if condition is false

printf

printf

(“the

answer

is

= %d”,

x);

^ Specifies data type & where to get thevalue to display.

More printf

printf

(“%8.4f %d\n”, total,

sum);

^ display^ 

a real #

total with 4 decimal places

^ an integer

sum

More scanf

scanf(“%f”,&num_f);

^ read in floating point value

Loops

^ Repeat a series of statements ^ Not reasonable to copy statementsmultiple time ^ Need a way to repeat a block of code ^ Loops allow repetition