Thread: Flow diagram If Elseif - Else statement

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    81

    Flow diagram If Elseif - Else statement

    I have made it flow diagram and I want understand how it would be look in code structure

    Flow diagram If Elseif - Else statement-ts-jpg

    What would be the structure of the code for this function flow diagram ?

    Code:
    #include<stdio.h>
    
    int main ()
    {
            
            if ( expression1)
            {
                   printf("statment 1");
            }
            
            else 
            {
                  printf("statment 2");
            }
            
            
    return 0;
    
    
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What's your best try?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Maybe if you gave each expression in your diagram a different name, it would help when you saw the code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Oct 2019
    Posts
    81
    Quote Originally Posted by laserlight View Post
    What's your best try?

    I shown my best making diagram I don't even know if this can happen real once I get the idea, I will try to make more diagrams


    Quote Originally Posted by Salem View Post
    Maybe if you gave each expression in your diagram a different name, it would help when you saw the code.
    Flow diagram If Elseif - Else statement-ts-jpg

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Like so - for you to finish off...
    Code:
    if ( E1 ) {
      if ( E2 ) {
        S2;
      } else {
        S21;
      }
    } else {
      if ( E3 ) {
      } else {
      }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Oct 2019
    Posts
    81
    Quote Originally Posted by Salem View Post
    Like so - for you to finish off...
    alright I am converting logic into code but I am surprised that you did not use the else if statement

    Code:
    #include<stdio.h>
    
    int main ()
    {
    	int n = 3;
    	
       if ( n == 1 )
    	   {
             if ( n == 2 )
    			 { 
            		 printf(" 2 \n");
    				 } 
    				 else 
    				 {
                        printf(" 23 \n");
                     }
                 }
     else 
     {
      if (n == 3 ) 
      {
    	   printf(" 3 \n");
      } 
      else 
      
      {
    	   printf(" 4 \n");
      }
    }
    
    
    return 0;
    }
    3

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > but I am surprised that you did not use the else if statement
    That would imply that you have more than true/false at some level.

    Code:
    if ( n == 1 ) {
    } else if ( n == 2 ) {
    } else {
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Flow chart or state diagram
    By Satya in forum C Programming
    Replies: 3
    Last Post: 12-19-2016, 12:12 PM
  2. C Code for a flow diagram
    By unity006 in forum C Programming
    Replies: 4
    Last Post: 11-08-2015, 04:24 PM
  3. Switch statement does not flow through cases
    By sjmp in forum C Programming
    Replies: 17
    Last Post: 07-27-2012, 07:12 AM
  4. Replies: 2
    Last Post: 08-12-2010, 01:55 PM
  5. Replies: 6
    Last Post: 11-10-2005, 01:37 PM

Tags for this Thread