Understanding Selection Structures: if, else if, and else in C Programming, Slides of Computer Engineering and Programming

An in-depth explanation of selection structures in c programming, focusing on the use of if, else if, and else statements. Selection structures enable programmers to make decisions based on logical expressions and execute alternate actions accordingly. The syntax and usage of single-selection, double-selection, and nested if/else structures, as well as examples of simple and complex programs.

Typology: Slides

2012/2013

Uploaded on 05/06/2013

apsara
apsara 🇮🇳

4.5

(2)

86 documents

1 / 18

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Control Statements
Lecture 8
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12

Partial preview of the text

Download Understanding Selection Structures: if, else if, and else in C Programming and more Slides Computer Engineering and Programming in PDF only on Docsity!

Control Statements

Lecture 8

IF

If you can keep your head when all about you Are losing theirs and blaming it on you, If you can trust yourself when all men doubt you But make allowance for their doubting too, If you can wait and not be tired by waiting, Or being lied about, don't deal in lies, Or being hated, don't give way to hating, And yet don't look too good, nor talk too wise: THEN Yours is the Earth and everything that's in it, And--which is more--you'll be a Man, my son! --Rudyard Kipling

if / else if / else Selection

Structures

  • A simple if structure is called a single-selection structure because it either selects or ignores a single action.
  • The if / else structure is called a double-selection structure because it selects between two different actions.
  • Nested if / else structures test for multiple cases by placing if / else structures inside other if / else structures.

if / else Selection Structures

  • Syntax for the if selection structure is as follows:

if ( this logical expression is true ) statement ;

  • Syntax for the if / else selection structure is as follows:

if ( this logical expression is true ) statement ;

else statement ;

if / else Selection Structures

  • The if selection structure is often written as:

if ( this logical expression is true ) statement ;

  • And, the if / else selection structure is often written as:

if ( this logical expression is true ) statement ; else statement ;

no semi-colon!

if / else Selection Structures

  • Often, the earlier example is written this way:

#include <stdio.h> int main ( ) { int a = 1, b = 2, c ; if (a > b) c = a ; else c = b ; }

if / else if / else Selection Structures

  • 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 ;

if / else if / else Selection

Structures

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 ;

} Docsity.com

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) ; Docsity.com

Grading Program Using if / else if

/ else

if (score >= 90) printf ("Your score of %d is a A\n", score) ; else if (score >= 80 && score < 90) printf ("Your score of %d is a B\n", score) ; else if (score >= 70) printf ("Your score of %d is a C\n", score) ; else if (score >= 60) printf ("Your score of %d is a D\n", score) ; else printf ("Your score of %d is an E\n", score) ;

}

A Little Help for Assignment G

  • But if we just use:

answer = rand ( ) % 11 ;

we'll get the same "random" value each

time we run the program.

  • So, how do we get something more

"random".

  • We must first "seed" the random number

generator: Docsity.com

A Little Help for Assignment G

  • On a UNIX system, the time of day

(expressed in seconds) is "sort of" a

random number:

srand ( time (NULL) ) ;

  • Where do these functions "live"?
    • rand, srand functions are in <stdlib.h>
    • time function is in <time.h>
  • Check C Library resources Docsity.com