0% found this document useful (0 votes)
153 views24 pages

More On Decision Making and Loops in C

The document discusses decision making and loops in C programming, covering if/else statements, logical operators, switch statements, and for, while, do-while loops. Examples are provided to illustrate the use of different conditional statements and loops.

Uploaded by

shayan15
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
153 views24 pages

More On Decision Making and Loops in C

The document discusses decision making and loops in C programming, covering if/else statements, logical operators, switch statements, and for, while, do-while loops. Examples are provided to illustrate the use of different conditional statements and loops.

Uploaded by

shayan15
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 24

CS 1001 Programming in C

Lecture 4
More on Decision
s
Making and Loops in C

s
The If Statement

 Syntax: if (expression) statement;


 If the expression is true (not zero), the
statement is executed.
 If the expression is false, it is not
executed.
 You can group multiple expressions
together 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)
Compound Operators

 Logical AND if (x==1 && y==2)


 Logical OR if (x==1 || y==2)
 Logical NOT if (!x) …
Single and Compound Statements

Single statements: Multiple statements:

if (condition) {
if (condition)
……
true_statement;
}
else
else {
false_statement;
……
}
Nested If Statements
void main(void)
{
int winner =3;
printf(“…and the winner of ICC is ”);
if (winner==1) printf(“Pakistan”);
else if (winner==2)
printf(“England”);
else if (winner==3)
printf(“WI”);
else
printf(“Australia”);

}
Conditional Statement
void main(void)
{
int num;
printf("Enter a value:");
scanf("%d", &num);
if (num < 0)
printf("The value is negative\n");
else if (num == 0)
printf("The value is zero\n");
else
printf("The value is positive\n");
return 0;
}
Switch Statements
 Switch statements look like this
example:

switch (expression) {
value_1 : statements_1; break;
value_2 : statements_2; break;
...
value_n : statements_n; break
default
switch (winner) {
case 1 : printf(“Pakistan”);
break;
case 2 : printf(“England”);
break;
.
value_n : statements_n; break
default:
Loops

 Repeat a series of statements


 Not reasonable to copy statements
multiple time
 Need a way to repeat a block of code
 Loops allow repetition
The For Loop

 Syntax:
 for (initialization; test; increment)
{
statements;
}
 The for loop will first perform the
initialization. Then, as long test is TRUE,
it will execute statements. After each
execution, it will increment.
For loop

 Known number of iterations

for(count=1;count<=10;count++)
{
body of loop
}
For loop examples

for (x=20 ; x <= 80; x +=10)


 from 20 to 80 in steps of 10

for(x=80; x >= 20; x -=10)


 from 80 to 20 in steps of -10
While loop

 Unknown number of iterations

degree =0;
while (degrees <= 360)
{
decree += increment;
}
The While Loop
 An example while loop looks like this:

void main(void)
{
char ch;

while (ch != ‘Q’ || ch != ‘q’)


{

ch = getchar( );
}
}
The Do-while Loop
 The do-while loop repeatedly executes a
block of code indicated by statements as long
as the conditional expression cond_expr is
true.

do {
statements;
} while (cond_expr);
A ‘while’ for ‘for’
i=0;
while(i<10)
{
body of the loop;
i++;
}
is equivalent to
for(i=0; i<10; i++)
{
body of the loop;
}
A ‘for’ for ‘while’

for(; degree<360;)
{
degree += increment;
}
Special Cases

 You can have as many control variables


as you want in loops. The following is
fine:

for (x=0, y=0; x+y<10; x++, y++)


A forever loop
 Using for
for(;;)
{
printf(“Would you please stop teaching C\n”);
}
 Using while
while(1)
{
printf(“Would you please stop teaching C\n”);
}
Fahrenheit-to-Celsius conversion
void main(void)
{
int deg_c, deg_f;
deg_c = 0;
while (deg_c <= 100)
{
deg_f = (deg_c-32)*5/9;
printf("%4d degrees F = %4d degrees C\n", deg_c, deg_f);
deg_c = deg_c + 10;
}
}
int main()
{
int count = 1;
while (count <= 100)
{
printf("%d\n",count);
count += 1; /* Shorthand for count = count + 1 */
}
return 0;
}
Continue & Break

 The continue statement is used to force


program execution to the bottom of the loop,
skipping any statements that come after it.

 The break statement is used to halt execution


of a loop prior to the loop’s normal test
condition being met.

 The exit statement causes the whole program


to terminate if it is called within the main
program block.
Coding for readability
int main()
{
int i,x,y;
Always indent after
float z; a left bracket
.
.
Right bracket if (x < 7)
Start a
{
level with y= 3; left bracket after
statement which } z= 4.2; a statement
started it else if (x > 23)
{
y= 5;
z= 7.2
}
for (i= 1; i < 200; i++) {
for (j= 1; j < 200; j++ {
/* Inside both loops */
}
/* Inside only the first loop */
}
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy