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

Lecture 6

The document provides an overview of control statements in the C programming language, focusing on selection structures such as simple if, if/else, and nested if/else. It includes syntax examples and practical applications, such as comparing numbers and grading based on test scores. Additionally, it introduces the switch statement and common programming errors to avoid.

Uploaded by

madin20232022
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lecture 6

The document provides an overview of control statements in the C programming language, focusing on selection structures such as simple if, if/else, and nested if/else. It includes syntax examples and practical applications, such as comparing numbers and grading based on test scores. Additionally, it introduces the switch statement and common programming errors to avoid.

Uploaded by

madin20232022
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

University of Hafr Al-Batin

Hafr Al-Batin Community College ( HBCC )

CST121+ CNT234 : Introduction to


Computer Programming

Control statements in C language


Lecture-6

1
Selection Structures
• The if selection structure can be used in three
different ways:

1. A simple if structure

2. The if / else structure

3. Nested if / else structures

2
if
• A simple if structure is called a single-
selection structure because it either selects or
ignores a single action.
• Syntax for the if selection structure is as
follows:
if ( this logical expression is true ) statement ;
• The if selection structure is often written as:
if ( this logical expression is true )
statement ; no semi-colon!

3
if / else
• The if / else structure is called a double-
selection structure because it selects between
two different actions.
• Syntax for the if / else selection structure :
if ( this logical expression is true ) statement ;
else statement ;
• if / else selection structure is often written as:
if ( this logical expression is true )
statement ;
else no semi-colon!
statement ; 4
if statement : Two alternatives

5
if / else if / else
• Nested if / else structures test for multiple
cases by placing if / else structures inside
other if / else structures.
• Syntax for the if / else if / else selection
structure is as follows:
if ( this logical expression is true )
statement ;
else if ( this logical expression is true )
statement ;
else
statement ; 6
if / else if / else
• The actual syntax for the multiple if / else if / else
selection structure is as follows:
if ( this logical expression is true )
statement ;
else if ( this logical expression is true )
statement ;
else if ( this logical expression is true )
statement ;
else
statement ;

7
if / else if / else
• Curly brackets {} can be used to put one or more
statements in a block.
if ( this logical expression is true )
{
Execute statements in this block ;
}
else if ( this logical expression is true )
{
Execute statements in this block ;
}
else
{
Execute statements in this block ;
}
8
Example 1
• One way of writing if/else: • Another way of writing
if/else:

#include <stdio.h> #include <stdio.h>


main ( ) main ( )
{ {
int a = 1, b = 2, c ; int a = 1, b = 2, c ;
if (a > b)
if (a > b) c = a; c=a;
else c = b; else
} c=b;
}
9
Example 2
// a program to compare two numbers
#include <stdio.h>
main ( )
{
int a , b ;
printf ("Enter values for a and b > ") ;
scanf ("%d%d", &a, &b ) ;
if ( a < b )
printf ("a is less than b \n") ;
else if ( a == b )
printf (" a is equal to b \n") ;
else
printf ("a is larger than b \n") ;
} 10
Example 3
/*
Grading program using if / else if / else.
This program associates letter grades with numeric test scores
*/

#include <stdio.h>
int main ( )
{
int score ;
printf (“Enter your test score >") ;
scanf ("%d", &score) ;
11
Cont…
if (score >= 90)
printf ("Your score of %d is grade A \n", score) ;
else if (score >= 80)
printf ("Your score of %d is grade B \n", score) ;
else if (score >= 70)
printf ("Your score of %d is grade C \n", score) ;
else if (score >= 60)
printf ("Your score of %d is grade D \n", score) ;
else
printf ("Your score of %d is grade F \n", score) ;
}

12
The switch Statement
• This is another form of the multi way decision. It is
well structured, but can only be used in certain cases
where;
– Only one variable is tested, all branches must depend on
the value of that variable. The variable must be an integral
type (int, long, short or char).
– Each possible value of the variable can control a single
branch. A final, catch all, default branch may optionally be
used to trap all unspecified cases.

13
The switch Statement
• The syntax of the switch statement is as follows:

switch(<expression>)
{
case <constant expression> :
<statements>
break;
………
default:
<statements>
} 14
Example of switch
#include <stdio.h>
void main (void)
{
float n1, n2;
int option;

printf("Please enter two real numbers \n");


scanf("%f %f", &n1, &n2);

printf("Available options:\n 1. add \n 2. subtract \n 3. multiply \n 4. divide \n ");


printf("Enter your choice ... ");
scanf("%d", &option);
A sample OUTPUT of the program:
switch (option)
{
case 1: printf("Answer = %f \n", n1 + n2);
break;
case 2: printf("Answer = %f \n", n1 - n2);
break;
case 3: printf("Answer = %f \n", n1 * n2);
break;
case 4: printf("Answer = %f \n", n1 / n2);
break;
default: printf("Error: You have input a wrong choice");
}
} 15
CommonProgramming Errors
• Don’t forget to parenthesize the condition.
• Don’t forget the { and } if they are needed
• C matches each else with the closest unmatched if, so be
careful so that you get the correct pairings of if and else
statements.
• In switch statements, make sure the controlling expression
and case labels are of the same permitted type.
• Remember to include the default case for switch statements.
• Don’t forget your { and } for the switch statement.
• Don’t forget your break statements!!!

16

You might also like